【问题标题】:ios Pop-up ask for location permission display non stop until I turn of my iPhoneios弹出询问位置权限显示不停,直到我打开我的iPhone
【发布时间】:2026-02-14 22:55:02
【问题描述】:

在将我的应用程序从 iOS 11 的 32 位转换为 64 位时遇到问题。 在第一次安装时,它不会请求获取位置的权限。 然后我的应用程序无法获得许可。搜索后,我添加了键“NSLocationAlwaysAndWhenInUseUsageDescription”然后它起作用了。

但是当构建发布应用程序时,我尝试安装然后再次确认得到一个问题,即弹出窗口多次显示,就像它在循环中一样。这种情况不会出现在模拟器上。详情可关注Video

我在调试时检查了同一台设备,它只显示一次,但使用存档 IPA 文件会出现此问题。

Info.plist

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

调用startUpdatingLocation方法。

(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
    case kCLAuthorizationStatusAuthorizedAlways:
    case kCLAuthorizationStatusAuthorizedWhenInUse:
        [locationManager startUpdatingLocation];
        break;
     default:
        break;
}

}

方法 didUpdateToLocation

(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//NSLog(@"Location:%@", newLocation);
[location release];
[locationDate release];
location = [newLocation retain];
locationDate = [[NSDate date] retain];
[locationDelegate locationSucceed:self];

if (isOneStop) {
    [self performSelector:@selector(locationRelease) withObject:nil afterDelay:0.1f];
}

}

源代码调用“requestAlwaysAuthorization”。

- (void)startLocation:(NSObject<EpLocationDelegate>*)delegate {
      NSLog(@"startLocation");
      [locationManager stopUpdatingLocation];

      [locationDelegate release];
       locationDelegate = [delegate retain];


       locationManager = [[CLLocationManager alloc] init];

       locationManager.delegate = self;
       locationManager.distanceFilter = kCLDistanceFilterNone;
       locationManager.desiredAccuracy = kCLLocationAccuracyBest;

       float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
       if (osVersion >= 8.0) {
           [locationManager requestAlwaysAuthorization];
      } else {
    [locationManager startUpdatingLocation];
 }

}

并且startLocation 调用了函数“viewWillAppear”

更新

我尝试通过viewDidLoad 致电startLocation 仍然无法解决我的问题。

【问题讨论】:

  • 请出示您拨打requestAlwaysAuthorization的代码
  • @Paulw11 感谢您的回复。我已经更新了我的问题。 requestAlwaysAuthorization 已调用 viewWillAppear 方法。
  • viewWillAppear 是一个不好的地方。我建议viewDidLoad。另外,locationManager 是如何/在哪里声明和分配的?
  • @Paulw11 我只是更新了在我的问题上声明和分配的完整 locationManager。所有这些都在一个方法名称startLocation 中,然后我在viewWillAppear 中调用它。所以现在我必须将startLocation 移动到viewDidLoad。对吗?

标签: ios objective-c popup cllocationmanager ios11


【解决方案1】:

在你的 info.plist 文件中添加:

<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
Now in your swift file, don't forget to add delegate: CLLocationManagerDelegate

In your viewDiDLoad(), add this:

locationManager = CLLocationManager()

locationManager.delegate = self

locationManager.requestWhenInUseAuthorization()

locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.startUpdatingLocation()

locationManager.startMonitoringSignificantLocationChanges()

// Here you can check whether you have allowed the permission or not.

if CLLocationManager.locationServicesEnabled()
    {
        switch(CLLocationManager.authorizationStatus())
        {

        case .authorizedAlways, .authorizedWhenInUse:

            print("Authorize.")

            break

        case .notDetermined:

            print("Not determined.")

            break

        case .restricted:

            print("Restricted.")

            break

        case .denied:

            print("Denied.")
        }
    }

【讨论】: