我必须在App中获得许可才能发送和获取一些数据。当我尝试获取数据时,如果未启用权限,则会出现一些错误,因此我尝试在AppDelegate中获取权限。 这是正确的方法吗?。
此外,当请求权限选择时,如果我突然从App退出并再次返回,则权限始终显示弹出窗口“您没有权限”,并且由于从AppDelegate检查控件,我重定向到“设置/权限”。但是“权限”未显示在“设置”中,因为我尚未安装权限。 我该如何处理此权限问题。 哪种方法是最好的?
// AppDelegate
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
let libStatus = GlobalVariables.defaults.object(forKey: "isCreatedAlbum") as? Bool
if libStatus != true {
PermissionsControl().library()
}
}
// PermissionControl库
func library(){
let libraryAuth = PHPhotoLibrary.authorizationStatus()
var topWindow: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
print("Library Authorization Status \(PHPhotoLibrary.authorizationStatus().hashValue)")
if libraryAuth == PHAuthorizationStatus.authorized {
topWindow?.isHidden = false // if you want to hide the topwindow then use this
topWindow = nil // if you want to remove the topwindow then use this
let permissionStatus = GlobalVariables.defaults.object(forKey: "isCreatedAlbum") as? Bool
if permissionStatus != true {
CustomPhotoAlbum().createAlbum()
GlobalVariables.defaults.set(true, forKey: "isCreatedAlbum")
}
} else if libraryAuth == PHAuthorizationStatus.notDetermined || libraryAuth == PHAuthorizationStatus.denied {
topWindow?.rootViewController = UIViewController()
topWindow?.windowLevel = UIWindowLevelAlert + 1
let alert: UIAlertController = UIAlertController(title: "Permission Error", message: "\nPlease give permission for Library!\n", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Settings", style: .default , handler: {(action: UIAlertAction) -> Void in
// continue your work
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true
})
}
}))
topWindow?.makeKeyAndVisible()
topWindow?.rootViewController?.present(alert, animated: true, completion: nil)
}
}