如何在iOS 10/Swift 3中请求远程许可 [英] How to ask for remote permission in iOS 10/Swift 3

查看:60
本文介绍了如何在iOS 10/Swift 3中请求远程许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从单独的viewController询问用户权限(远程和本地通知,摄像头和音频).该视图控制器将在入职过程中展示,并且再也不会展示.

I want to ask the user for permissions (remote and local notifications, camera and audio) from a separate viewController. The viewcontroller will be presented during the onboarding process, and never again.

我如何请求在AppDelegate外部显示远程通知的权限?我的问题不是如何触发它们,而是一旦授予许可,如何确保AppDelegate对处理它们负责".

How can I ask for permissions to display remote notifications outside the AppDelegate? My question is not how to trigger them, but how to make sure that the AppDelegate is "responsible" for handling them once a permission have been granted.

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]


    ....

}

我要求许可的ViewController

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)
                }
            })
        })
    }
}

任何帮助都是非常非常感谢的!!谢谢.

Any help is very, very much appreciated !! Thank you.

推荐答案

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

这篇关于如何在iOS 10/Swift 3中请求远程许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆