我试图通过直接使用此代码,而不要求任何权限保存联系人的联系人应用程序:
import Foundation
import UIKit
import Contacts
@available(iOS 9.0, *)
class OnCallEmpContact: UITableViewController {
var store: CNContactStore!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var phoneLabel: UILabel!
@IBOutlet weak var emailLabel: UILabel!
@IBOutlet weak var workPhoneLabel: UILabel!
var emp = employee()
var rank = ""
override func viewDidLoad() {
super.viewDidLoad()
self.nameLabel.text=self.emp.getFistName()+" "+emp.getLastName()
self.phoneLabel.text="0"+self.emp.getMobile()
self.emailLabel.text=self.emp.getEmail()
self.workPhoneLabel.text=self.emp.getPhone()
store = CNContactStore()
checkContactsAccess()
}
private func checkContactsAccess() {
switch CNContactStore.authorizationStatusForEntityType(.Contacts) {
// Update our UI if the user has granted access to their Contacts
case .Authorized:
self.accessGrantedForContacts()
// Prompt the user for access to Contacts if there is no definitive answer
case .NotDetermined :
self.requestContactsAccess()
// Display a message if the user has denied or restricted access to Contacts
case .Denied,
.Restricted:
let alert = UIAlertController(title: "Privacy Warning!",
message: "Permission was not granted for Contacts.",
preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
private func requestContactsAccess() {
store.requestAccessForEntityType(.Contacts) {granted, error in
if granted {
dispatch_async(dispatch_get_main_queue()) {
self.accessGrantedForContacts()
return
}
}
}
}
// This method is called when the user has granted access to their address book data.
private func accessGrantedForContacts() {
//Update UI for grated state.
//...
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0{
if rank == "0"{
return "Primary Employee"
}
else if rank == "1"{
return "Backup Employee"
}
}
return""
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true) // to stop highliting the selected cell
if indexPath.section==1 && indexPath.row==0{
self.saveContact()
}
}
func saveContact(){
do{
let contact = CNMutableContact()
contact.givenName = self.emp.getFistName()
contact.familyName = self.emp.getLastName()
contact.phoneNumbers = [CNLabeledValue(
label:CNLabelPhoneNumberiPhone,
value:CNPhoneNumber(stringValue:emp.getMobile())),
CNLabeledValue(
label:CNLabelPhoneNumberiPhone,
value:CNPhoneNumber(stringValue:emp.getPhone()))]
let workEmail = CNLabeledValue(label:CNLabelWork, value:emp.getEmail())
contact.emailAddresses = [workEmail]
let saveRequest = CNSaveRequest()
saveRequest.addContact(contact, toContainerWithIdentifier:nil)
try store.executeSaveRequest(saveRequest)
print("saved")
}
catch{
print("error")
}
}
}
我的方法try store.executeSaveRequest(saveRequest)
的最后一行saveContact
得到错误
错误:
致命错误:“试试吧!”表达意外上调的错误:错误域= CNErrorDomain代码= 100的UserInfo = {NSLocalizedDescription =拒绝访问,NSLocalizedFailureReason =此应用程序还没有被授权访问联系人“访问被拒绝”。
我发现在苹果公司的网站上面的代码,而且我冲这在中间隐私权部分:
到CNContactStore任何调用将阻止该应用程序,而用户被要求授予或拒绝访问
但没有出现要求访问通讯录应用..我应该写更多的代码来做到这一点?怎么样 ?
这是雨燕最新的语法。希望能帮助到你 !
let store = CNContactStore()
if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined {
store.requestAccess(for: .contacts) {granted, error in
if granted {
print("PERMISSION GRANTED")
} else {
print("PERMISSION NOT GRANTED")
}
}
} else if CNContactStore.authorizationStatus(for: .contacts) == .authorized {
// If the user user has earlier provided the access, then add the contact
print("AUTHORIZED")
findContactsOnBackgroundThread { (contact) in
print("contacts : ",contact as Any)
}
} else {
// If the user user has NOT earlier provided the access, create an alert to tell the user to go to Settings app and allow access
print("NOT AUTHORIZED")
}