ios - 如何使用 Objective C 在我的应用程序中获取联系人图像和电话号码

标签 ios objective-c contacts cncontact

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

- (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];

    }];
}

对于这段代码,我得到了响应

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

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

那么这在 iOS 9 中可能吗?

给我建议

谢谢。

最佳答案

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

    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: 方法中相应地使用字典。

关于ios - 如何使用 Objective C 在我的应用程序中获取联系人图像和电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34492804/

相关文章:

ios - 如何解析快速字符串以获取数据

c++ - 使用 cocoapods 在 iOS 上集成 GoogleCloudMessaging

ios - SkScene 更新方法被推送通知中断,如何检测中断

Android - 从所有来源获取所有联系人

Android 2.0 如何获取联系人组中的显示名称和号码

ios - 当用户位于特定位置时如何获取基于位置的内容?

iphone - WWDC2008 和 2009 iPhone 视频

iOS - UITableView 不滚动到数据源中的最后一行

android - Uri.withAppendedPath 方法

ios - NSArray 上的 Swift 内存泄漏