我们不能再次请求许可。苹果通常会限制这种行为,如果使用失控,可能会让用户非常恼火。
但是,如果您的应用程序确实需要这样做,那么解决方案是将用户导航到设置,指示他们为应用程序打开授权:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
checkTrackingAuthorization(ATTrackingManager.trackingAuthorizationStatus)
}
private func checkTrackingAuthorization(_ status: ATTrackingManager.AuthorizationStatus) {
switch status {
case .authorized: break
// Access is grantted
case .notDetermined: requestTrackingAccess()
// The permission was not asked before
case .denied, .restricted: displayTrackingAccessAlert()
default: break
// Unexpected status (there may be additional unknown values added in the future)
}
}
private func requestTrackingAccess() {
ATTrackingManager.requestTrackingAuthorization { [weak self] status in
self?.checkTrackingAuthorization(status)
}
}
private func displayTrackingAccessAlert() {
let alert = UIAlertController(title: "Tracking access is required", message: "Please turn on access to tracking on the settings", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { action in
// Open the Settings app
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alert.addAction(settingsAction)
alert.addAction(cancelAction)
alert.preferredAction = settingsAction
present(alert, animated: true, completion: nil)
}