iOSにて、プッシュ通知許諾設定がOFFの場合に「プッシュ通知をオンにしてね」というダイアログやViewを表示するケースがあると思います。
その際に謎の現象に苦しめられたのでメモとして残しておきます。
問題
プッシュ通知許諾を確認( UNUserNotificationCenter.current().getNotificationSettings
)した上で表示するUI上のボタンテキストが表示されない。
問題発生コード
表示するUIにボタンを設定(storyboard)
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)
}
実行結果
対応
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)
}
}
おわり
一応目的の動作を満たすことは出来ました。
あまりスマートな対応とは言えないので、何か知見をお持ちの方がいらっしゃればコメントなど頂ければ幸いです。