Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 仅获取使用相同应用程序的手机联系人_Ios_Objective C_Contacts - Fatal编程技术网

Ios 仅获取使用相同应用程序的手机联系人

Ios 仅获取使用相同应用程序的手机联系人,ios,objective-c,contacts,Ios,Objective C,Contacts,我正在开发一个应用程序,希望从iphone中的联系人列表中获取使用同一应用程序的联系人。 怎么做?有没有示例代码或链接 请帮帮我 注意:我不想获取ios应用程序中的所有联系人,我 只想获取使用相同应用程序的联系人 首先导入AddressBook框架 然后调用这两个函数 -(void)AddressBookFetch{ ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); if (status =

我正在开发一个应用程序,希望从iphone中的联系人列表中获取使用同一应用程序的联系人。

怎么做?有没有示例代码或链接

请帮帮我


注意:我不想获取ios应用程序中的所有联系人,我 只想获取使用相同应用程序的联系人


首先导入AddressBook框架

然后调用这两个函数

-(void)AddressBookFetch{
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
        // if you got here, user had previously denied/revoked permission for your
        // app to access the contacts, and all you can do is handle this gracefully,
        // perhaps telling the user that they have to go to settings to grant access
        // to contacts
        [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        return;
    }

    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (!addressBook) {
        NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
        return;
    }

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            if (error) {
                NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
            }

            if (granted) {
                // if they gave you permission, then just carry on

                [self listPeopleInAddressBook:addressBook];
            } else {
                // however, if they didn't give you permission, handle it gracefully, for example...

                dispatch_async(dispatch_get_main_queue(), ^{
                    // BTW, this is not on the main thread, so dispatch UI updates back to the main queue

                    [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                });
            }
        CFRelease(addressBook);
    });

}
然后通过此函数列出联系人

- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
    NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
    numberOfPeople = [allPeople count];

    for (NSInteger i = 0; i < numberOfPeople; i++) {
        ABRecordRef person = (__bridge ABRecordRef)allPeople[i];

        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
        NSLog(@"Name:%@ %@", firstName, lastName);
        if (firstName==nil) {
            firstName=@"";
        }

        [nm addObject:firstName];
        if (lastName==nil) {
            lastName=@"";
        }
        [ttl addObject:lastName];


        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

        // CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
        //for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 0));
        NSLog(@"  phone:%@", phoneNumber);
        if (phoneNumber==nil) {
            phoneNumber=@"";
        }
        [phn addObject:phoneNumber];
        // }
        // CFRelease(phoneNumbers);

        NSLog(@"=============================================");
    }
}
-(void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSArray*allPeople=CfBrigingRelease(abAddressBookCopyArrayFallPeople(addressBook));
numberOfPeople=[allPeople count];
对于(NSInteger i=0;i
没有后端是不可能的,你不能只在ios应用程序中完成。

可能重复@NimitParekh我不想检索所有联系人,我只想检索使用同一应用程序的联系人。当您以编程方式添加联系人时,需要添加一些文本;当获取联系人时,可以过滤掉这些记录。您需要一个在线数据库来存储应用程序的用户及其电子邮件或其他唯一令牌(因此他们需要注册为用户)。然后获取所有联系人并筛选已知令牌。这不可能仅在应用程序内完成。我不想获取ios应用程序中的所有联系人,我只想获取使用同一应用程序的联系人。然后,您必须借助保存所有用户的数据库,并检查哪些用户与您的联系人匹配ts。