【问题标题】:iphone ask for Current location permission againiphone再次请求当前位置权限
【发布时间】:2013-06-21 06:36:41
【问题描述】:

案例:对于当前位置,用户在应用安装时选择了“不允许”,那么有没有一种方法可以让我再次询问用户位置并触发当前位置的原生 iphone 警报??

我在stackoverflow上看到了一些帖子,但是有旧的,现在有没有解决方案可以调用新的sdk或有人找到了方法,

帖子引用: CLLocation ask again for permission

【问题讨论】:

  • 如果用户选择“允许”或“拒绝”仍然没有办法,每个安装的应用只有一次更改

标签: iphone ios ios6 cllocationmanager


【解决方案1】:

很遗憾,您不能这样做。您可以做的一件事是提示用户更改位置设置。

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

【讨论】:

  • 我已经这样做了,但是你知道的客户,想要他们心中的一切:|
  • nice :) 更好的是你应该告诉情况,这对客户有好处。
【解决方案2】:

在 ios8 中,苹果引入了一个 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)
}

【讨论】:

  • 在添加“打开设置”操作之前,您不需要检查 SystemVersion 吗?
【解决方案3】:

似乎接受的答案并不完全正确。 [CLLocationManager locationServicesEnabled] 检查是否启用了位置服务,如文档中所述。

返回一个布尔值,指示设备上是否启用了位置服务。 用户可以通过在一般情况下切换定位服务开关来从设置应用程序启用或禁用定位服务。 您应该在开始位置更新之前检查此方法的返回值,以确定用户是否为当前设备启用了位置服务。位置服务会在用户第一次尝试在应用程序中使用与位置相关的信息时提示用户,但不会提示后续尝试。如果用户拒绝使用位置服务,而您仍然尝试启动位置更新,则位置管理器会向其代理报告错误。

如果你想检查用户是否被允许使用他/她的位置,你应该检查[CLLocationManager authorizationStatus]。如果您的应用的状态为kCLAuthorizationStatusDenied,则表示用户在请求权限时明确拒绝了您的应用。您可以使用它并相应地通知用户。

【讨论】:

    【解决方案4】:

    我认为没有办法再次请求位置许可。但是,如果您确实需要用户位置,那么您可以显示警报,指示他们从设置中启用它。

    【讨论】:

      【解决方案5】:

      考虑到以前的答案,这就是我所做的: (这是在 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);
              }
      

      【讨论】:

        猜你喜欢
        • 2017-09-13
        • 2017-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-11
        相关资源
        最近更新 更多