散记2

取出联系人信息的方法

1.引入AddressBook.framework和AddressBookUI.framework

2.获取打开contact联系人的权限

switch (ABAddressBookGetAuthorizationStatus())
    {
        case  kABAuthorizationStatusAuthorized:
        {
            //do something
                });
            });
        }
            break;
        case  kABAuthorizationStatusNotDetermined :
            <p class="p1">ABAddressBookRequestAccessWithCompletion<span class="s1">(</span>addressBook<span class="s1">, ^(</span><span class="s2">bool</span><span class="s1"> granted, </span>CFErrorRef<span class="s1"> error)</span></p><p class="p2">                                             {</p><p class="p2">                                                 <span class="s2">if</span> (granted)</p><p class="p2">                                                 {</p><p class="p2">                                                    //doSomething</p><p class="p2">                                                         });</p><p class="p2">                                                     });</p><p class="p2">                                                 }</p><p class="p2">                                             });</p>
            break;
        case  kABAuthorizationStatusDenied:
        case  kABAuthorizationStatusRestricted:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Warning"
                                                            message:@"Permission was not granted for Contacts."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles: nil];
            alert.delegate = self;
            [alert show];
        }
            break;
        default:
            break;
    }
3.获取联系人数据

<span class="s1" style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="objc"><pre name="code" class="objc">ABAddressBookRef <span style="font-family: Arial, Helvetica, sans-serif;">addressBook</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;"> = </span><span style="font-family: Arial, Helvetica, sans-serif;">ABAddressBookCreateWithOptions</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">(</span><span class="s2" style="font-family: Arial, Helvetica, sans-serif;">NULL</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">, </span><span class="s2" style="font-family: Arial, Helvetica, sans-serif;">NULL</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">);//获得contacts对象</span>
 
 
if (addressBook) {
        CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);//获得一个由所有联系人组成的数组

<span style="white-space:pre">	</span>//将results转成一个可变数组,方便后面的排序
        CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy
        (
         kCFAllocatorDefault,
         CFArrayGetCount(results),
         results
         );
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>//排序
        CFArraySortValues
        (
         peopleMutable,
         CFRangeMake(0, CFArrayGetCount(peopleMutable)),
         (CFComparatorFunction) ABPersonComparePeopleByName,
         (void*) ABPersonGetSortOrdering()
         );

        for (int i = 0; i < CFArrayGetCount(peopleMutable); i++) {
<span style="white-space:pre">	</span>//以下取得信息时所用的key都在ABPerson.h文件下
            
            ABRecordRef person = CFArrayGetValueAtIndex(peopleMutable, i);//获得一个联系人的数据

<span style="white-space:pre">	</span>//取得姓氏和名字
            NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
            NSString *lastname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

<span style="white-space:pre">	</span>//在中文系统下获得姓氏和名字的拼音,在其他语言环境下获得对应的信息。
            NSString *firstNamePinyin = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);
            NSString *lastNamePinyin = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>//获得该联系人的email信息数组,一个联系人可能会有若干个email
            ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
            int emailcount = (int)ABMultiValueGetCount(email);
<span style="white-space:pre"></span><pre name="code" class="objc">  <span style="white-space:pre">	</span>for (int x = 0; x < emailcount; x++)
                {
                    NSString* emailString = (__bridge NSString*)ABMultiValueCopyValueAtIndex(email, x);
                }
<span style="white-space:pre">	</span>//获得该联系人的头像图片
<span style="white-space:pre"></span><pre name="code" class="objc"><span style="white-space:pre">		</span>NSData *imageData = (__bridge NSData*)ABPersonCopyImageData(person);
 } } 
 
 
 

 关于iOS8下UItableviewCell的subviews与iOS7的差别 
 

iOS7下cell.subviews中只有一个UITableViewCellScrollView,cell的contentView是在UITableViewCellScrollView里,但是到了iOS8中后UITableViewCellScrollView被删去了,cell.contentView就在cell.subviews中。

猜你喜欢

转载自blog.csdn.net/u010843426/article/details/42553905