我正在开发一个应用程序。我(在AppDelegate中(做的最重要的事情之一是调用OneSignal的initwithlaunchingoptions(...这会自动使我的应用显示"应用想要发送通知",请求权限。在我的应用生命周期中,我需要用户的其他权限(如日历(。我想显示(在所有权限之前(一个简短的 AlertView,解释我会问什么以及为什么。但是,如果我无法从AppDelegate中移动OneSignal初始化,而我的"解释警报"仅发生在主视图控制器的viewDidLoad中,我该如何完成此操作?
谢谢。
胜利者
下面是一个UIViewController
示例,其中包含有关应用程序需要位置数据的信息,当用户按UIButton
时,它会请求权限。 您可以对所有权限执行相同的操作。
class LocationRequestViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
}
//when user authorised or denied ->push next `UIViewController`
func locationManager(_: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse || status == .denied {
let destinationVC = self.storyboard!.instantiateViewController(withIdentifier: "Notifications Request")
self.navigationController?.pushViewController(destinationVC, animated: true)
}
}
@IBAction func requestLocation(_: UIButton) {
self.locationManager.requestWhenInUseAuthorization()
}
}