繁体   English   中英

iOS通知和位置服务权限在启动时显示多次

[英]iOS Notifications and location services permissions displaying multiple times at launch

在iOS 10以上版本中,当我启动应用程序时,请求通知和位置服务都会触发两次。 第一个短暂出现并立即消失而没有允许我做任何动作,然后我得到第二个具有正常行为的弹出窗口,等待用户“允许”或“拒绝”。

这是问题的gif

这是我的AppDelegate中的通知方法:

 func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    if #available(iOS 10, *) { // iOS 10 support
        UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
        UIApplication.shared.registerForRemoteNotifications()
    }else if #available(iOS 9, *) { // iOS 9 support
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
    AppEventsLogger.activate(application)

}

这是我的AppDelegate中的定位服务方法:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    //
    self.setupLocationService()


 func setupLocationService(){
    self.locationManager.delegate = self
    let status = CLLocationManager.authorizationStatus()
    if status == .authorizedWhenInUse || status == .authorizedAlways{
        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
        self.locationManager.startUpdatingLocation()
    }else if status == .notDetermined{
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.requestAlwaysAuthorization()
    }else{
        print("Location service disabled")
    }
}

由于您要求两个权限请求,因此发生了此问题。 因此,您需要按以下方式更改代码。

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (authorised, error) in
    self.setupLocationService()
})

像这样更新您的代码。 这将首先询问您通知授权请求。 基于用户与弹出窗口的交互,此后会要求您提供位置权限请求。

希望这对您有用。

Write notification code in didFinishLaunchingWithOptions

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,CLLocationManagerDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    setupLocationService()

    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()

    return true
}

func setupLocationService(){
    self.locationManager.delegate = self
    let status = CLLocationManager.authorizationStatus()
    if status == .authorizedWhenInUse || status == .authorizedAlways{
        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
        self.locationManager.startUpdatingLocation()
    }else if status == .notDetermined{
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.requestAlwaysAuthorization()
    }else{
        print("Location service disabled")


    }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM