目前,我能够成功地要求用户允许他们访问他们的联系信息。我正在通过这样的 switch 语句来处理这个问题:
func requestContactPermissions() {
let store = CNContactStore()
var authStatus = CNContactStore.authorizationStatus(for: .contacts)
switch authStatus {
case .restricted:
print("User cannot grant permission, e.g. parental controls in force.")
exit(1)
case .denied:
print("User has explicitly denied permission.")
print("They have to grant it via Preferences app if they change their mind.")
exit(1)
case .notDetermined:
print("You need to request authorization via the API now.")
store.requestAccess(for: .contacts) { success, error in
if let error = error {
print("Not authorized to access contacts. Error = \(String(describing: error))")
exit(1)
}
if success {
print("Access granted")
}
}
case .authorized:
print("You are already authorized.")
@unknown default:
print("unknown case")
}
}
在这种.notDetermined
情况下,这是打开对话框,我可以单击no
或yes
,授予或拒绝应用程序访问权限。这是好的和预期的。
我想要做的是,如果用户点击改变视图yes
。现在,我有requestContactPermissions
一个按钮内的功能,如下所示:
Button(action: {
withAnimation {
// TODO: Screen should not change until access is successfully given.
requestContactPermissions()
// This is where the view change is occurring.
self.loginSignupScreen = .findFriendsResults
}
})
一旦用户授予应用程序对其联系人的访问权限,我如何添加逻辑以更改视图?