我想要从一个单独的viewController向用户请求权限(远程和本地通知,摄像头和音频)。视频控制器将在入职过程中呈现,而不再一次。如何在iOS 10中请求远程权限/ Swift 3
我该如何要求权限在AppDelegate之外显示远程通知? 我的问题不是如何触发它们,而是如何确保AppDelegate在授予权限后“负责”处理它们。在AppDelegate中
import UIKit
import UserNotifications
import PushKit
import CallKit
AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, UNUserNotificationCenterDelegate {
let pushRegistry = PKPushRegistry(queue: DispatchQueue.main)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
pushRegistry.delegate = self
pushRegistry.desiredPushTypes = [.voIP]
....
}
视图控制器,我要求允许
import UIKit
import UserNotifications
class AskForNotificationPermissionVC: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
requestPermissions { (videoGranted, audioGranted, notificationsGranted) in
if videoGranted && audioGranted && notificationsGranted {
DispatchQueue.main.async{
self.performSegue(withIdentifier: "toPaySegue", sender: self)
}
} else {
DispatchQueue.main.async{
self.presentSpecialPopoup()
}
}
}
}
func requestPermissions (completion: @escaping ((Bool, Bool, Bool)->())) {
let center = UNUserNotificationCenter.current()
center.delegate = UIApplication.shared.delegate as! UNUserNotificationCenterDelegate?
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { (audioGranted: Bool) -> Void in
center.requestAuthorization(options: [.alert, .badge, .sound]) { (notificationGranted:Bool, error) -> Void in
UIApplication.shared.registerForRemoteNotifications()
completion(videoGranted, audioGranted, notificationGranted)
}
})
})
}
}
任何帮助
当前的代码是非常,非常感谢!谢谢。
佣工似乎很困惑。请描述更详细的问题。使用你的代码实际发生了什么?你想达到什么目的? – shallowThought