第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

獲取iOS App的位置更新即使暫停

獲取iOS App的位置更新即使暫停

慕標(biāo)琳琳 2019-08-12 16:33:59
獲取iOS App的位置更新即使暫2014年初,Apple已將iOS 7.0更新為7.1,以便即使應(yīng)用程序在前臺而非后臺處于活動狀態(tài)時(shí)也允許位置更新。我們怎么做?我讀過一些文章,比如Apple的iOS 7.1將修復(fù)地理定位錯(cuò)誤。但是,即使應(yīng)用程序被終止/終止/暫停,Apple也沒有提供與此相關(guān)的任何通信,也沒有提供有關(guān)如何獲取位置更新的示例代碼。我已閱讀iOS 7.1發(fā)行說明。我也找不到任何相關(guān)的東西。那么,即使應(yīng)用程序被暫停,我們?nèi)绾螌?shí)際獲得iOS 7和8的位置更新?
查看完整描述

2 回答

?
翻閱古今

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊

通過試驗(yàn)核心位置框架經(jīng)過數(shù)月的試驗(yàn)和錯(cuò)誤后,即使應(yīng)用程序被殺死/暫停,我也找到了獲取位置更新的解決方案。它適用于iOS 7和8。

這是解決方案: -

如果您的應(yīng)用是基于位置的移動應(yīng)用,需要在設(shè)備發(fā)生重大變化時(shí)監(jiān)控設(shè)備的位置,當(dāng)設(shè)備距離上一個(gè)已知位置移動超過500米時(shí),iOS會返回一些位置坐標(biāo)。是的,即使應(yīng)用程序被用戶或iOS本身殺死/暫停,您仍然可以獲得位置更新。

因此,locationManager即使應(yīng)用程序被殺死/暫停,為了獲得位置更新,您必須使用該方法startMonitoringSignificantLocationChanges,您不能使用startUpdatingLocation

當(dāng)iOS想要將位置更新返回給應(yīng)用程序時(shí),它將幫助您重新啟動應(yīng)用程序并將密鑰返回UIApplicationLaunchOptionsLocationKey到應(yīng)用程序委托方法didFinishLaunchingWithOptions

關(guān)鍵UIApplicationLaunchOptionsLocationKey是非常重要的,你必須知道如何處理它。您必須在收到密鑰時(shí)創(chuàng)建新的locationManager實(shí)例,并且您將獲得locationManager委托方法的位置更新didUpdateLocations。

以下是示例代碼: -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    self.shareModel = [LocationShareModel sharedModel];

    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) { 
      self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
      self.shareModel.anotherLocationManager.delegate = self;
      self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
      self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

      if(IS_OS_8_OR_LATER) {
        [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
      }

     [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];   

    }    
        return YES;
 }

除了didFinishLaunchingWithOptions方法之外,我還在locationManager應(yīng)用程序處于活動狀態(tài)時(shí)創(chuàng)建了實(shí)例。以下是一些代碼示例:

- (void)applicationDidEnterBackground:(UIApplication *)application{
    [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];

    if(IS_OS_8_OR_LATER) {
        [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
    }

    [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];}- (void)applicationDidBecomeActive:(UIApplication *)application{
    if(self.shareModel.anotherLocationManager)
        [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];

    self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
    self.shareModel.anotherLocationManager.delegate = self;
    self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

    if(IS_OS_8_OR_LATER) {
        [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
    }

    [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];}

我寫了一篇文章解釋有關(guān)如何獲取iOS 7和8的位置更新的詳細(xì)信息,即使應(yīng)用程序被殺死/暫停也是如此。我還在GitHub上傳了完整的源代碼,其中包含如何測試此解決方案的步驟。


查看完整回答
反對 回復(fù) 2019-08-12
?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

locationManager = [[CLLocationManager alloc] init];#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)if(IS_OS_8_OR_LATER){
    [locationManager requestWhenInUseAuthorization];}locationManager.delegate = self;locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we movelocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;[locationManager startUpdatingLocation];

該代碼用戶位置僅更新forground app運(yùn)行但不運(yùn)行后臺運(yùn)行

[locationManager requestWhenInUseAuthorization];


查看完整回答
反對 回復(fù) 2019-08-12
  • 2 回答
  • 0 關(guān)注
  • 832 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號