2013-06-21 82 views
6

案例:對於當前位置,用戶在應用安裝時選擇了「不允許」,所以有什麼方法可以再次詢問用戶位置並觸發當前位置的本機iPhone警報??iphone再次請求當前位置權限

我看到計算器一些帖子,但上有老,有沒有一種解決方案現在在新的SDK或某人打電話找到了一種方法,

帖子稱: CLLocation ask again for permission

+2

,有一次每次改變已安裝的應用程序 – adali

回答

8

不幸的是,你不能這樣做。你可以做的一件事是提示用戶更改位置設置。

if (![CLLocationManager locationServicesEnabled]) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                 message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
} 
+1

我已經做到了這一點,但客戶端你知道,他們想要的是他們想要的一切: –

+0

不錯:)你應該更好地告訴情況,這對客戶有利。 – manujmv

0

我不認爲有是再次請求位置許可的方式。但是,如果您真的需要用戶位置,那麼您可以顯示警報,指示他們從設置中啓用它。

5

看來接受的答案並不完全正確。如文檔中所述,[CLLocationManager locationServicesEnabled]檢查是否啓用位置服務。

返回一個布爾值,指示是否在設備上啓用位置服務。 用戶可以通過切換常規中的位置服務開關來啓用或禁用「設置」應用程序中的位置服務。 在開始位置更新之前,您應該檢查此方法的返回值,以確定用戶是否爲當前設備啓用了位置服務。位置服務會在用戶第一次嘗試在應用中使用位置相關信息時提示用戶,但不會提示後續嘗試。如果用戶拒絕使用位置服務,並且嘗試啓動位置更新,則位置管理器會向其委託人報告錯誤。

如果你想檢查用戶是否被授予使用他/她的位置的權限,你應該檢查[CLLocationManager authorizationStatus]。如果您的應用的狀態爲kCLAuthorizationStatusDenied,則表示用戶在請求權限時明確拒絕了您的應用。你可以使用它並相應地通知用戶。

5

在ios8中,apple引入了一個UIApplicationOpenSettingsURLString常量,它是設備「設置」視圖的位置。

您可以編寫以下(斯威夫特),以引導用戶設置視圖:

switch CLLocationManager.authorizationStatus() { 
    case .AuthorizedWhenInUse, .Restricted, .Denied: 
     let alertController = UIAlertController(
      title: "Background Location Access Disabled", 
      message: "In order to be notified, please open this app's settings and set location access to 'Always'.", 
      preferredStyle: .Alert) 

     let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 
     alertController.addAction(cancelAction) 

     let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in 
      if let url = NSURL(string:UIApplicationOpenSettingsURLString) { 
       UIApplication.sharedApplication().openURL(url) 
      } 
     } 
     alertController.addAction(openAction) 

     self.presentViewController(alertController, animated: true, completion: nil) 
} 
+0

在添加「打開設置」操作之前,您是否必須檢查SystemVersion? –

0

這是我做過什麼考慮以前的答案: (這是MonoTouch的C#,但容易翻譯到Swift或Obj-C)

以下請求權限,如果授予,則繼續進行位置更新。否則,下次用戶會來;如果位置服務被禁用或否認它會顯示一個消息重定向請求許可/激活這仍然沒有辦法,如果用戶選擇「允許」或「拒絕」的設置

//Location Manager (foreground) 
     CLLocationManager locMgr = new CLLocationManager(); 
     locMgr.PausesLocationUpdatesAutomatically = false; 

     if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) 
     { 
      locMgr.RequestWhenInUseAuthorization(); 
     } 

     if (CLLocationManager.LocationServicesEnabled && CLLocationManager.Status == CLAuthorizationStatus.AuthorizedWhenInUse) 
     { 
      //set the desired accuracy, in meters 
      locMgr.DesiredAccuracy = 150; 
      locMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => 
      { 
       Console.WriteLine(e.Locations); 
      }; 

      locMgr.StartUpdatingLocation(); 
     } 
     else if (!CLLocationManager.LocationServicesEnabled || CLLocationManager.Status == CLAuthorizationStatus.Denied) 
     { 
      var alert = UIAlertController.Create(NSBundle.MainBundle.LocalizedString("Location access", "Location access"), NSBundle.MainBundle.LocalizedString("Please check location", "To show your position on the map, you have to enable location services and authorize the app"), UIAlertControllerStyle.Alert); 

      alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Cancel, null)); 

      if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) 
      { 
       alert.AddAction(UIAlertAction.Create(NSBundle.MainBundle.LocalizedString("Go to settings", "Go to settings"), UIAlertActionStyle.Default, delegate 
       { 

        var url = new NSUrl(UIApplication.OpenSettingsUrlString); 
        UIApplication.SharedApplication.OpenUrl(url); 

       })); 
      } 

      PresentViewController(alert, true, null); 
     } 
相關問題