LoginSignup
0
0

More than 5 years have passed since last update.

プッシュ通知許諾設定の結果によってViewを表示する際にボタンテキストが消える問題の対策

Posted at

iOSにて、プッシュ通知許諾設定がOFFの場合に「プッシュ通知をオンにしてね」というダイアログやViewを表示するケースがあると思います。
その際に謎の現象に苦しめられたのでメモとして残しておきます。

問題

プッシュ通知許諾を確認( UNUserNotificationCenter.current().getNotificationSettings )した上で表示するUI上のボタンテキストが表示されない。

問題発生コード

表示するUIにボタンを設定(storyboard)

button.png

Push通知許諾を確認した上でUIを表示

UNUserNotificationCenter.current().getNotificationSettings { settings in
    guard settings.authorizationStatus != .authorized else { return }

    let nextView = UIStoryboard(name: "second", bundle: nil).instantiateInitialViewController()
    self.present(nextView!, animated: true, completion: nil)
}

実行結果

screen.png
※一度ボタン部分をタップすると文字は表示される

対応

presentの処理をMain Threadで実行すると文字部が表示される

UNUserNotificationCenter.current().getNotificationSettings { settings in
    guard settings.authorizationStatus != .authorized else { return }

    DispatchQueue.main.async {
        let nextView = UIStoryboard(name: "second", bundle: nil).instantiateInitialViewController()
        self.present(nextView!, animated: true, completion: nil)
    }
}

おわり

一応目的の動作を満たすことは出来ました。
あまりスマートな対応とは言えないので、何か知見をお持ちの方がいらっしゃればコメントなど頂ければ幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0