2011-02-03 67 views
65

我有一個具有明確用戶交互的應用程序,該用戶可以使用用戶的當前位置。如果用戶拒絕訪問位置服務,我仍然希望隨後的使用能夠提示用戶轉到設置併爲我的應用重新啓用位置服務。如何在用戶拒絕使用後提示用戶打開位置服務

我想要的行爲是內置的地圖應用:

  1. 在設置復位位置警告>通用>還原>還原位置警告。
  2. 啓動地圖應用程序。
  3. 點擊左下角的當前位置按鈕。
  4. 帶「」地圖「的地圖提示將使用您當前的位置」| 「不允許」| 「允許」。
  5. 選擇「不允許」選項。
  6. 再次點擊左下角的當前位置按鈕。
  7. 將地圖提示與「打開位置服務以允許」地圖「確定您的位置」| 「設置」| 「取消」。

在自己的應用程序,同樣的基本流程的結果在我的CLLocationManagerDelegate -locationManager:didFailWithError:方法被調用了kCLErrorDenied錯誤,在最後步驟,並且用戶沒有給出,打開設置應用程序來糾正選項它。

我可以顯示自己的警報來響應錯誤,但它不能啓動設置應用程序,就像操作系統可以提供內置地圖應用程序所使用的警報一樣。

CLLocationManager類中有什麼我缺少能夠給我這種​​行爲嗎?

+0

現在,我只是顯示一個警告用戶,要求他們去設置以重新啓用它。我也很想聽到更好的解決方案。 – donkim 2011-02-03 23:22:13

+0

我也想回答這個問題,當然還有更好的方法 – conorgriffin 2011-02-21 19:46:58

+0

我發現CoreLocation並不滿足這個原因。我最終使用了容易整合和有據可查的skyhook庫。座標似乎也更精確。唯一的缺點是必須將1.5MB dylib與應用程序捆綁在一起。 – 2011-03-17 13:32:50

回答

39

使用iOS8,您最終可以通過openURL將用戶鏈接到設置應用程序。例如,您可以創建一個單一的按鈕,用戶將進入到設置應用一個UIAlertView中:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ICLocalizedString(@"LocationServicesPermissionTitle") 
                message:ICLocalizedString(@"LocationPermissionGeoFenceMessage") 
                delegate:self 
              cancelButtonTitle:@"Settings" 
              otherButtonTitles:nil]; 
    [alert show]; 

在你的UIAlertView中委託:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES]; 
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: UIApplicationOpenSettingsURLString]]; 
} 
2

我想你會在蘋果會考慮新的SDK時回答你的問題。在當前的時間和據我所知,這是不可能的:

沒有URL處理程序提供
沒有可用的方法來調用

但是......隨着地圖這樣做,這是可以做到的,但可能使用私人API。如果你不害怕這種編碼,你應該在我的意見中搜索。

31

更新:

由於iOS 8的的,現在有不斷UIApplicationOpenSettingsURLString,其表示,打開時,打開設置應用到你的應用程序的設置(用戶可以重新啓用位置的URL服務)。


原文:

有沒有方法可以讓你做到這一點。您唯一真正的選擇是顯示警報,通知用戶您的應用程序需要位置服務,並指示他們手動轉到設置應用程序並將其打開。

+0

我已閱讀過Apple文檔,並根據文檔發佈了以下答案,但我沒有對自己進行測試。這是SDK中的變化還是相同? – 2012-07-22 17:49:36

+0

恐怕這個答案不再正確。你可能希望更新或刪除你的答案。 – 2015-07-10 12:59:57

6

根據Apple's DocslocationServicesEnabled方法。

The user can enable or disable location services from the Settings application by toggling the Location Services switch in General.

You should check the return value of this method before starting location updates to determine whether the user has location services enabled for the current device. If this method returns NO and you start location updates anyway, the Core Location framework prompts the user to confirm whether location services should be reenabled.

所以你不能剛剛開始位置服務更新任何方式導致警報提示?

16

AlertViews被棄用在iOS的8有現在有更好的方式使用新AlertController處理警報:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Enter your title here", @"") message:NSLocalizedString(@"Enter your message here.", @"") preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"") style:UIAlertActionStyleCancel handler:nil]; 
UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Settings", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: 
                UIApplicationOpenSettingsURLString]]; 
}]; 

[alertController addAction:cancelAction]; 
[alertController addAction:settingsAction]; 

[self presentViewController:alertController animated:YES completion:nil]; 
0

斯威夫特3擴展創建設置報警控制器:

進口基金會

extension UIAlertController { 
    func createSettingsAlertController(title: String, message: String) -> UIAlertController { 
     let controller = UIAlertController(title: title, message: message, preferredStyle: .alert) 

     let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment:""), style: .cancel, handler: nil) 
     let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment:""), style: .default, handler: { action in 
      UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!) 
     }) 
     controller.addAction(cancelAction) 
     controller.addAction(settingsAction) 

     return controller 
    } 
} 
1

下面的代碼在由馬庫斯回答迅捷的版本。此代碼會創建一個警報,使用戶可以選擇打開設置。

let alertController = UIAlertController(title: NSLocalizedString("Enter your title here", comment: ""), message: NSLocalizedString("Enter your message here.", comment: ""), preferredStyle: .Alert) 

let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel, handler: nil) 
let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .Default) { (UIAlertAction) in 
    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!) 
} 

alertController.addAction(cancelAction) 
alertController.addAction(settingsAction) 
self.presentViewController(alertController, animated: true, completion: nil) 
5

這是由Markus和bjc提供的代碼的swift 3實現。

let alertController = UIAlertController(title: NSLocalizedString("Enter your title here", comment: ""), message: NSLocalizedString("Enter your message here.", comment: ""), preferredStyle: .alert) 

let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil) 
let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .default) { (UIAlertAction) in 
       UIApplication.shared.openURL(NSURL(string: UIApplicationOpenSettingsURLString)! as URL) 
      } 

alertController.addAction(cancelAction) 
alertController.addAction(settingsAction) 
      self.present(alertController, animated: true, completion: nil) 
1

在Swift 4中,它的語法有一個更新。

斯威夫特4

extension UIAlertController { 

    func createSettingsAlertController(title: String, message: String) -> UIAlertController { 

     let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 

     let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil) 
     let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .default) { (UIAlertAction) in 
     UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)! as URL, options: [:], completionHandler: nil) 
     } 

     alertController.addAction(cancelAction) 
     alertController.addAction(settingsAction) 
     self.present(alertController, animated: true, completion: nil) 

    } 
} 
相關問題