我想要從一個單獨的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