{"id":324,"date":"2014-08-22T11:27:03","date_gmt":"2014-08-22T03:27:03","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=324"},"modified":"2016-07-15T17:25:07","modified_gmt":"2016-07-15T09:25:07","slug":"ios%e8%8e%b7%e5%8f%96%e7%b3%bb%e7%bb%9f%e9%80%9a%e8%ae%af%e5%bd%95%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/08\/22\/112703324.html","title":{"rendered":"ios\u83b7\u53d6\u7cfb\u7edf\u901a\u8baf\u5f55\u65b9\u6cd5"},"content":{"rendered":"<p>\u7f51\u4e0a\u76f8\u5173\u8d34\u5b50\u5f88\u591a\uff0c\u4f46\u57fa\u672c\u4e0a\u90fd\u6765\u81ea\u540c\u4e00\u4e2a\u51fa\u5904\uff0c\u57fa\u7840\u4e0d\u597d\u7684\u540c\u5b66\u53ef\u80fd\u770b\u6765\u8d77\u4f1a\u5f88\u5403\u529b\uff0c\u6211\u4eca\u5929\u6574\u7406\u4e86\u4e00\u4e0b\uff0c\u5316\u7e41\u4e3a\u7b80\u53ea\u63d0\u53d6\u901a\u8baf\u5f55\u4e2d\u59d3\u540d\u4e0e\u7535\u8bdd\u53f7\u7801\uff0c\u7ed9\u51fa\u6700\u7b80\u4fbf\u7684\u65b9\u4fbf\u4f9b\u5927\u5bb6\u53c2\u8003<\/p>\n<p>\u4e3a\u65b9\u4fbf\u5927\u5bb6\u8c03\u7528\uff0c\u6211\u5199\u6210\u4e86\u8fd4\u56de\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u76f4\u63a5copy\u5230\u4f60\u7684\u4ee3\u7801\u4e2d\u5373\u53ef\u53d6\u5f97\u8054\u7cfb\u4eba\u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u5e9f\u8bdd\u4e0d\u591a\u8bf4\u76f4\u63a5\u4e0a\u4ee3\u7801\u5427\uff0c\u6570\u636e\u6a21\u578b\u53ef\u81ea\u5b9a\u4e49\uff0c\u53ea\u8981\u5305\u542b\u59d3\u540d\u3001\u53f7\u7801\u4e24\u4e2a\u6210\u5458\u53d8\u91cf\u5373\u53ef\uff0c\u6211\u8fd9\u91cc\u5c31\u4e0d\u7c98\u4e86\uff0c\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u5427\uff0c\u5982\u6709\u9700\u8981\u53ef\u56de\u590d<\/p>\n<pre lang=\"objc\" line=\"1\">- (NSArray *)getContacts:(ABAddressBookRef)adress\n{\n    \/\/ 1\u3001\u83b7\u53d6\u901a\u8baf\u5f55\n    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(adress);\n\n    \/\/ 2\u3001\u904d\u5386\u901a\u8baf\u5f55\n    NSMutableArray *array = [NSMutableArray array];\n    for (int i = 0; i < CFArrayGetCount(results); i++){\n\n        \/\/ 2.1\u3001\u83b7\u53d6\u5355\u6761\u8bb0\u5f55\n        ABRecordRef person = CFArrayGetValueAtIndex(results, i);\n\n        \/\/ 2.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        \/\/ 2.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            NSString * phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, k); \/\/ \u53d6\u51fa\u53f7\u7801\n            RMContact *contact = [RMContact contactWithName:name phone:phone];\n            [array addObject:contact];                                                      \/\/ \u52a0\u5165\u6570\u7ec4\n        }\n    }\n    return array;\n}<\/pre>\n<p><!--more--><br \/>\n\u5176\u4e2d\uff1a<strong>RMContact *contact = [RMContact contactWithName:name phone:phone];<\/strong>\u4e3a\u81ea\u5b9a\u4e49\u6a21\u578b\uff0c\u6839\u636e\u9700\u6c42\u81ea\u5df1\u53bb\u5b9e\u73b0\u3002<br \/>\n\u65b9\u4fbfCopy\u5199\u6210\u4ee3\u7801\u5757<\/p>\n<pre lang=\"objc\" line=\"1\">- (NSArray *)getContacts:(ABAddressBookRef)adress\n{\n????\/\/ 1\u3001\u83b7\u53d6\u901a\u8baf\u5f55\n????CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(adress);\n?????\n????\/\/ 2\u3001\u904d\u5386\u901a\u8baf\u5f55\n????NSMutableArray *array = [NSMutableArray array];\n????for (int i = 0; i < CFArrayGetCount(results); i++){\n?????????\n????????\/\/ 2.1\u3001\u83b7\u53d6\u5355\u6761\u8bb0\u5f55\n????????ABRecordRef person = CFArrayGetValueAtIndex(results, i);\n?????????\n????????\/\/ 2.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????????\/\/ 2.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????????????NSString * phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, k); \/\/ \u53d6\u51fa\u53f7\u7801\n????????????RMLocalContact *contact = [RMLocalContact contactWithName:name phone:phone];\n????????????[array addObject:contact];????????????????????????????????????????????????????? \/\/ \u52a0\u5165\u6570\u7ec4\n????????}\n????}\n????_contacts = [NSArray arrayWithArray:array];\n?????\n????return array;\n}<\/pre>\n<p><strong>Demo\uff1a<\/strong><a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/Demo.zip\">Demo<\/a><\/p>\n<p>\u76f8\u5f53\u94fe\u63a5\uff1a<a href=\"http:\/\/www.yusian.com\/blog\/project\/2014\/08\/20\/113351.html\" target=\"_blank\">ios\u5f00\u53d1\u83b7\u53d6\u624b\u673a\u901a\u8baf\u5f55\uff08\u6700\u7b80\u5355\u660e\u4e86\uff09<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u4e0a\u76f8\u5173\u8d34\u5b50\u5f88\u591a\uff0c\u4f46\u57fa\u672c\u4e0a\u90fd\u6765\u81ea\u540c\u4e00\u4e2a\u51fa\u5904\uff0c\u57fa\u7840\u4e0d\u597d\u7684\u540c\u5b66\u53ef\u80fd\u770b\u6765\u8d77\u4f1a\u5f88\u5403\u529b\uff0c\u6211\u4eca\u5929\u6574\u7406\u4e86\u4e00\u4e0b\uff0c\u5316\u7e41\u4e3a\u7b80\u53ea\u63d0\u53d6\u901a\u8baf\u5f55\u4e2d\u59d3\u540d\u4e0e\u7535\u8bdd\u53f7\u7801\uff0c\u7ed9\u51fa\u6700\u7b80\u4fbf\u7684\u65b9\u4fbf\u4f9b\u5927\u5bb6\u53c2\u8003 \u4e3a\u65b9\u4fbf\u5927\u5bb6\u8c03\u7528\uff0c\u6211\u5199\u6210\u4e86\u8fd4\u56de\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u76f4\u63a5copy\u5230\u4f60\u7684\u4ee3\u7801\u4e2d\u5373\u53ef\u53d6\u5f97\u8054\u7cfb\u4eba\u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u5e9f\u8bdd\u4e0d\u591a\u8bf4\u76f4\u63a5\u4e0a\u4ee3\u7801\u5427\uff0c\u6570\u636e\u6a21\u578b\u53ef\u81ea\u5b9a\u4e49\uff0c\u53ea\u8981\u5305\u542b\u59d3\u540d\u3001\u53f7\u7801\u4e24\u4e2a\u6210\u5458\u53d8\u91cf\u5373\u53ef\uff0c\u6211\u8fd9\u91cc\u5c31\u4e0d\u7c98\u4e86\uff0c\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u5427\uff0c\u5982\u6709\u9700\u8981\u53ef\u56de\u590d &#8211; (NSArray *)getContacts:(ABAddressBookRef)adress { \/\/ 1\u3001\u83b7\u53d6\u901a\u8baf\u5f55 CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(adress); \/\/ 2\u3001\u904d\u5386\u901a\u8baf\u5f55 NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i < CFArrayGetCount(results); i++){ \/\/ 2.1\u3001\u83b7\u53d6\u5355\u6761\u8bb0\u5f55 ABRecordRef person = CFArrayGetValueAtIndex(results, i); \/\/ 2.2\u3001\u53d6\u8bb0\u5f55\u4e2d\u7684\u540d\u5b57 NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); NSString *name = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty); if (firstName) name = [name stringByAppendingString:firstName]; [&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":[20],"class_list":["post-324","post","type-post","status-publish","format-standard","hentry","category-project","tag-basics"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/324","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=324"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}