天天看點

[轉] 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; } }