2016-11-14 72 views
0

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

任何幫助

當前的代碼是非常,非常感謝!謝謝。

+0

傭工似乎很困惑。請描述更詳細的問題。使用你的代碼實際發生了什麼?你想達到什麼目的? – shallowThought

回答

0
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in 
    //handle result 
} 
+0

有人編輯了改變含義的問題。我的問題不是如何觸發警報,而是如何在視圖控制器中觸發警報,同時這樣做是爲了使AppDelegate能夠處理傳入的遠程通知。 – KML

+0

你可以從任何地方調用這個方法,任何你選擇的viewController。 – shallowThought

+0

@FiReTiTi不是一個有效的問題,KML是原始提問者。 – WMios