我想显示设备联系人用户

时间:2016-09-12 06:32:24

标签: ios objective-c ios6

这是我获取设备联系人的代码,但现在我想显示特定的用户联系信息,如联系人用户默认信息屏幕(如App Store中的Easy Backup App)

 CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
     if( status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted){
     }else{

          CNContactStore *contactStore = [[CNContactStore alloc] init];
          NSArray *keys = [[NSArray alloc]initWithObjects:CNContactIdentifierKey, CNContactEmailAddressesKey,CNContactOrganizationNameKey,CNContactJobTitleKey,CNContactNoteKey,CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey,CNContactEmailAddressesKey, CNContactViewController.descriptorForRequiredKeys, nil];
          CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
          request.predicate = nil;
          [contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact* __nonnull contact, BOOL* __nonnull stop){
               NSString *phoneNumber = @"";
               NSString *emailAddress = @"";
               if( contact.phoneNumbers)
                    phoneNumber = [[[contact.phoneNumbers firstObject] value] stringValue];
               if( contact.emailAddresses)
                    emailAddress = [[contact.emailAddresses firstObject] value];
               NSMutableDictionary *contactValue=[[NSMutableDictionary alloc] init];
               NSString *catStr;

               if ([contact.givenName isEqualToString:@""]) {
               }else{
                    [contactValue setValue:phoneNumber ?:@"" forKey:@"phoneNumber"];
                    [contactValue setValue:emailAddress   ?:@"" forKey:@"emailAddress"];
                    [contactValue setObject:contact.identifier forKey:@"phoneIdentifier"];
                    [contactValue setObject:contact.organizationName ?:@"" forKey:@"organizationName"];
                    [contactValue setObject:contact.jobTitle ?:@"" forKey:@"jobTitle"];
                    [contactValue setObject:contact.note ?:@"" forKey:@"note"];
                    [contactValue setObject:contact.birthday ?:@"" forKey:@"birthday"];
                    [contactValue setObject:contact.givenName ?:@"" forKey:@"firstName"];
                    [contactValue setObject:contact.familyName ?:@"" forKey:@"lastName"];
                    [contactValue setObject:contact.note ?:@"" forKey:@"imageData"];

                    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"inderPath"] integerValue]==1) {
                         if ([contact.familyName isEqualToString:@""]) {
                              catStr=[NSString stringWithFormat:@"%@", contact.givenName];
                         }else if ([contact.givenName isEqualToString:@""]){
                              catStr=[NSString stringWithFormat:@"%@ ",contact.givenName];
                         }else{
                              catStr=[NSString stringWithFormat:@"%@ %@", contact.familyName,contact.givenName];
                         }
                    }else{
                         catStr=[NSString stringWithFormat:@"%@ %@", contact.givenName,contact.familyName];
                    }

                    [contactValue setObject:catStr forKey:@"userName"];
               }
               [_totalContact addObject:contactValue];
          }];
          }

0 个答案:

没有答案