iOS10 - 訪問系統通訊錄新方法


  • 所需框架
#import <ContactsUI/ContactsUI.h>
  • 遵循代理
CNContactPickerDelegate
  • 調用通訊錄
    • 如果在iOS10的機器上調用以前的ABPeoplePickerNavigationController老方法將直接崩潰.
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //iOS 10
//    AB_DEPRECATED("Use CNContactPickerViewController from ContactsUI.framework instead")
    CNContactPickerViewController * contactVc = [CNContactPickerViewController new];
    contactVc.delegate = self;
    [self presentViewController:contactVc animated:YES completion:^{
        
    }];
}
  • 選擇完成代理回調
#pragma mark - 用戶點擊聯系人獲取方法 兩個方法都寫只調用此方法
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
    
//    NSLog(@"contact : %@",contact);
    
                                // 姓氏               名字
    NSLog(@"name:%@%@",contact.familyName,contact.givenName);
    
    //公司名
    NSLog(@"公司: %@",contact.organizationName);

    //獲取通訊錄某個人所有電話並存入數組中 需要哪個取哪個
    NSMutableArray * arrMPhoneNums = [NSMutableArray array];
    for (CNLabeledValue * labValue in contact.phoneNumbers) {
        
        NSString * strPhoneNums = [labValue.value stringValue];
        NSLog(@"所有電話是: %@",strPhoneNums);
        [arrMPhoneNums addObject:strPhoneNums];
    }
    
    //所有郵件地址數組
    NSMutableArray * arrMEmails = [NSMutableArray array];
    for (CNLabeledValue * labValue in contact.emailAddresses) {
        
        NSLog(@"email : %@",labValue.value);
        [arrMEmails addObject:labValue.value];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
  • 用戶點某個聯系人進去獲取屬性調用的方法 例如從通訊錄選擇聯系人打電話兩個方法都寫只調用上面方法
#pragma mark - 用戶點進去獲取屬性調用方法 例如從通訊錄選擇聯系人打電話兩個方法都寫只調用上面方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{

//    NSLog(@"contactProperty : %@",contactProperty);
//    NSLog(@"contact : %@",contactProperty.contact);
//    NSLog(@"key : %@",contactProperty.key);
//    [[UIApplication sharedApplication] openURL:url];
//    NSLog(@"identifier : %@",contactProperty.identifier);
//    NSLog(@"label : %@",contactProperty.label);
    
    //獲得點擊的屬性,在此進行處理...
    NSLog(@"value : %@",[contactProperty.value stringValue]);
    [picker dismissViewControllerAnimated:YES completion:nil];
}

  • 取消選擇回調
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{

    [picker dismissViewControllerAnimated:YES completion:nil];
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM