你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發之系統通訊錄

iOS開發之系統通訊錄

編輯:IOS開發綜合

@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"]];
}


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved