【发布时间】: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