`
sillycat
  • 浏览: 2551574 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

IPhone and Location(2)Documents Region Monitoring and Region Sample

 
阅读更多

IPhone and Location(2)Documents Region Monitoring and Region Sample

The Core Location framework provides 2 ways to detect a user's entry and exit.
a. geographical region monitoring
b. beacon region monitoring

In iOS, regions associated with your app are tracked at all times, including when our app is not running. If a region boundary is crossed while an app is not running, that app is relaunched into the background to handle the event. If the app is suspended when the event occurs, it is woken up and given a short time (10 seconds) to handle the event.

The same when you want more time, call beginBackgroundTaskWithExpirationHandler and call endBackgroundTask after that.
 
Determining the Availability of Region Monitoring
In iOS 7.0 and later, we should always call the isMonitoringAvailableForClass: and authorizationStatus of CLLocationManager

If our app needs to process location updates in the background, be sure to check the backgroundRefreshStatus property of the UIApplication class.

Monitoring Geographical Regions
In iOS 7.0 and later, you define geographical regions using the CLCircularRegion class(old class CLRegion).

To register a region, call the startMonitoringForRegion: method of our CLLocationManager Object.

We store the region information with an identifier.

-(void) registerRegionWithCircularOverlay: (MKCircle*) overlay andIdentifier:(NSString*)identifier {
     //if the radius is too large, registration fails automatically
     CLLocationDegrees radius = overlay.radius;
     if(raids > self.locManager.maximumRegionMonitoringDistance) {
          radius = self.locManager.maximumRegionMonitoringDistance;
     }

     CLCircularRegion *geoRegion = [[CLCircularRegion alloc]
          initWithCenter:overlay.coordinate
                      radius:radius
                   identifier:identifier];
     [self.locManager startMonitoringForRegion:geoRegion];
}

The monitoring of a geographical region begins immediately, but the event will not happen if you are already in the region.

We can use the method requestStateForRegion method of the CLLocationManger to check whether the user is already inside the boundary of a region.

Regions are a shared system resource and the total number of regions available systemwide is limited. Single app limitation is 20.
If you try to register a region while the space is unavailable, the location manager calls the locationManager:monitoringDidFailForRegion:withError: method of its delegate with the KCLErrorRegionMonitoringFailure error code.

Handling Boundary-Crossing Events for a Geographical Region
a. locationManager: didEnterRegion:
b. locationManager: didExitRegion:

We can customize which boundary-crossing events notify our app by setting the notifyOnEntry and notifyOnExit properties of the CLRegion class(The default value of both properties is YES)

The system does not report boundary crossings until the boundary plus a system-defined cushion distance is exceeded.

Monitoring Beacon Regions
A proximity UUID
A major value
A minor value

If all the stores are monitored by the same UUID, each of which is different by a different major value. In addition, the app can use different minor values to distinguish different departments within the same store.

Defining a Beacon Region to Be Monitored
CLBeaconRegion -----> proximityUUID, major, minor.

To register a beacon region, call the startMonitoringForRegion: of CLLocationManager object.

-(void) registerBeaconRegionWithUUID:(NSUUID *) proximityUUID and Identifier:(NSString*)identifier{
     CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID
                                                                                          identifier:identifier];
     [self.locManager startMonitoringForRegion:beaconRegion];
}

Handling Boundary-Crossing Events for a Beacon Region
When the enters happen, the location manager calls the locationManager:didEnterRegion, locationManager:didExitRegion.

Determining the Proximity of a Beacon Using Ranging
While the user's device is inside a registered beacon region, apps can use the startRangingBeaconsInRegion of CLLocationManager class to determine the relative proximity of one or more beacons in the region.( Call isRangingAvailable before that.)

Whenever beacons in this specified beacon region come within range, go out of range, or their proximity changes, the location manager calls locationManager:didRangeBeacons:inRegion of its delegate object.

The value of the proximity property of the CLBeacon gives a general sense of the relative distance to a beacon.

- (void) locationManager:(CLLocationManager *) manager
               didRangeBeacons:(NSArray *)beacons
                    inRegion:(CLBeaconRegion *) region {
     if([beacons count] > 0) {
          CLBeacon *nearestExhibit = [beacons firstObject];

          if(CLProximityNear == nearestExhibit.proximity){
               [self presentExhibitInfoWithMajorValue:nerestExhibit.major.integerValue];
          }else{
               [self dismissExhibitInfo]
          }
     }
}

Turn Your iOS Device or Mac into a Beacon
Link your app to CoreBluetooth.framework and 
#import <CoreBluetooth/CoreBluetooth.h> 

Creating and Advertising a Beacon Region
generate a 128-bit UUID
>uuidgen

NSUUID *proximityUUID = [[NSUUID alloc]
                    initWithUUIDString:@"A6A71452-13FB-4245-833B-C555A118D383" ];
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc]
                              initWithProximityUUID:proximityUUID
                                   identifier:@"com.sillycat.easylocation_1"

After that we need to advertise this beacon using CBPeripheralManager from the Core Bluetooth framework.

To advertise peripheral data in Core Bluetooth, we call the startAdvertising method of the CBPeripheralManager.

NSDictionary *beaconPeripheralDate = [beaconRegion peripheralDataWithMessuredPower:nil];

CBPeripheralManager *peripheralManager = [[CBPeripheralManager alloc]
                                        initWithDelegate:self queue:nil options:nil];
[peripheralManager startAdvertising:beaconPeripheralData];

Testing Your App's Region Monitoring Support


Region Sample Codes
https://developer.apple.com/library/ios/samplecode/Regions/Introduction/Intro.html
https://github.com/nicktoumpelis/HiBeacons.git


References:
sample codes
https://developer.apple.com/library/ios/search/?q=location+sample

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html#//apple_ref/doc/uid/TP40009497-CH9-SW1

NSOperationQueue
http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
http://software.intel.com/zh-cn/articles/3-nsoperationqueue


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics