如何从iphone检查导入联系人

时间:2013-02-01 12:25:53

标签: iphone xcode

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    // setting the first name
    NSString *fname = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    // setting the last name
    NSString *lname = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSString *mail=(__bridge NSString*)ABRecordCopyValue(person, kABPersonEmailProperty);
        //NSString *mobile=(__bridge NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);

    NSString *pNo = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
    NSLog(@"LLname%@",lname);
    NSLog(@"fLname%@",fname);
    NSLog(@"mobile%@",pNo);
    NSLog(@"mail%@",mail);
     if([fname isEqualToString:@""] || fname == nil)
     {
        UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Please enter first name." delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", @"") otherButtonTitles:nil];
        [locationAlert show];
     // return;
     }
     else if([lname isEqualToString:@""]|| lname == nil)
     {
        UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Please enter last name." delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", @"") otherButtonTitles:nil];
        [locationAlert show];
     //return;
     }
     else if([pNo isEqualToString:@""]|| pNo == nil)
    {
         UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Please enter mobile no." delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", @"") otherButtonTitles:nil];
         [locationAlert show];
    // return;
     }

    else if([mail isEqualToString:@""] || mail == nil)
    {
        UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Please enter email address." delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", @"") otherButtonTitles:nil];
        [locationAlert show]; 
    //return;
    }
    else
    {
    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    CFStringRef email = ABMultiValueCopyValueAtIndex(emails, 0);
    NSLog(@"email %@\n",(__bridge NSString *) email);

    // setting the number
    /*
     this function will set the first number it finds

     if you do not set a number for a contact it will probably
     crash
     */

    NSString *mNo = pNo;
    mNo=[mNo stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mNo=[mNo stringByReplacingOccurrencesOfString:@")" withString:@""];
    mNo=[mNo stringByReplacingOccurrencesOfString:@" " withString:@""];
    mNo=[mNo stringByReplacingOccurrencesOfString:@"-" withString:@""];
//    NSLog(@"mno %@\n",mNo);

    NSString *name = [fname stringByAppendingFormat:[NSString stringWithFormat:@" %@",lname]];

    [clist setValue:mNo forKey:name];
//    NSLog(@"dic %@\n",clist);

//    [allcontactArray addObject:name];
    //    [contactList addObject:(NSArray *)allcontactArray];
    [contacts addObject:name];
//    NSLog(@"contacts %@\n",contacts);

//    NSLog(@"str %@\n",[clist objectForKey:name]);

    [contactArr addObject:name];
    [contactDic setObject:mNo forKey:name];

//    NSLog(@"arr: %@\n",contactArr);
//    NSLog(@"dic: %@\n",contactDic);

    contactname = name;

    [contactStr appendFormat:@"%@, ",name];
//     NSLog(@"contactstr: %@\n",contactStr);


//    NSLog(@"mno %@\n",mNo);

    [phoneNoArray addObject:mNo];
    [phoneStr appendFormat:@"%@=",mNo];

//    NSLog(@"phonearr: %@\n",phoneNoArray);
//    NSLog(@"phonestr: %@\n",phoneStr);

    tf.text = contactStr;

    // remove the controller
    [self dismissModalViewControllerAnimated:YES];

    return NO;
    }
}

我正在使用这个开源来从iphone导入联系人。我的应用程序在名字或姓氏或移动号码或电子邮件空时崩溃。我可以检查名字和姓氏是否空。我无法检查mobileno和email.Please帮助解决这个问题。

1 个答案:

答案 0 :(得分:0)

要检查电话号码是否存在,您可以这样做,

ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty); 
if([phoneNumbers count] > 0)
{
    // more than one phone numbers exist
}

对于电子邮件,

ABMultiValueRef emailprperty = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray* emails = (NSArray*)ABMultiValueCopyArrayOfAllValues(emailprperty);

if([emails count] > 0)
{
    // email exists
} 

希望这可以帮助你...