ios項目開發— iOS8 定位功能API改變

來源:互聯網
上載者:User

標籤:

iOS8 定位新增功能

  一、  Core Location framework的變化主要有以下幾點:
         1. 在定位狀態中引入Always 和WhenInUse的概念。
         2. 加入Visit monitoring的特性, 這類特性特別適合旅行類別的應用,當使用者到達某個指定的地區內,monitor開始作用。
         3.加入室內地圖技術,增加CLFloor, 在室內可以得到樓層資訊。

下面針對這三種分別詳細的講一下使用方法。

二、.定位的種類分為:

持續的更新: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); // 停止監視

三、在IOS8中定位功能新增了兩個方法:

 

- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);

這兩個新增的方法導致,之前寫的程式在iOS8運行會出現,定位功能無法正常使用

這樣讓iOS8正常使用定位功能呢?

<1>你需要在info.plist表裡面添加兩條變數

 

在Info.plist中加入兩個預設沒有的欄位

    • NSLocationAlwaysUsageDescription

    • NSLocationWhenInUseUsageDescription

這兩個欄位沒什麼特別的意思,就是自訂提示使用者授權使用地理定位功能時的提示。

 

ios項目開發— iOS8 定位功能API改變

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.