ios 检查用户是否登录到 icloud,如果没有则将用户发送到设置
ios check if user is logged into icloud and if not send user to settings
我检查用户是否登录到 iCloud,如果没有我设置警报。但是我想将用户发送到适当的设置,用户可以在其中实际登录 iCloud,并且一旦登录就完全重定向到当前视图控制器。
如何修改此代码?
var iCloudStatus:Bool = false
func ifUserLoggedinToICloud() -> Bool {
let alertController = UIAlertController(title: "Alert", message: "iCloud Status", preferredStyle: .alert)
defaultContainer.accountStatus(completionHandler: { (accountStatus, error) in
if (accountStatus == .available) {
print("iCloud is available")
iCloudStatus = true
}
else {
print("iCloud is not available, log into iCloud")
alertController.message = "iCloud not available, log into iCloud"
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
})
return iCloudStatus
}
帐户状态检查是异步的,因此正如 Vadian 评论的那样,在 ifUserLoggedInToICloud
更改之前,函数将始终 return false。您应该 运行 完成闭包中的代码。
您可以通过以下方式引导用户进行设置:
defaultContainer.accountStatus(completionHandler: { (accountStatus, error) in
switch accountStatus {
case .noAccount, .restricted:
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else { return }
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
// Completion handler when settings opened
})
case .available:
print("Account available")
case .couldNotDerermine:
print("Error")
}
})
您不能将用户从设置页面重定向回来。是否导航回您的应用取决于用户。
有私有 API 可以重定向到特定的 iOS 设置页面(例如 "App-Prefs:root=CASTLE"
URL),但 Apple 不允许使用这些。
我检查用户是否登录到 iCloud,如果没有我设置警报。但是我想将用户发送到适当的设置,用户可以在其中实际登录 iCloud,并且一旦登录就完全重定向到当前视图控制器。
如何修改此代码?
var iCloudStatus:Bool = false
func ifUserLoggedinToICloud() -> Bool {
let alertController = UIAlertController(title: "Alert", message: "iCloud Status", preferredStyle: .alert)
defaultContainer.accountStatus(completionHandler: { (accountStatus, error) in
if (accountStatus == .available) {
print("iCloud is available")
iCloudStatus = true
}
else {
print("iCloud is not available, log into iCloud")
alertController.message = "iCloud not available, log into iCloud"
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
})
return iCloudStatus
}
帐户状态检查是异步的,因此正如 Vadian 评论的那样,在 ifUserLoggedInToICloud
更改之前,函数将始终 return false。您应该 运行 完成闭包中的代码。
您可以通过以下方式引导用户进行设置:
defaultContainer.accountStatus(completionHandler: { (accountStatus, error) in
switch accountStatus {
case .noAccount, .restricted:
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else { return }
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
// Completion handler when settings opened
})
case .available:
print("Account available")
case .couldNotDerermine:
print("Error")
}
})
您不能将用户从设置页面重定向回来。是否导航回您的应用取决于用户。
有私有 API 可以重定向到特定的 iOS 设置页面(例如 "App-Prefs:root=CASTLE"
URL),但 Apple 不允许使用这些。