在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")
}
}
答案 0 :(得分:1)
由于您要求两个权限请求,因此发生此问题。因此,您需要将代码更改为以下内容。
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (authorised, error) in
self.setupLocationService()
})
只需像这样更新您的代码。这将首先询问您通知授权请求。根据用户与弹出窗口的交互,然后询问您的位置许可请求。
我希望这对你有用。
答案 1 :(得分:0)
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")
}
}
}