update: 2016/07/30
reference:
1. Beacon Tech Overview - Estimote Developer
Part 2: Background monitoring
A. Start beacon monitoring
1. 開啟 AppDelegate.swift 修改如下:
....
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//@add ################################
// 3. Set the beacon manager's delegate
self.beaconManager.delegate = self
//@add ################################
// request the authorization:
self.beaconManager.requestAlwaysAuthorization()
//@add ################################
// case 1: Start monitoring for UUID & major & minor
/*
self.beaconManager.startMonitoringForRegion(CLBeaconRegion(
proximityUUID: NSUUID(UUIDString: "FDA50693-A4E2-4FB1-AFCF-C6EB07647825")!,
major: 0, minor: 5, identifier: "UUID, major, minor: monitored region"))
*/
// case 2: Start monitoring for UUID & major
/*
self.beaconManager.startMonitoringForRegion(CLBeaconRegion(
proximityUUID: NSUUID(UUIDString: "FDA50693-A4E2-4FB1-AFCF-C6EB07647825")!,
major: 0, identifier: "UUID, major: monitored region"))
*/
// case 3: Start monitoring for UUID
self.beaconManager.startMonitoringForRegion(CLBeaconRegion(
proximityUUID: NSUUID(UUIDString: "FDA50693-A4E2-4FB1-AFCF-C6EB07647825")!, identifier: "UUID: monitored region"))
// stop Monitoring For Region
/*
self.beaconManager.stopMonitoringForRegion(CLBeaconRegion(
proximityUUID: NSUUID(UUIDString: "FDA50693-A4E2-4FB1-AFCF-C6EB07647825")!, identifier: "UUID: monitored region"))
*/
// check regionsCount
/*
let regionsCount = self.beaconManager.monitoredRegions.count
print("regionsCount = \(regionsCount)")
*/
return true
}
....
2. 結果:
執行 app 並 Lock 住, 當 Unlock 時, 左下角會出現: Suggested apps,
將其往上滑(swiped up), 就可以開啟此 app.
說明: 必須配合 self.beaconManager.requestAlwaysAuthorization() 才有效
B. Show an "enter / exit" region notification
1. 開啟 AppDelegate.swift 修改如下:
....
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
....
//@add ################################
// to show notifications
UIApplication.sharedApplication().registerUserNotificationSettings(
UIUserNotificationSettings(forTypes: .Alert, categories: nil))
return true
}
//@add ################################
// Show an "enter" notification
func beaconManager(manager: AnyObject, didEnterRegion region: CLBeaconRegion) {
let notification = UILocalNotification()
notification.alertBody =
"You enter the region!"
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
print("You enter the region! identifier: \(region.identifier)")
}
//@add ################################
// Show an "exit" notification
func beaconManager(manager: AnyObject, didExitRegion region: CLBeaconRegion) {
let notification = UILocalNotification()
notification.alertBody =
"You exit the region!"
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
print("You exit the region! identifier: \(region.identifier)")
}
....
2. 結果:
說明: 必須配合 self.beaconManager.requestAlwaysAuthorization() 才有效
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。