使用Objective-C从iPad上获取联系方式 [英] contact details from ipad using objective-c

查看:48
本文介绍了使用Objective-C从iPad上获取联系方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从iPad获取联系信息,并在我的应用程序中使用它.我正在使用以下代码,并从模拟器中获取详细信息.但是在ipad上运行时,我没有得到联系人图像,电子邮件等.我得到了正确的电话号码.

How can I get the contact details from iPad and use it in my app. I am using the following code and getting the details from simulator. But while running in ipad i am not getting the contact image, email etc. i am getting the phone number correctly.

ABAddressBookRef addressBook = ABAddressBookCreate();

// Get all contacts in the addressbook
NSArray *allPeople = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

for (id person in allPeople) {
    // Get all phone numbers of a contact
    ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
    ABMultiValueRef emailaddress = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonEmailProperty);


    // If the contact has multiple phone numbers, iterate on each of them
    for (int i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);



        // Remove all formatting symbols that might be in both phone number being compared
        NSCharacterSet *toExclude = [NSCharacterSet characterSetWithCharactersInString:@"/.()- +"];
        phone = [[phone componentsSeparatedByCharactersInSet:toExclude] componentsJoinedByString: @""];


        if ([phone isEqualToString:number]) {

            NSData *contactImageData = (__bridge NSData*)ABPersonCopyImageData((__bridge ABRecordRef)(person));
            NSString *mail = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(emailaddress, i);
            NSLog(@"%@",mail);
            if(mail)
            {
                hasEmail=TRUE;
                NSLog(@"true");
            }
            else{
                hasEmail=FALSE;
                NSLog(@"false");
            }

            ContactImage = [[UIImage alloc] initWithData:contactImageData];
           // [conImage setImage:ContactImage];

            break;
            break;
        }
    }
}
if(ContactImage)
{
    [conImage setImage:ContactImage];
}
else{

    NSLog(@"no image");
}

我需要在ipad上运行时获取图像

I need to get the image while running on ipad

推荐答案

您可以将此代码实现为 ViewDidLoad 方法.

You can implement this code to the ViewDidLoad Method.

 ABAddressBookRef addressBook;
 if ([self isABAddressBookCreateWithOptionsAvailable]) {
    CFErrorRef error = nil;
    addressBook = ABAddressBookCreateWithOptions(NULL,&error);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        // callback can occur in background, address book must be accessed on thread it was created on
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error) {

            } else if (!granted) {


            } else {
                // access granted
                [self GetAddressBook];


            }
        });
    });

} else {
    // iOS 4/5

    [self GetAddressBook];
 }

获取联系人的方法:-

Method For getting the Contacts :-

-(void)GetAddressBook
{
    Contacts = [[NSMutableArray alloc]init];

    if (ABAddressBookCreateWithOptions) {

    @try {

        ABAddressBookRef addressBook = ABAddressBookCreate();
        // NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        if (!addressBook) {
            NSLog(@"opening address book");
        }
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

        NSLog(@"opening address book ==%ld",nPeople);

        for (int i=0;i < nPeople;i++) {

            NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
            NSString *Contact;
            ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
            CFStringRef firstName, lastName;
            NSMutableArray *array = [[NSMutableArray alloc]init];
            NSString *email;
            firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
            ABMultiValueRef multiValueRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
            array = [(__bridge NSMutableArray *)ABMultiValueCopyArrayOfAllValues(multiValueRef) mutableCopy];
            email = ([array count] > 0) ? array[0] : @"";

            if(firstName)
            {
                Contact = [NSString stringWithFormat:@"%@", firstName];
                if(lastName)
                    Contact = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
            }
            [dOfPerson setObject:Contact forKey:@"name"];
            [dOfPerson setObject:[NSString stringWithFormat:@"%d", i] forKey:@"id"];
            [dOfPerson setObject:[NSString stringWithFormat:@"%@",@""] forKey:@"found"];
            [dOfPerson setObject:email forKey:@"email"];

            NSString* mobileLabel;
            for(CFIndex j = 0; j< ABMultiValueGetCount(phones); j++)
            {
                mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
                if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                {
                    [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                }
                else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
                {
                    [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                    break ;
                }
            }
            [Contacts addObject:dOfPerson];
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);          
    }
    dispatch_async(dispatch_get_main_queue(), ^{           

    });

    NSLog(@"%@",Contacts);
    if([Contacts count]>0)
    {
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
        [Contacts sortUsingDescriptors:[NSArray arrayWithObject:sort]];
        [Tableview reloadData];
    }
    else
    {

    }
}
else
{
    [self GetAddressBook];
}
}

此处 Contacts NSMutableArray ,并且所有数据都显示在 UITableView 中.此数据包括名字,姓氏,电话号码和电子邮件地址.希望对您有所帮助.

Here Contacts is an NSMutableArray and all the data is being shown in a UITableView. This data includes the First Name, Last Name, Phone Number and Email address. Hope it helps.

这篇关于使用Objective-C从iPad上获取联系方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆