`
sillycat
  • 浏览: 2539740 次
  • 性别: 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


分享到:
评论

相关推荐

    Head First iPhone and iPad Development, 3rd Edition

    A Learner’s Guide to Creating Objective-C Applications for the iPhone and iPad Book Description : Let’s say you have a killer app idea for iPhone and iPad. Where do you begin? Head First iPhone and...

    iOS 10 SDK Development: Creating iPhone and iPad Apps with Swift

    iOS 10 SDK Development: Creating iPhone and iPad Apps with Swift by Chris Adamson English | 24 Mar. 2017 | ASIN: B071RRCK9R | 264 Pages | AZW3 | 5.24 MB All in on Swift! iOS 10 and Xcode 8 make it ...

    Learn iPhone and iPad Cocos2D Game Developmentn中文版

    《Learn iPhone and iPad Cocos2D Game Development》中文版是一本专为苹果移动平台开发者设计的游戏开发教程。Cocos2D是一款广泛使用的2D游戏引擎,尤其在iOS平台上备受青睐,因为它提供了强大的图形渲染能力、丰富...

    Learn iPhone and iPad Cocos2D Game Development

    By creating 2-3 sample games over the course of the book, you’ll learn key concepts of the cocos2d game engine and relevant tools like Zwoptex (TextureAtlas), ParticleDesigner (Particle Effects), ...

    Learn iPhone and iPad cocos2d Game Development中文版全集

    总之,《Learn iPhone and iPad cocos2d Game Development中文版全集》是学习cocos2d游戏开发的宝贵资源,无论你是初入游戏开发的新手,还是寻求提高的资深开发者,都能从中受益匪浅,掌握创建令人惊叹的iOS游戏所需...

    Head First iPhone and iPad Development, 2nd Edition.pdf

    Head First iPhone and iPad Development, 2nd Edition.pdf

    iPhone small app sample-2

    One iPhone app example. Use image-controller, pumkinFace mask, and transitions in it. run on the iPhone SDK. not iPhone.

    Learn iPhone and iPad Cocos2D Game Development(中文版)

    01 介绍cocos2d 02 开始学习 03 基础知识 04 你的第一个游戏 05 游戏构成要素 06 深入了解精灵 07 横向滚屏射击游戏 08 完成滚屏射击游戏 09 粒子效果 10 瓷砖地图基础知识 11 45度角瓷砖地图 ...13 弹球游戏

    Building iPhone and iPad Electronic Projects

    2. iOS外围传感器编程:描述中提及了“iOS外围传感器编程”,这说明书中将对如何访问和使用iPhone和iPad的内置传感器进行编程作出介绍。这可能包括加速度计、陀螺仪、磁力计等传感器的编程接口和方法。 3. ...

    Location-and-Map-Sample-master.zip_GPS

    标题中的"Location-and-Map-Sample-master.zip_GPS"暗示了一个iOS开发相关的项目,它专注于获取设备的GPS(全球定位系统)位置,包括纬度和经度。这个项目可能是一个示例应用,展示了如何在iPhone SDK中集成GPS功能...

    学习iPhone and iPad cocos2d

    《学习iPhone and iPad cocos2d》是一份专为iOS设备开发者设计的学习资源,特别是针对使用cocos2d游戏引擎的初学者。cocos2d是一个开源的2D游戏开发框架,广泛应用于iPhone、iPad等iOS设备上的游戏和交互式应用开发...

    Developing Hybrid Applications for the iPhone: Using HTML, CSS, and JavaScript to Build Dynamic Apps for the iPhone

    Along the way, he introduces techniques and sample code designed to streamline development, eliminate complexity, optimize performance, and leverage all iPhone’s native capabilities—from its ...

    Learn iPhone and iPad Cocos2D Game

    《Learn iPhone and iPad Cocos2D Game Development》是一本专为iOS游戏开发爱好者和专业人士编写的英文原版书籍,旨在帮助读者深入理解如何利用Cocos2D框架创建高性能、交互性强的游戏。Cocos2D是一个广泛使用的...

    iphone cocos2d game sample

    【标题】"iPhone Cocos2D游戏示例"是一份专为Cocos2D游戏初学者准备的宝贵资源。这个项目提供了一个实际的游戏样本,帮助新手理解如何在iOS平台上使用Cocos2D框架构建游戏。 【描述】"适用于Cocos2D游戏初学者的源...

    iPhone.SDK.3.Programming:Advanced.Mobile.Development.for.Apple.iPhone.and.iPod.touch(Wiley.2009-9).pdf

    iPhone SDK 3 Programming:Advanced Mobile Development for Apple iPhone and iPod touch,英文版本,大小 9 Mb,作者 Maher Ali,2009 年 9 月出版。 Get the expert guidance you need to begin building native...

    [iOS] iPhone & iPad 应用开发 初学者指南 第3版 (英文版)

    It cuts through the fog of jargon and misinformation that surrounds iPhone and iPad app development, and gives you simple, step-by-step instructions to get you started. ☆ 出版信息:☆ [作者信息] ...

    Developing iOS 7 Apps for iPhone and iPad ppt (1-10)

    斯坦福大学的"Developing iOS 7 Apps for iPhone and iPad"课程是学习如何构建iOS应用程序的宝贵资源,尤其适合初学者和有经验的开发者。这门课程采用最新的Xcode 5作为开发环境,针对苹果公司的操作系统iOS 7进行...

Global site tag (gtag.js) - Google Analytics