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

Posted

技术标签:

【中文标题】iOS 通知和位置服务权限在启动时显示多次【英文标题】:iOS Notifications and location services permissions displaying multiple times at launch 【发布时间】:2018-03-29 06:27:56 【问题描述】:

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")
    

【问题讨论】:

我看到 didFinishLaunchingWithOptions 不完整。你能分享 didFinishLaunchingWithOptions 的完整代码吗? 【参考方案1】:

此问题是由于您要求两次权限请求而发生的。所以你需要改变你的代码如下。

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

只需像这样更新您的代码。这将首先询问您通知授权请求。基于用户与弹出窗口的交互,然后请求您的位置许可请求。

我希望这对你有用。

【讨论】:

【参考方案2】:
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")


    
    


【讨论】:

以上是关于iOS 通知和位置服务权限在启动时显示多次的主要内容,如果未能解决你的问题,请参考以下文章

当视图在iOS中加载时如何在缩放时显示用户当前位置?

如何实现消息传递通知iOS

当用户打开iOS 7及更高版本的位置服务时收到通知

Android通知会多次打开,而不是只打开一次

如何在 Flutter 中获取当前位置时显示加载微调器?

今日 iOS 小部件仅在调试时显示