- (IBAction)addContact:(ID)sender { ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init]; ABAddressBookRef addressBook = [peoplePicker addressBook]; // create person record ABRecordRef person = ABPersonCreate(); // set name and other string values Nsstring *organization = @"American Business Center"; Nsstring *personEmail = @"leasing@americanbusinesscenter.com"; Nsstring *phoneNo = @"(727)536-6379"; Nsstring *webURL = @"www.AmericanBusinessCenter.com"; Nsstring *addressOne = @"8340 ulmerton Road"; Nsstring *addresstwo = @"Suite 202"; Nsstring *cityname =@ "Largo"; Nsstring *statename = @"FlorIDa"; Nsstring *pinCode = @"34209"; Nsstring *country = @"United States"; CFErrorRef cfError=nil; ABRecordSetValue(person,kABPersonorganizationProperty,(__brIDge CFStringRef)organization,NulL); if (webURL) { ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(urlMultiValue,(__brIDge CFStringRef) webURL,kABPersonHomePageLabel,NulL); ABRecordSetValue(person,kABPersonURLProperty,urlMultiValue,nil); CFRelease(urlMultiValue); } if (personEmail) { ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(emailMultiValue,(__brIDge CFStringRef) personEmail,kABWorkLabel,kABPersonEmailProperty,emailMultiValue,nil); CFRelease(emailMultiValue); } if (phoneNo) { ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType); NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "]; for (Nsstring *venuePhoneNumberString in venuePhoneNumbers) ABMultiValueAddValueAndLabel(phoneNumberMultiValue,(__brIDge CFStringRef) venuePhoneNumberString,kABPersonPhoneMainLabel,kABPersonPhoneProperty,phoneNumberMultiValue,nil); CFRelease(phoneNumberMultiValue); } // add address ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultIDictionaryPropertyType); NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init]; if (addressOne) { if (addresstwo) addressDictionary[(Nsstring *) kABPersonAddressstreetKey] = [Nsstring stringWithFormat:@"%@\n%@",addressOne,addresstwo]; else addressDictionary[(Nsstring *) kABPersonAddressstreetKey] = addressOne; } if (cityname) addressDictionary[(Nsstring *)kABPersonAddressCityKey] = cityname; if (statename) addressDictionary[(Nsstring *)kABPersonAddressstateKey] = statename; if (pinCode) addressDictionary[(Nsstring *)kABPersonAddressZIPKey] = pinCode; if (country) addressDictionary[(Nsstring *)kABPersonAddressCountryKey] = country; ABMultiValueAddValueAndLabel(multiAddress,(__brIDge CFDictionaryRef) addressDictionary,NulL); ABRecordSetValue(person,kABPersonAddressproperty,multiAddress,NulL); CFRelease(multiAddress); //Add person Object to addressbook Object. ABAddressBookAddRecord(addressBook,person,&cfError); if (ABAddressBookSave(addressBook,nil)) { NSLog(@"\nPerson Saved successfuly"); } else { NSLog(@"\n Error Saving person to AddressBook"); }}
编辑
根据LML的答案我做了这个:
- (IBAction)addContact:(ID)sender { ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init]; ABAddressBookRef addressBook = [peoplePicker addressBook]; // create person record ABRecordRef person = ABPersonCreate(); // set name and other string values Nsstring *organization = @"American Business Center"; Nsstring *personEmail = @"leasing@americanbusinesscenter.com"; Nsstring *phoneNo = @"(727)536-6379"; Nsstring *webURL = @"www.AmericanBusinessCenter.com"; Nsstring *addressOne = @"8340 ulmerton Road"; Nsstring *addresstwo = @"Suite 202"; Nsstring *cityname =@ "Largo"; Nsstring *statename = @"FlorIDa"; Nsstring *pinCode = @"34209"; Nsstring *country = @"United States"; CFErrorRef cfError=nil; ABRecordSetValue(person,NulL); CFRelease(multiAddress); if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBook,^(bool granted,CFErrorRef error) { // First time access has been granted,add the contact if(error == NulL){ NSLog(@"Success"); ABAddressBookAddRecord(addressBook,&cfError); } }); CFRelease(addressBook); CFRelease(person); } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { // The user has prevIoUsly given access,add the contact ABAddressBookSave(addressBook,&cfError); if(cfError == NulL){ NSLog(@"Success") ; } CFRelease(addressBook); CFRelease(person); } else { NSLog(@"Adding to address book Failed"); }}
现在,当我按下按钮时它会崩溃并且我收到错误:
Passing ‘const CFErrorRef *’ (aka ‘struct __CFError *const *’) to parameter of type ‘CFErrorRef *’ (aka ‘struct __CFError **’) discards qualifIErs
我想我误解了:P
解决方法 弄清楚了!要获得访问联系人的权限,然后制作并显示使用以下内容:
要求许可或通知用户允许联系人访问:
- (IBAction)addContact:(ID)sender { // Request authorization to Address Book ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NulL,NulL); if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBookRef,CFErrorRef error) { if (granted) { [self addContactToAddressBook]; } else { [self customAlert]; } }); } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { [self addContactToAddressBook]; } else { [self customAlert]; }}
接受许可后联系:
- (voID)addContactToAddressBook { CFErrorRef error = NulL; ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init]; ABAddressBookRef addressBook = [peoplePicker addressBook]; // create person record ABRecordRef person = ABPersonCreate(); // set name and other string values Nsstring *organization = @"American Business Center"; Nsstring *personEmail = @"leasing@americanbusinesscenter.com"; Nsstring *phoneNo = @"(727)536-6379"; Nsstring *webURL = @"www.AmericanBusinessCenter.com"; Nsstring *addressOne = @"8340 ulmerton Road"; Nsstring *addresstwo = @"Suite 202"; Nsstring *cityname =@ "Largo"; Nsstring *statename = @"FlorIDa"; Nsstring *pinCode = @"34209"; Nsstring *country = @"United States"; ABRecordSetValue(person,NulL); ABAddressBookAddRecord(addressBook,&error); ABAddressBookSave(addressBook,&error);
然后打开新联系人
ABPersonVIEwController *ctrl = [[ABPersonVIEwController alloc]init]; ctrl.allowsEditing = NO; [ctrl setPersonVIEwDelegate:self]; [ctrl setdisplayedPerson:person]; self.navigationController.navigationbar.tintcolor = [UIcolor whitecolor]; UILabel * TitleVIEw = [[UILabel alloc] initWithFrame:CGRectZero]; TitleVIEw.backgroundcolor = [UIcolor clearcolor]; TitleVIEw.Font = [UIFont boldSystemFontOfSize:18.0]; TitleVIEw.text = @"Info"; TitleVIEw.textcolor = [UIcolor whitecolor]; ctrl.navigationItem.TitleVIEw = TitleVIEw; [TitleVIEw sizetoFit]; [self.navigationController pushVIEwController:ctrl animated:YES];}总结
以上是内存溢出为你收集整理的ios – 如何询问用户访问联系人的权限然后建立和打开联系人?全部内容,希望文章能够帮你解决ios – 如何询问用户访问联系人的权限然后建立和打开联系人?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)