【问题标题】:iOS Notifications and location services permissions displaying multiple times at launchiOS 通知和位置服务权限在启动时显示多次
【发布时间】: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 的完整代码吗?

标签: ios swift push-notification apple-push-notifications appdelegate


【解决方案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")
    
    
        }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 2016-06-22
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      相关资源
      最近更新 更多