iOS開發之系統通訊錄

來源:互聯網
上載者:User

@iOS叫用作業通訊錄所用的庫檔案

AddressBook.framework

AddressBookUI.framework

#import "HMTMainViewController.h"#import #import @interface HMTMainViewController ()@property (nonatomic,strong) ABPeoplePickerNavigationController *personPickVC;@property (nonatomic,strong) UIWebView *phoneCallWebView;@end@implementation HMTMainViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationItem.title = @"系統通訊錄";    //    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(didClickDeletePersonAction)];    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    button.frame = CGRectMake(100, 70, 120, 40);    [button setTitle:@"選擇連絡人" forState:UIControlStateNormal];    [button addTarget:self action:@selector(didClickSelectAction) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    }- (void)didClickSelectAction{        //  選擇電話薄的連絡人    self.personPickVC = [[ABPeoplePickerNavigationController alloc] init];    _personPickVC.peoplePickerDelegate = self;    _personPickVC.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]];    [self deletePerson];    [self presentViewController:_personPickVC animated:YES completion:NULL];    //[self.navigationController pushViewController:[_personPickVC.viewControllers objectAtIndex:0] animated:YES];    //[self getAllPerson];            }//  進入選擇連絡人介面- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{    //  選中連絡人後,就退出,並返回想要的屬性    [peoplePicker dismissViewControllerAnimated:YES completion:^{               NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);        NSLog(@"person = %@",firstName);                ABMultiValueRef phones = ABRecordCopyValue(person,kABPersonPhoneProperty);        for (int i = 0; i < ABMultiValueGetCount(phones); i++) {            NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));            NSLog(@"telephone = %@",phone);        }    }];        return YES;}- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{    [peoplePicker dismissViewControllerAnimated:YES completion:^{            }];}- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{    return YES;}//  展示所有連絡人- (void)getAllPerson{    CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);    for (id person in ((__bridge NSArray *)allPerson)) {                NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty);        NSLog(@"firstName = %@",firstName);         //  因為一個使用者可能有多個電話,所以需要迴圈調取        ABMultiValueRef phones = ABRecordCopyValue((__bridge ABRecordRef)person,kABPersonPhoneProperty);        for (int i = 0; i < ABMultiValueGetCount(phones); i++) {            NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));            NSLog(@"telephone = %@",phone);        }            }}//  新增連絡人...- (void)addPerson{    ABAddressBookRef addressBook = self.personPickVC.addressBook;    ABRecordRef person = ABPersonCreate();    ABRecordSetValue(person, kABPersonFirstNameProperty, @"胡", nil);    ABRecordSetValue(person, kABPersonLastNameProperty, @"明濤", nil);    ABMutableMultiValueRef mulRef = ABMultiValueCreateMutable(kABStringPropertyType);    for (int i = 0; i < 1; i++) {        ABMultiValueIdentifier mutableIdentifier;        ABMultiValueAddValueAndLabel(mulRef, @"18690231234", nil, &mutableIdentifier);    }    ABRecordSetValue(person, kABPersonPhoneProperty, mulRef, nil);    ABAddressBookAddRecord(addressBook, person, nil);    ABAddressBookSave(addressBook, nil);    }//  刪除連絡人- (void)deletePerson{        CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);    for (id person in (__bridge NSArray *)allPerson) {                NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty);        if ([firstName isEqualToString:@"胡"]) {            ABAddressBookRemoveRecord(self.personPickVC.addressBook, (__bridge ABRecordRef)person, nil);        }    }    ABAddressBookSave(self.personPickVC.addressBook, nil);}//  撥打到電話- (void)dialPhoneNumber:(NSString *)phoneNumber{    // 1.UIWebView載入電話    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];    self.phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];    [self.phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];        // 2.私人方法    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10086"]];}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.