【问题标题】:How to add contact from within an iPhone app?如何从 iPhone 应用程序中添加联系人?
【发布时间】:2011-01-24 14:26:39
【问题描述】:

我希望在我的 iPhone 应用程序中跟踪人员 - 从现有联系人数据中添加他们,或者提示用户输入将保存到他们的联系人中的新联系人。

我知道我可以创建人员记录并将其写入通讯录,是否可以显示此屏幕
还是我必须实现自己的视图以方便创建联系人条目?

【问题讨论】:

    标签: iphone iphone-sdk-3.0 addressbook contacts


    【解决方案1】:

    以下代码可用于所有 IOS 版本, #import ,#import

    -(void)addContact
    {
    
    ABPeoplePickerNavigationController *peoplePicker;
    ABAddressBookRef addressBook;
    
    
    peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    addressBook = [peoplePicker addressBook];
    if(!IOS_OLDER_THAN_6)
    {
        addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
        {
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
                                                     {
                                                         if (granted)
                                                         {
                                                             if (![self checkExistsContacts]){
                                                                 [self addThisContact];
    
                                                             }
                                                             else
                                                             {
                                                                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“App " message:@"Your contat is already exists." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                                                 [alert show];
                                                             }
                                                         }
                                                         else
                                                         {
                                                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“App does not access to your contacts" message:@"To enable access go to : iPhone's Settings Privacy  > Contacts > App > set 'On'" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                                             [alert show];
                                                         }
                                                     });
        }
        else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
        {
            // The user has previously given access, add the contact
            if (![self checkExistsContacts]){
                [self addThisContact];
    
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“App " message:@"Your contat is already exists." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
    
        }
        else
        {
            // The user has previously denied access
            // Send an alert telling user to change privacy setting in settings app
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“App does not access to your contacts" message:@"To enable access go to : iPhone's Settings Privacy  > Contacts > App > set 'On'" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }
    else
    {
        if (![self checkExistsContacts]){
            [SVProgressHUD showWithStatus:@"Saving..." maskType:SVProgressHUDMaskTypeClear];
    
            NSString *strCell=@"1-800-123-1234”;
            NSString *strFirstName=@“fname”;
            NSString *strLastName=@“lname”;
            NSUInteger addressbookId = 0;
    
            ABRecordRef aRecord = ABPersonCreate();
            CFErrorRef  anError = NULL;
            ABRecordSetValue(aRecord, kABPersonFirstNameProperty, (__bridge CFTypeRef)(strFirstName), &anError);
            ABRecordSetValue(aRecord, kABPersonLastNameProperty, (__bridge CFTypeRef)(strLastName), &anError);
    
    
            //(@"adding phonee");
            ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    
    
            if(strCell) ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)(strCell), kABPersonPhoneIPhoneLabel,NULL);
    
            CFRelease(multi);
            ABAddressBookRef addressBook1;
            CFErrorRef error = NULL;
            addressBook1 = ABAddressBookCreate();
            ABAddressBookAddRecord (addressBook1, aRecord, &error);
    
            if (error != NULL) {
            }
            error = NULL;
            if(ABAddressBookSave ( addressBook1,  &error)){
                addressbookId =  ABRecordGetRecordID (aRecord);
            }
    
            if (error != NULL) {
            }
    
            CFRelease(aRecord);
            CFRelease(addressBook1);
            [SVProgressHUD dismiss];
    
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“App” message:@"Contact saved successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
    
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“App " message:@"Your contat is already exists." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    
    
    
    }
    }
    
    - (void)addThisContact
    {
    [SVProgressHUD showWithStatus:@"Saving..." maskType:SVProgressHUDMaskTypeClear];
    
         NSString *strCell=@"1-800-123-1234”;
            NSString *strFirstName=@“fname”;
            NSString *strLastName=@“lname”;
    
    ABRecordRef person = ABPersonCreate();
    
    // set name and other string values
    CFErrorRef cfError=nil;
    if (strFirstName) {
        ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(strFirstName) , nil);
    }
    
    if (strLastName) {
        ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(strLastName) , nil);
    }
    
    
    ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    if (strCell)
    {
        ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(strCell), (CFStringRef)@"iPhone", NULL);
    }
    ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
    CFRelease(phoneNumberMultiValue);
    
    
    //Add person Object to addressbook Object.
    ABAddressBookAddRecord(addressBook, person, &cfError);
    if (ABAddressBookSave(addressBook, nil))
    {
        NSLog(@"\nPerson Saved successfuly");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“App” message:@"Contact saved successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else
    {
        NSLog(@"\n Error Saving person to AddressBook");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“App” message:@"Contact details are not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    [SVProgressHUD dismiss];
    
    }
    - (BOOL)checkExistsContacts
    {
    NSString *strFirstName=@“fname”;
    NSString *strLastName=@“lname”;
    
    CFErrorRef err;
    ABAddressBookRef adbk = ABAddressBookCreateWithOptions(addressBook,&err);
    ABRecordRef moi = NULL;
    CFArrayRef matts = ABAddressBookCopyPeopleWithName(adbk, (__bridge CFStringRef)strFirstName);
    // might be multiple matts, but let's find the one with last name Neuburg
    
    for (CFIndex ix = 0; ix < CFArrayGetCount(matts); ix++)
    {
        ABRecordRef matt = CFArrayGetValueAtIndex(matts, ix);
        CFStringRef last = ABRecordCopyValue(matt, kABPersonLastNameProperty);
        if (last && CFStringCompare(last, (CFStringRef)strLastName, 0) == 0)
            moi = matt;
        if (last)
            CFRelease(last);
    }
    
    if (NULL == moi)
    {
        NSLog(@"Couldn't find myself");
        CFRelease(matts);
        CFRelease(adbk);
        return NO;
    }
    else
    {
        NSLog(@"number already exists");
        return YES;
    }
    return NO;
    }
    

    【讨论】:

      【解决方案2】:

      Apple 提供ABNewPersonViewController。如果您想要一些示例代码,请参阅Quick Contacts,尤其是本节:

      ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
      picker.newPersonViewDelegate = self;
      
      UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
      [self presentModalViewController:navigation animated:YES];
      
      [picker release];
      [navigation release];
      

      【讨论】:

        【解决方案3】:

        您是否尝试过使用 ABNewPersonViewController?
        请参阅 this 并查找标题为“提示用户创建新人员记录”的部分。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-06
          • 2016-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多