2016-11-14 69 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