{"id":541,"date":"2014-08-20T11:33:51","date_gmt":"2014-08-20T03:33:51","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=541"},"modified":"2016-07-15T14:30:04","modified_gmt":"2016-07-15T06:30:04","slug":"ios%e5%bc%80%e5%8f%91%e8%8e%b7%e5%8f%96%e6%89%8b%e6%9c%ba%e9%80%9a%e8%ae%af%e5%bd%95%ef%bc%88%e6%9c%80%e7%ae%80%e5%8d%95%e6%98%8e%e4%ba%86%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/08\/20\/113351541.html","title":{"rendered":"ios\u5f00\u53d1\u83b7\u53d6\u624b\u673a\u901a\u8baf\u5f55\uff08\u6700\u7b80\u5355\u660e\u4e86\uff09"},"content":{"rendered":"<p>\u7f51\u4e0a\u641c\u7d22\u4e86\u5f88\u591a\u76f8\u5173\u8d44\u6599\u90fd\u5199\u5f97\u8fc7\u4e8e\u590d\u6742\uff0c\u53ef\u6211\u4eec\u6700\u5e38\u7528\u7684\u53ea\u9700\u8981\u7528\u5230\u59d3\u540d\u4e0e\u7535\u8bdd\u53f7\u7801\uff0c\u6240\u4ee5\u6211\u6574\u7406\u4e86\u4e00\u4e0b\uff0c\u53ea\u9700\u8981\u4e00\u4e2a\u65b9\u6cd5\u641e\u5b9a\uff01\u6162\u6162\u770b\u5427<br \/>\n1\u3001\u5bfc\u5165AddressBook.framework\u5148\uff0c\u7136\u540e\u5f15\u7528 #import <AddressBook\/AddressBook.h><br \/>\n2\u3001\u6388\u6743\uff0c\u5982\u679c\u5df2\u6210\u529f\uff0c\u8bfb\u53d6\u901a\u8baf\u5f55\u6570\u636e\u5230\u6570\u7ec4\u91cc\uff0c\u5426\u5219\u7a0b\u5e8f\u4f1a\u5d29\u6e83<br \/>\n3\u3001\u8bfb\u53d6\u65b9\u6cd5<\/p>\n<pre lang=\"objc\" line=\"1\">- (NSArray *)getContacts\n{\n    \n    \/\/ 1\u3001\u521b\u5efa\u5bf9\u8c61\n    ABAddressBookRef tmpAddressBook = ABAddressBookCreateWithOptions(nil, nil);\n    \n    \/\/ 2\u3001\u83b7\u53d6\u901a\u8baf\u5f55\n    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);\n    \n    \/\/ 3\u3001\u904d\u5386\u901a\u8baf\u5f55\n    NSMutableArray *array = [NSMutableArray array];\n    for (int i = 0; i < CFArrayGetCount(results); i++){\n        \/\/ 3.1\u3001\u83b7\u53d6\u5355\u6761\u8bb0\u5f55\n        ABRecordRef person = CFArrayGetValueAtIndex(results, i);\n        \n        \/\/ 3.2\u3001\u53d6\u8bb0\u5f55\u4e2d\u7684\u540d\u5b57\n        NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);\n        NSString *name = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);\n        if (firstName) name = [name stringByAppendingString:firstName];                     \/\/ \u540d\u5b57\u62fc\u63a5\n        \n        \/\/ 3.3\u3001\u53d6\u8bb0\u5f55\u4e2d\u7684\u53f7\u7801\n        ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);         \/\/ \u6240\u6709\u53f7\u7801\n        for (int k = 0; k < ABMultiValueGetCount(phones); k++)                              \/\/ \u904d\u5386\u53d6\u503c\n        {\n            NSString * phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, k); \/\/ \u53d6\u51fa\u53f7\u7801\n            NSDictionary *dict = @{name : phone};                                           \/\/ \u62fc\u63a5\u5b57\u5178\n            [array addObject:dict];                                                         \/\/ \u52a0\u5165\u6570\u7ec4\n        }\n    }\n    \n    return array;\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u4e0a\u641c\u7d22\u4e86\u5f88\u591a\u76f8\u5173\u8d44\u6599\u90fd\u5199\u5f97\u8fc7\u4e8e\u590d\u6742\uff0c\u53ef\u6211\u4eec\u6700\u5e38\u7528\u7684\u53ea\u9700\u8981\u7528\u5230\u59d3\u540d\u4e0e\u7535\u8bdd\u53f7\u7801\uff0c\u6240\u4ee5\u6211\u6574\u7406\u4e86\u4e00\u4e0b\uff0c\u53ea\u9700\u8981\u4e00\u4e2a\u65b9\u6cd5\u641e\u5b9a\uff01\u6162\u6162\u770b\u5427 1\u3001\u5bfc\u5165AddressBook.framework\u5148\uff0c\u7136\u540e\u5f15\u7528 #import 2\u3001\u6388\u6743\uff0c\u5982\u679c\u5df2\u6210\u529f\uff0c\u8bfb\u53d6\u901a\u8baf\u5f55\u6570\u636e\u5230\u6570\u7ec4\u91cc\uff0c\u5426\u5219\u7a0b\u5e8f\u4f1a\u5d29\u6e83 3\u3001\u8bfb\u53d6\u65b9\u6cd5 &#8211; (NSArray *)getContacts { \/\/ 1\u3001\u521b\u5efa\u5bf9\u8c61 ABAddressBookRef tmpAddressBook = ABAddressBookCreateWithOptions(nil, nil); \/\/ 2\u3001\u83b7\u53d6\u901a\u8baf\u5f55 CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(tmpAddressBook); \/\/ 3\u3001\u904d\u5386\u901a\u8baf\u5f55 NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i < CFArrayGetCount(results); i++){ \/\/ 3.1\u3001\u83b7\u53d6\u5355\u6761\u8bb0\u5f55 ABRecordRef person = CFArrayGetValueAtIndex(results, i); \/\/ 3.2\u3001\u53d6\u8bb0\u5f55\u4e2d\u7684\u540d\u5b57 NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); NSString *name [&hellip;]\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[17],"class_list":["post-541","post","type-post","status-publish","format-standard","hentry","category-project","tag-skill"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=541"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/541\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}