授予联系人权限后更改视图
Change view once contact permission is granted
目前,我能够成功请求用户允许访问他们的联系信息。我正在通过这样的 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
}
})
一旦用户授予应用程序对其联系人的访问权限,我如何添加逻辑以更改视图?
向 requestContactPermissions
函数添加一个补全,如下所示(我删除了与答案无关的部分):
func requestContactPermissions(completion: @escaping (Bool) -> ()) {
let store = CNContactStore()
var authStatus = CNContactStore.authorizationStatus(for: .contacts)
switch authStatus {
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)
//call completion for failure
completion(false)
}
if success {
//call completion for success
completion(true)
print("Access granted")
}
}
}
}
然后你可以在闭包中判断用户是否授权:
Button(action: {
withAnimation {
// TODO: Screen should not change until access is successfully given.
requestContactPermissions { didGrantPermission in
if didGrantPermission {
//this is the part where you know if the user granted permission:
// This is where the view change is occurring.
self.loginSignupScreen = .findFriendsResults
}
}
}
})
目前,我能够成功请求用户允许访问他们的联系信息。我正在通过这样的 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
}
})
一旦用户授予应用程序对其联系人的访问权限,我如何添加逻辑以更改视图?
向 requestContactPermissions
函数添加一个补全,如下所示(我删除了与答案无关的部分):
func requestContactPermissions(completion: @escaping (Bool) -> ()) {
let store = CNContactStore()
var authStatus = CNContactStore.authorizationStatus(for: .contacts)
switch authStatus {
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)
//call completion for failure
completion(false)
}
if success {
//call completion for success
completion(true)
print("Access granted")
}
}
}
}
然后你可以在闭包中判断用户是否授权:
Button(action: {
withAnimation {
// TODO: Screen should not change until access is successfully given.
requestContactPermissions { didGrantPermission in
if didGrantPermission {
//this is the part where you know if the user granted permission:
// This is where the view change is occurring.
self.loginSignupScreen = .findFriendsResults
}
}
}
})