菜鸟教程小白 发表于 2022-12-11 19:03:01

iOS 不可靠位置权限警报


                                            <p><p>在我们的应用中,我们要求在一个用于显示 map 的 View 上获得位置许可 (WhenInUse)。</p>

<p>如果用户选择禁用设备定位服务(即在设备设置中全局禁用)然后在应用中打开我们的 View ,将显示定位权限弹出窗口。重复冲洗几次(重新打开服务、继续应用、离开应用、关闭服务等),几次后位置权限警报将停止显示。</p>

<p>有人知道这是否是 iOS 中的错误(发生在 iOS 10 上)?</p>

<p>我们可以使用自己的警报来显示何时</p>

<pre><code>CLLocationManager locationServicesEnabled = NO
</code></pre>

<p>但由于我们无法控制是否/何时弹出 iOS 位置警报,有时它们会同时显示,这是不好的用户体验。</p>

<p>该问题的任何已知解决方案?如果这是 iOS 中的错误,我必须向我们的 QA 和经理解释。</p>

<p>编辑:</p>

<pre><code>- (BOOL)negotiateLocationServicePermission:(UIViewController *)context
{
    /* Device location service is enabled. */
    if ()
    {
      /* App location service is already authorized. */
      if ( == kCLAuthorizationStatusAuthorizedWhenInUse
            || == kCLAuthorizationStatusAuthorizedAlways)
      {
            /* App location service is authorized. Start location updates ... */
            ;
            return YES;
      }
      else
      {
            /* App location service not yet authorized and status is not determined (aka: first time asking for permission). */
            if ( == kCLAuthorizationStatusNotDetermined)
            {
                /* Request the location permission from the user. */
                if ()
                  ; /* iOS 8+ */
                else
                  ; /* iOS 7 */
                return YES;
            }
            /* App location service not authorized and previously denied. */
            else
            {
                /* App location service permission was denied before. */
                // Show custom alert!
                return NO;
            }
      }
    }
    /* Device location service is disabled. */
    else
    {
      // Show custom alert!
      return NO;
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我不认为这是一个错误,这是不同的 AlertView。</p>

<p>如果用户接受一次位置权限,它会被保存,它不会再次询问他。但如果定位服务被禁用,情况就不同了。</p>

<p>你可以这样实现它:</p>

<pre><code>if (){

    NSLog(@&#34;Location Services Enabled&#34;);

    if (==kCLAuthorizationStatusDenied){
      alert = [ initWithTitle:@&#34;App Permission Denied&#34;   
                                           message:@&#34;To re-enable, please go to Settings and turn on Location Service for this app.&#34;
                                          delegate:nil
                                 cancelButtonTitle:@&#34;OK&#34;
                                 otherButtonTitles:nil];
      ;
    }
}
</code></pre>

<p>因此,如果用户从设置中禁用定位服务,警报 View 可以重定向到设置页面。</p>

<p>这样就不会显示多个 AlertView。这仅取决于您希望如何处理每种情况,例如:</p>

<ul>
<li>在“设置”中启用了定位服务,但此应用的权限被拒绝</li>
<li>在设置中启用定位服务并获得授权</li>
<li>在“设置”中禁用了定位服务</li>
</ul>

<p>确保您处理每个案例并进行测试。</p>

<p>不知道我是否准确回答了您的问题,希望对您的实现有所帮助。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 不可靠位置权限警报,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42386047/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42386047/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 不可靠位置权限警报