2016-11-14 10 views
0

別のviewControllerからのアクセス許可(リモート通知とローカル通知、カメラとオーディオ)をユーザーに尋ねたいと思います。ビューコントローラはオンボーディングプロセス中に提示され、再度は表示されません。iOS 10/Swift 3でリモート許可を求める方法

リモート通知をAppDelegateの外部に表示するにはどうすればよいですか? 私の質問は、それらを起動する方法ではなく、AppDelegateが権限を与えられた後にそれらを処理するための "責任"があることを確認する方法です。私は許可

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

どれでも助けを求める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で

現在のコードは非常に、非常に高く評価されます!ありがとうございました。

+0

ヘルパーは混乱しているようです。より詳細な問題を説明してください。あなたのコードを使って実際に何が起こっていますか?そして、あなたは何を達成したいのですか? – shallowThought

答えて

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

誰かが意味を変えた質問を編集しました。私の質問は、アラートをトリガする方法ではなく、ViewControllerでアラートをトリガする方法と同時に、AppDelegateが着信リモート通知を処理できるようにする方法です。 – KML

+0

このメソッドは、どこからでも、任意のviewControllerから呼び出すことができます。 – shallowThought

+0

@FiReTiTi有効な問題ではなく、KMLが元の質問者です。 – WMios

関連する問題