如何使用Objective C在我的应用程序中获取联系人图像和电话号码 [英] how to fetch contact image and phone Number in my app using Objective C

查看:79
本文介绍了如何使用Objective C在我的应用程序中获取联系人图像和电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从手机中获取联系人姓名。
检查我的代码:

I am getting the contact name from my phone. Check my code:

- (void) fetchContacts
{
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusDenied) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"This app previously was refused permissions to contacts; Please go to settings and grant permission to this app so it can use contacts" preferredStyle:UIAlertControllerStyleAlert];

        [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:TRUE completion:nil];
        return;
    }

    CNContactStore *store = [[CNContactStore alloc] init];    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

        // make sure the user granted us access

        if (!granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // user didn't grant access;
                // so, again, tell user here why app needs permissions in order  to do it's job;
                // this is dispatched to the main queue because this request could be running on background thread
            });
            return;
        }

        // build array of contacts

        NSMutableArray *contacts = [NSMutableArray array];

        NSError *fetchError;
        CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]]];

        BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop) {
            [contacts addObject:contact];
        }];



        if (!success) {
            NSLog(@"error = %@", fetchError);
        }



        // you can now do something with the list of contacts, for example, to show the names

        CNContactFormatter *formatter = [[CNContactFormatter alloc] init];

        for (CNContact *contact in contacts) {
            if (!_contacts) {
                _contacts = [[NSMutableArray alloc] init];
            }

            NSString *string = [formatter stringFromContact:contact];
            NSLog(@"contact = %@", string);
            [_contacts addObject:string];
        }
        [_contactatableview reloadData];

    }];
}

对于此代码,我得到响应

for this code, I am getting the response

2015-12-28 15:18:13.867 finalprototype[2138:66648] contact = John Appleseed
2015-12-28 15:18:13.868 finalprototype[2138:66648] contact = Kate Bell
2015-12-28 15:18:13.868 finalprototype[2138:66648] contact = Anna Haro
2015-12-28 15:18:13.868 finalprototype[2138:66648] contact = Daniel Higgins Jr.
2015-12-28 15:18:13.868 finalprototype[2138:66648] contact = David Taylor
2015-12-28 15:18:13.868 finalprototype[2138:66648] contact = Hank M. Zakroff

我的要求是我需要显示图像并我的应用程序联系人页面中特定联系人的电话号码。

my requirement is I need to display the image and phone number of a particular contact in my app contacts page.

那么在iOS 9中有可能吗?

So is this possible in iOS 9?

给我建议

谢谢。

推荐答案

获取图像联系很容易。我认为您想在表格视图中显示联系人的图像以及姓名。如果是这样,则将每个联系人的数据作为字典存储在数据源数组中。

Fetching the image of the contact is easy. I think that you want to show the image of the contact along with the name in the table view. If so, then store each contact's data in your data source array as dictionaries.

    for (CNContact *contact in contacts) {
        if (!_contacts) {
            _contacts = [[NSMutableArray alloc] init];
        }

        NSArray<CNLabeledValue<CNPhoneNumber*>*> *phoneNumbers = contact.phoneNumbers;
        CNPhoneNumber *phone = [phoneNumbers[0] value];

        NSMutableDictionary *dictionary = [NSMutableDictionary new];
        NSString *nameString = contact.givenName;
        [dictionary setValue:nameString forKey:@"name"];

        NSString *phoneString = phone.stringValue;
        [dictionary setValue:phoneString forKey:@"phone"];

        if (contact.imageDataAvailable) {
            [dictionary setValue:self.contactToAdd.imageData forKey:@"image"];
        }
        NSLog(@"contact = %@", contact);
        [_contacts addObject:dictionary];
    }

在您的 cellForRowAtIndexPath中:方法相应地使用字典。

And in your cellForRowAtIndexPath: method use the dictionary accordingly.

这篇关于如何使用Objective C在我的应用程序中获取联系人图像和电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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