天天看点

[转] iOS8 定位新增功能

从iOS诞生之日起,系统便配带了定位功能,随着无线互联网的需求,移动功能的日新月异的变化,以及iOS的不断升级,定位功能都有不断的更新,自iOS7加入了iBeacon,为无线移动推送提供了更多的变数以后,在iOS8里面定位功能也有不俗的表现。

Core Location framework的变化主要有以下几点:

1. 在定位状态中引入Always 和WhenInUse的概念。

2. 加入Visit monitoring的特性, 这类特性特别适合旅行类别的应用,当用户到达某个指定的区域内,monitor开始作用。

3.加入室内定位技术,增加CLFloor, 在室内可以得到楼层信息。

下面针对这三种分别详细的讲一下使用方法。

a.定位的种类分为:

持续的更新:location, background location, ranging (Always/WhenInUse work)

监视类的更新:region monitoring, significant location changes (Always work)

其中持续的后台位置更新,在程序前台的时候发起的,WhenInUse模式也可以得到正确的地理位置更新,但是如果是类似于从后台被唤起这种服务,则需要使用Always authorization

c. 增加了跳转到privacy的link: UIApplicationOpenSettingsURLString当需要提示关闭了定位功能的用户使用定位的时候可以给通过如下的方式跳转到设定画面:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]];

e. kCLAuthorizationStatus,由原来的kCLAuthorizationStatusAuthorized,变为kCLAuthorizationStatusAuthorizedAlways和kCLAuthorizationStatusAuthorizedWhenInUse

具体的使用方法:

A. 决定是否开启后台模式:在Target->capsbilities->backgourn modes

B. 在plist中增加WhenInUse/Always的提示文字,使用NSLocationWhenInUseUsageDescription /NSLocationAlwaysUseUsageDescription

C. 请求不同的服务:

地理位置变化:

self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;

[self.locationManager requestWhenInUseAuthorization];

[self.locationManager startUpdatingLocation]

监听region:

self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;

CLCircularRegion *region = [[CLCircularRegion alloc]...];

[self.locationManager requestAlwaysAuthorization];

[self.locationManager startMonitoringForRegion:region];

获取地理位置并监听region:

在plist里同时设置NSLocationWhenInUseUsageDescription和NSLocationAlwaysUseUsageDescription,调用方式可以参考上面,但是需要注意的是,always的时候可能需要对模式进行判断。

if (authorizationStatus == kCLAuthorizationStatusDenied || authorizationStatus == kCLAuthorizationStatusWhenInUse) {

// TODO:do what you want e.g. goto setting view

}

[self.locationManager requestAlwaysAuthorization];

[self.locationManager startMonitoringForRegion:region];

D: 集成的时候可以使用responsToSelector来避免iOS7.和iOS8api不同引发的错误。

E: 对于只是在MKMapView和Html5中使用的定位同样需要在plist中对提示增加自定义。

@interface CLLocationManager (CLVisitExtensions)

  • (void)startMonitoringVisits NSAVAILABLE(NA, 80); // 开启监视
  • (void)stopMonitoringVisits NSAVAILABLE(NA, 80); // 停止监视

当设备到达指定的地点时,系统会调用下面的方法来通知。

  • (void)locationManager:(CLLocationManager *)manager didVisit:(CLVisit *)visit; 具体可以拿到的信息。 @interface CLVisit : NSObject @property (nonatomic, readonly, copy) NSDate *arrivalDate; @property (nonatomic, readonly, copy) NSDate *departureDate; @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; // center of region @property (nonatomic, readonly) CLLocationAccuracy horizontalAccuracy;

使用功能:

self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;

[self.locationManager requestAlwaysAuthorization];

[self.locationManager startUpdatingLocation]

<h5 500;="" color:="" #333333;\"="" style="font-size: 20px; margin: 0px; padding: 0px; font-weight: normal; letter-spacing: 0em; color: rgb(1, 35, 49); word-wrap: break-word !important;">回调:

  • (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { for (CLLocation *newLocation in locations) { CLFloor *floor = newLocation.floor; NSInteger level = floor.level; } }