2017-06-28 1 views
0

新しい通知画面を表示する前に、自分のアプリでローカル通知を使用しています。まず、承認ステータスを確認します。ローカル通知の承認ステータスの確認

ここ
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { 
    if identifier == "AddReminder" { 
     // Check to see if notifications are authorised for this app by the user 
     let isAuthorised = NotificationsManager.checkAuthorization() 
     if isAuthorised { 
      print(isAuthorised) 
      return true 
     } 
     else { 
      let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert) 
      let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { 
       (action: UIAlertAction) in 
      } 

      let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in 
       guard let settingsURL = URL(string: UIApplicationOpenSettingsURLString) else { 
        return 
       } 
       if UIApplication.shared.canOpenURL(settingsURL) { 
        UIApplication.shared.open(settingsURL, options: [:], completionHandler: { (success) in 
         print("Settings opened: \(success)") 
        }) 
       } 
      } 
      alert.addAction(cancelAction) 
      alert.addAction(settingsAction) 

      present(alert, animated: true, completion: nil) 

      print(isAuthorised) 
      return false 
     } 
    } 

    // By default, transition 
    return true 
} 

:通知は、ユーザーが許可されていない場合は、ユーザーが設定し、保存したシーンは、新たな通知が提示されないように、>ブールの方法 - 私はshouldPerformSegue(識別子:,差出人:)を使用しています私は、認可チェックに使用する方法は次のとおりです。

static func checkAuthorization() -> Bool { 
    // var isAuthorised: Bool 
    var isAuthorised = true 
    UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in 
     switch notificationSettings.authorizationStatus { 
     case .notDetermined: 
      self.requestAuthorization(completionHandler: { (success) in 
      guard success else { return } 

     }) 
      print("Reached .notDetermined stage") 
     case .authorized: 
      isAuthorised = true 
     case .denied: 
      isAuthorised = false 
     } 

    } 

    //print("Last statement reached before the check itself") 
    return isAuthorised 
} 

I)は上記の関数の最後の文は、(のisAuthorizedを返す)(UNUserNotificationCenter.currentの体の前に戻ったことを考え出しgetNotificationSettingsは{}が実行され、したがって、それは常に。 isAuthorizedが設定されているものを、メソッドの最初の段階で返します。

質問:私のやり方はうまくいかないので、私はより良い方法で認可を確認する方法を提案してください。

これは私の最初のIOSアプリケーションであるため、私はIOS開発に比較的新しいです。どんな助けでも大歓迎です。

+0

APIが変更されているため、対象のiOSバージョンを指定していません – nathan

+0

悪い、iOS 10、UserNotificationsが10 – Gideon

答えて

0

同様の問題を抱えている人は、getNotificationsSettings()メソッドを使用する代わりに、囲みメソッドが返された後に計算されます。私たちは別のアプローチ、すなわち、私たちのアプリの通知設定であるcurrentUserNotificationSettingsを取得することができます。現在の設定に.aler、.soundなどが含まれているかどうかを確認します。答えがYESの場合、アプリケーションで通知が有効になっていることを確認できます。

関連する問題