Swift编程

iOS 调用系统通讯录,获取联系人信息;适配 iOS9;发送短信

2016-08-29  本文已影响0人  流痕Coder

一、 调用系统通讯录,获取联系人信息

常用的一个代理方法

// Called after a property has been selected by the user.
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0);

常用的两个代理方法

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;

当上面两个代理方法都存在时, 只会执行后者。

// 通讯录列表 - 点击某个联系人 - 详情页 - 点击一个号码, 返回
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {

    /** 姓名 */
    NSString *personName = [NSString stringWithFormat:@"%@%@", contactProperty.contact.familyName, contactProperty.contact.givenName];
    /** 电话 */
    NSString *phoneNumber = [contactProperty.value stringValue];
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
    
    // 选中联系人后跳转详情页, 跟从手机系统进入一样, 可以拨打电话、编辑联系人.
    CNContactViewController *contactVC = [CNContactViewController viewControllerForContact:contact];
    [self.navigationController pushViewController:contactVC animated:YES];
    
    /**
     *  还可以通过viewControllerForNewContact新增联系人
     *  注意:通过其他方式进入详情页, 用 modal
     */
}

二、发信息


1. 导入 MessageUI 框架:  #import <MessageUI/MessageUI.h>
2. 服从协议:MFMessageComposeViewControllerDelegate
3. 配置方法,指定代理,并实现代理方法

/** 群发/单发 指定信息 */
- (void)sendContacts:(NSArray*)phoneNumbers message:(NSString *)message {
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if(messageClass != nil){
        MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc]init];
        messageVC.messageComposeDelegate = self;
        messageVC.body = message;
        messageVC.recipients = phoneNumbers;
        [self presentViewController:messageVC animated:YES completion:nil];
    }else {
        //        Have error here ...
    }
    
}


/** 发送信息后的回调方法  **/
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    
    [self dismissViewControllerAnimated:YES completion:^{}];
    
    switch (result) {
        case MessageComposeResultCancelled:
            
            break;
        case MessageComposeResultSent:
            
            break;
        case MessageComposeResultFailed:
            
            break;
            
        default:
            break;
    }
}

点此下载源码

上一篇下一篇

猜你喜欢

热点阅读