【问题标题】:iPhone:add, update and remove contact programmaticallyiPhone:以编程方式添加、更新和删除联系人
【发布时间】:2009-12-25 11:21:56
【问题描述】:

我是 iPhone 编程新手 我想问我是否可以从 iPhone 联系人列表中添加、更新或删除联系人。 请建议或提供一些有用的链接。

【问题讨论】:

    标签: iphone add contact


    【解决方案1】:

    Apple 在访问一般通讯簿和操纵contacts 方面拥有广泛的docs。但是,它是基于 C 的,而不是 Objective-C。

    【讨论】:

      【解决方案2】:

      删除联系人:

           - (void) deleteAllContacts {
      CNContactStore *contactStore = [[CNContactStore alloc] init];
      
      [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
          if (granted == YES) {
              NSArray *keys = @[CNContactPhoneNumbersKey];
              NSString *containerId = contactStore.defaultContainerIdentifier;
              NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
              NSError *error;
              NSArray *cnContacts = [contactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
      
              if (error) {
                  NSLog(@"error fetching contacts %@", error);
              } else {
                  CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
      
                  for (CNContact *contact in cnContacts) {
           //Here write the necessary condition to filter the contacts which you want to delete .  
                      [saveRequest deleteContact:[contact mutableCopy]];
                  }
      
                  [contactStore executeSaveRequest:saveRequest error:nil];
                  DDLogVerbose(@"Deleted contacts %lu", cnContacts.count);
              }
          }
      }];
      

      }

      添加联系人:

          CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
      if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
          UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Access to contacts" message:@"This app requires access to contacts" preferredStyle:UIAlertControllerStyleActionSheet];
          [alert addAction:[UIAlertAction actionWithTitle:@"Go to settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
              [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
          }]];
          [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
          [self presentViewController:alert animated:TRUE completion:nil];
          return;
      }
      CNContactStore *store = [[CNContactStore alloc] init];
      [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
          if (!granted) {
              dispatch_async(dispatch_get_main_queue(), ^{
                  //user didn't grant access
              });return;}
          //create contact
          CNMutableContact *contact = [[CNMutableContact alloc] init];
          NSMutableString *temp = [[NSMutableString alloc] initWithString:newContactName];
          [temp appendString:@"(PATIENT)"];
          NSString *immutable = [NSString stringWithString:temp];
          contact.givenName = immutable;
      
          CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:mobileNumberOfPatient]];
          contact.phoneNumbers = @[homePhone];
          CNSaveRequest *request = [[CNSaveRequest alloc] init];
          [request addContact:contact toContainerWithIdentifier:nil];
          [self.activityIndicator setHidden:YES];
          [self.activityIndicator stopAnimating];
          [self shareViaWhatsApp];
          //save it
          NSError *saveError;
          if (![store executeSaveRequest:request error:&saveError]) {
              NSLog(@"error: %@",saveError);
          }
      }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-02
        • 2011-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多