zoukankan      html  css  js  c++  java
  • 调用电话/获取通讯录

    1.调用电话

    - (void) dialPhoneNumber:(NSString *)aPhoneNumber  
    {  
        NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",aPhoneNumber]];  
        if ( !phoneCallWebView ) {          
            phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];  
        }  
        [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];  
    }  

    2.获取通讯录

    总结: 主要用到AddressBook.framework 和 AddressBookUI.framework两个框架

    AddressBookUI顾名思义, 是ui部分的, 包含:

    通讯录列表控制器:ABPeoplePickerNavigationController

    联系人: ABPersonViewController

    新联系人: ABNewPersonViewController

    未知联系人: ABUnknowPersonViewController

    AddressBook更为常用些:

    ABAddressBook 通讯录人员的增删改

    ABPerson 通讯录的查询

    ABRecord 一个录人员, 获取人员的各种属性(电话 姓 名 公司 邮箱 备注...)

    AFMutiValueRef: 主要用于电话, 因为电话有很多个

    AddressBook主要与CoreFoundation连用, 所以释放AddressBook框架的对象统一使用CFRelease(AB对象);

    //ios6以后需要授权

        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

        //获取通讯录权限

        dispatch_semaphore_t semaphore_t = dispatch_semaphore_create(0);

        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

            dispatch_semaphore_signal(semaphore_t);

        });

        dispatch_semaphore_wait(semaphore_t, DISPATCH_TIME_FOREVER);

    ABAddressBookRef addressBook = ABAddressBookCreate();

        CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);
        
        for(int i = 0; i < CFArrayGetCount(results); i++)
        {
            ABRecordRef person = CFArrayGetValueAtIndex(results, i);
            //读取firstname
            NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
            if(personName != nil)
                textView.text = [textView.text stringByAppendingFormat:@" 姓名:%@ ",personName];
            //读取lastname
            NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
            if(lastname != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",lastname];
            //读取middlename
            NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
            if(middlename != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",middlename];
            //读取prefix前缀
            NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);
            if(prefix != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",prefix];
            //读取suffix后缀
            NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPers*****uffixProperty);
            if(suffix != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",suffix];
            //读取nickname呢称
            NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);
            if(nickname != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",nickname];
            //读取firstname拼音音标
            NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);
            if(firstnamePhonetic != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",firstnamePhonetic];
            //读取lastname拼音音标
            NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);
            if(lastnamePhonetic != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",lastnamePhonetic];
            //读取middlename拼音音标
            NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);
            if(middlenamePhonetic != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",middlenamePhonetic];
            //读取organization公司
            NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
            if(organization != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",organization];
            //读取jobtitle工作
            NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);
            if(jobtitle != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",jobtitle];
            //读取department部门
            NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);
            if(department != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",department];
            //读取birthday生日
            NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);
            if(birthday != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",birthday];
            //读取note备忘录
            NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);
            if(note != nil)
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",note];
            //第一次添加该条记录的时间
            NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);
            NSLog(@"第一次添加该条记录的时间%@ ",firstknow);
            //最后一次修改該条记录的时间
            NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);
            NSLog(@"最后一次修改該条记录的时间%@ ",lastknow);
            
            //获取email多值
            ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
            int emailcount = ABMultiValueGetCount(email);    
            for (int x = 0; x < emailcount; x++)
            {
                //获取email Label
                NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));
                //获取email值
                NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x);
                textView.text = [textView.text stringByAppendingFormat:@"%@:%@ ",emailLabel,emailContent];
            }
            //读取地址多值
            ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
            int count = ABMultiValueGetCount(address);    
            
            for(int j = 0; j < count; j++)
            {
                //获取地址Label
                NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",addressLabel];
                //获取該label下的地址6属性
                NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);        
                NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
                if(country != nil)
                    textView.text = [textView.text stringByAppendingFormat:@"国家:%@ ",country];
                NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
                if(city != nil)
                    textView.text = [textView.text stringByAppendingFormat:@"城市:%@ ",city];
                NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
                if(state != nil)
                    textView.text = [textView.text stringByAppendingFormat:@"省:%@ ",state];
                NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
                if(street != nil)
                    textView.text = [textView.text stringByAppendingFormat:@"街道:%@ ",street];
                NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];
                if(zip != nil)
                    textView.text = [textView.text stringByAppendingFormat:@"邮编:%@ ",zip];    
                NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];
                if(coutntrycode != nil)
                    textView.text = [textView.text stringByAppendingFormat:@"国家编号:%@ ",coutntrycode];    
            }
            
            //获取dates多值
            ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);
            int datescount = ABMultiValueGetCount(dates);    
            for (int y = 0; y < datescount; y++)
            {
                //获取dates Label
                NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));
                //获取dates值
                NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y);
                textView.text = [textView.text stringByAppendingFormat:@"%@:%@ ",datesLabel,datesContent];
            }
            //获取kind值
            CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);
            if (recordType == kABPersonKindOrganization) {
                // it's a company
                NSLog(@"it's a company ");
            } else {
                // it's a person, resource, or room
                NSLog(@"it's a person, resource, or room ");
            }
            
            
            //获取IM多值
            ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);
            for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++)
            {
                //获取IM Label
                NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);
                textView.text = [textView.text stringByAppendingFormat:@"%@ ",instantMessageLabel];
                //获取該label下的2属性
                NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);        
                NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];
                if(username != nil)
                    textView.text = [textView.text stringByAppendingFormat:@"username:%@ ",username];
                
                NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];
                if(service != nil)
                    textView.text = [textView.text stringByAppendingFormat:@"service:%@ ",service];            
            }
            
            //读取电话多值
            ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
            for (int k = 0; k<ABMultiValueGetCount(phone); k++)
            {
                //获取电话Label
                NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
                //获取該Label下的电话值
                NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k);
                    
                textView.text = [textView.text stringByAppendingFormat:@"%@:%@ ",personPhoneLabel,personPhone];
            }
            
            //获取URL多值
            ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);
            for (int m = 0; m < ABMultiValueGetCount(url); m++)
            {
                //获取电话Label
                NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));
                //获取該Label下的电话值
                NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m);
                
                textView.text = [textView.text stringByAppendingFormat:@"%@:%@ ",urlLabel,urlContent];
            }
            
            //读取照片
            NSData *image = (NSData*)ABPersonCopyImageData(person);
                

            UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)];
            [myImage setImage:[UIImage imageWithData:image]];
            myImage.opaque = YES;
            [textView addSubview:myImage];
            

        
        }
        
        CFRelease(results);
        CFRelease(addressBook); 

  • 相关阅读:
    Atitit sumdoc t5 final file list \sumdoc t5 final\sumdoc t511 \sumdoc t5 final\sumdoc t511.zip \sum
    上课流程法如何上好第一节课(1) 目录 1. 目录 1 1.1. 销售自己 1 1.2. 销售课程 1 1.3. 学习方法 1 1.4. 制定规章 2 2. 销售自己自我介绍 2 2.1.
    Atitit 重复文件清理工具 按照文件名 目录 1. 原理, 1 1.1. If base filex exist dele other files 1 1.2. Get getStartIdex
    Atitit sumdoc index 2019 v6 t56 .docx Atitit sumdoc index s99 目录 1. Zip ver 1 1.1. C:\Users\Adminis
    Atitit lucence es solr的各种query 与sql运算符的对比 目录 1.1. 等于运算 TermQuery 1 1.2. 范围运算 1 1.3. 大小运算 1 1.4. Wi
    Atitit 程序设计概论 艾提拉著作 目录 1. 界面ui设计 1 2. 编程语言部分 1 3. 面向对象的程序设计 1 4. 算法章节 数据结构 1 5. 第21章 标准库 2 5.1. 文件i
    Atitit 命令行执行sql 跨语言 目录 1.1. 无需输入密码,那就不要p参数即可 1 1.2. 4.使用mysql命令执行 1 1.3. 5.mysql命令执行sql,并将查询结果保存到
    Atitit java播放 wav MIXER 混响器编程 目录 1.1. MIXER 混响器编程 1 1.2. 得到系统中一共有多少个混音器设备: 1 1.3. 接口摘要 1 1.4. 调节音量
    Atitit object 和class的理解 目录 1.1. 发现很多Object的方法都是相同的,他们被重复地放在一个个对象当中,太浪费了。 1 1.2. 那我们怎么把这些Object给创建起来
    Atitit 数据库的历史与未来 目录 1.1. 两个对于数据库强需求的行业。电信 金融 1 1.2. 艾提拉分析 对数据库强需求行业金融 1 2. 数据库历史 2 2.1. ,上个世纪50,6
  • 原文地址:https://www.cnblogs.com/apem/p/4350286.html
Copyright © 2011-2022 走看看