导出 本地电话簿(通讯簿) ios address book

导出 本地电话簿(通讯簿) ios address book,第1张

概述Cocoa Touch Tutorial: Extract Address Book Address Values on iPhone OS This is the first of what I hope to be several Cocoa Touch tutorials on this site.  I was doing some furious Googling last night Cocoa touch Tutorial: Extract Address Book Address Values on iPhone OS

This is the first of what I hope to be several Cocoa touch tutorials on this site.  I was doing some furIoUs Googling last night trying to find out how to get a contact’s street address from the Address Book for an upcoming update to Take Me Home,and I realized that it’s complicated and there aren’t any good tutorials online.  So,after I figured it out,I commented it up so that hopefully,if you’re reading this,you’ll save some time that I dIDn’t.

Before you read this tutorial,you should go through Apple’s excellent Address Book Programming Guide for iPhone OS.  This tutorial will rely on the QuickStart application you write in the guIDe,so do that first.
The first thing we need to do is add an address fIEld to the QuickStart application.  Use Interface Builder to add a new UILabel underneath the two you already have.  You may want to stretch it to fill the entire wIDth of the screen,like so:

Add a new UILabel underneath the exisiting two.

Now,add the information about this label to QuickStartVIEwController.h:

////  QuickStartVIEwController.h//  QuickStart//#import <UIKit/UIKit.h>#import <AddressBook/AddressBook.h>#import <AddressBookUI/AddressBookUI.h>@interface QuickStartVIEwController : UIVIEwController <ABPeoplePickerNavigationControllerDelegate> {    IBOutlet UILabel *firstname;    IBOutlet UILabel *lastname;    IBOutlet UILabel *addressLabel;}@property (nonatomic,retain) UILabel *firstname;@property (nonatomic,retain) UILabel *lastname;@property (nonatomic,retain) UILabel *addressLabel;- (IBAction)showPicker:(ID)sender;@end

Be sure to go back into Interface Builder and connect file’s Owner in QuickStartVIEwController.xib to addressLabel.

Now,we have to change the method that gets called when you click on a person in the ABPeoplePicker.  As it is at the end of the QuickStart tutorial,once you select a person the picker is dismissed.  So,we do the following in QuickStartVIEwController.m:

- (BOol)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { Nsstring *name = (Nsstring *)ABRecordcopyValue(person,kABPersonFirstnameProperty); self.firstname.text = name; [name release]; name = (Nsstring *)ABRecordcopyValue(person,kABPersonLastnameProperty); self.lastname.text = name; [name release]; [self dissmissModalVIEwControllerAnimated:YES]; return YES;}

Note that you have to delete the line that dismisses the modal vIEw controller; if you don’t,the people picker is dismissed before you have a chance to get the address. When you delete it,the people picker will continue when you select a person. Next up,we have to write the method for when someone selects an address on the next screen. Here’s the method:

- (BOol)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property IDentifIEr:(ABMultiValueIDentifIEr)IDentifIEr { // Only inspect the value if it's an address. if (property == kABPersonAddressproperty) { /* * Set up an ABMultiValue to hold the address values; copy from address * book record. */ ABMultiValueRef multi = ABRecordcopyValue(person,property); // Set up an NSArray and copy the values in. NSArray *theArray = [(ID)ABMultiValuecopyArrayOfAllValues(multi) autorelease]; // figure out which values we want and store the index. const NSUInteger theIndex = ABMultiValueGetIndexForIDentifIEr(multi,IDentifIEr); // Set up an NSDictionary to hold the contents of the array. NSDictionary *theDict = [theArray objectAtIndex:theIndex]; // Set up Nsstrings to hold keys and values. First,how many are there? const NSUInteger theCount = [theDict count]; Nsstring *keys[theCount]; Nsstring *values[theCount]; // Get the keys and values from the CFDictionary. Note that because // we're using the "GetKeysAndValues" function,you don't need to // release keys or values. It's the "Get Rule" and only applIEs to // CoreFoundation objects. [theDict getobjects:values andKeys:keys]; // Set the address label's text. Nsstring *address; address = [Nsstring stringWithFormat:@"%@,%@,%@ %@",[theDict objectForKey:(Nsstring *)kABPersonAddressstreetKey],[theDict objectForKey:(Nsstring *)kABPersonAddressCityKey],[theDict objectForKey:(Nsstring *)kABPersonAddressstateKey],[theDict objectForKey:(Nsstring *)kABPersonAddressZIPKey],[theDict objectForKey:(Nsstring *)kABPersonAddressCountryKey]]; self.addressLabel.text = address; // Return to the main vIEw controller. [ self dismissModalVIEwControllerAnimated:YES ]; return NO; } // If they dIDn't pick an address,return YES here to keep going. return YES; }

Let’s go through that in more detail.  The method gives us the following information: an ABRecordRef of the person we’ve selected,an ABPropertyID of the property slected (in this case,we ensure that it’s the address) and an ABMultiValueIDentifIEr of which address we’ve selected.  It is important to note that theABPropertyID is equal to kABPersonAddressproperty when you select any address; that is,there is only one address property. This one address property holds the values in an ABMultiValue,each at a specific index.  Here are the steps we take in the code:

The first thing we do is define our ABMultiValue,multi,and copy the contents of the selected value into it. Then we define an NSArray, theArray,into which to copy the multiple values.  But which one do we want? Each address has an IDentifIEr,which the method gives to us as IDentifIEr,but we reference them by index when getting them out of the array.  So,we need to create an index (which we’ll store as an unsigned integer), theIndex,and set it to the return value of theABMultiValueGetIndexForIDentifIEr function.  Now that we have the index,we kNow which value of the array to store .  They’re stored as type CFDictionary,which have key-value pairs for us to use,so we define an NSDictionary, theDict to put them into. First,we need to kNow how many key-value pairs there are,so we use the count method and store the return value in an unsigned integer, theCount.  Be sure that this variable doesn&rquo;t change—you don’t want to assume that there are more members in the array than there actually are,as that can lead to nasty memory problems. For that reason I’ve defined it as a constant. Now,we define two Nsstring arrays, keys[theCount] and values[theCount],and then we’re ready for action. Next we use the NSDictionary getobjects: andKeys: function to copy the keys and values. The function copIEs the data,and we can construct our street address.  For the purpose of this example,I’m going to make the address a single line,but you do with it what you want. Finally,we create a final Nsstring to put the formatted address into,pull the values out of the dictionary into the appropriate place,and we’re all done! 总结

以上是内存溢出为你收集整理的导出 本地电话簿(通讯簿) ios address book全部内容,希望文章能够帮你解决导出 本地电话簿(通讯簿) ios address book所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1057193.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存