在程序中集成地址簿、电子邮件和地图功能
地址簿
iOS通過兩個(gè)框架提供了全面的地址簿數(shù)據(jù)庫訪問功能:Address Book和Address Book UI。
Address Book UI框架是一組用戶界面類,封裝了Address Book框架,并向用戶提供了使用聯(lián)系人信息的標(biāo)準(zhǔn)方式。通過使用Address Book UI框架的界面,用戶可以在地址薄中瀏覽、搜索和選擇聯(lián)系人,顯示并編輯選定聯(lián)系人的信息,以及創(chuàng)建新的聯(lián)系人。
要使用框架Address Book UI,需要將其加入到項(xiàng)目中,并導(dǎo)入其接口文件:
#import <AddressBook/AddressBook.h>ABPeoplePickerNavigationController類提供一個(gè)顯示地址薄UI的視圖控制器,使用方法如下:
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];picker.peoplePickerDelegate = self;[self presentViewController:picker animated:YES completion:nil];顯示地址簿后,需要處理用戶的選擇;主要通過實(shí)現(xiàn)委托ABPeoplePickerNavigationControllerDelegate的三個(gè)方法:
-?(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker-?(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person-?(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier第一個(gè)方法處理用戶的取消操作。
第二個(gè)方法處理用戶的第一次選擇,在這個(gè)方法里可以獲取用戶的所有信息集合;當(dāng)這個(gè)方法返回YES,將允許用戶進(jìn)一步選擇。
第三個(gè)方法處理用戶的第二次選擇,在這個(gè)方法里可以獲取用戶選擇的單項(xiàng);當(dāng)這個(gè)方法返回YES,將允許用戶進(jìn)一步選擇。
在項(xiàng)目中使用地址簿示例:
#import <UIKit/UIKit.h> #import <AddressBook/AddressBook.h> #import <AddressBookUI/ABPeoplePickerNavigationController.h>@interface ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>@end?
#import "ViewController.h"@interface ViewController () - (IBAction)btnTestClickHandler:(id)sender;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad]; }- (IBAction)btnTestClickHandler:(id)sender {ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];picker.peoplePickerDelegate = self;[self presentViewController:picker animated:YES completion:nil]; }//取消選擇 - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {[self dismissViewControllerAnimated:YES completion:nil]; }//一次觸發(fā) - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {//first nameNSString *strFirstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);NSLog(@"first name: %@",strFirstName);//emailABMultiValueRef emailAddresses = ABRecordCopyValue(person, kABPersonEmailProperty);if(ABMultiValueGetCount(emailAddresses)>1){NSString *strSecondEmail = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emailAddresses, 1);NSLog(@"第二個(gè)Email: %@",strSecondEmail);}//phone numberABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);NSString *strPhone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);NSLog(@"電話號(hào)碼:%@",strPhone);return YES; }//二次觸發(fā) - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {if(property == kABPersonPhoneProperty){NSLog(@"你選擇的是電話號(hào)碼");}ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, property);for(CFIndex i=0; i<ABMultiValueGetCount(phoneNumbers); i++){if(identifier == ABMultiValueGetIdentifierAtIndex(phoneNumbers, i)){NSString *strPhone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers, i);NSLog(@"你選擇的內(nèi)容:%@",strPhone);}}return NO; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end?
電子郵件
在項(xiàng)目里使用框架Message UI可以很方便的發(fā)送電子郵件,只需要初始化一個(gè)MFMailComposeViewController對(duì)象來顯示發(fā)郵件的界面就可以了,具體發(fā)送操作由iOS自動(dòng)完成。
需要遵守一個(gè)協(xié)議:MFMailComposeViewControllerDelegate。該協(xié)議定義了一個(gè)清理方法:mailComposeController:didFinishWithResult:error,將在用戶使用完郵件書寫窗口后被調(diào)用。在大多數(shù)情況下,這個(gè)方法只需關(guān)閉郵件書寫視圖控制器即可。如果想進(jìn)一步獲悉郵件書寫視圖關(guān)閉的原因,可查看result(其類型為MFMailComposeResult)的值。其取值為下述常量之一:
MFMailComposeResultCancelled、MFMailComposeResultSaved、MFMailComposeResultSent和MFMailComposeResultFailed。
在項(xiàng)目中發(fā)送電子郵件示例:
#import <UIKit/UIKit.h> #import <MessageUI/MessageUI.h>@interface ViewController : UIViewController <UINavigationControllerDelegate, MFMailComposeViewControllerDelegate>@end?
#import "ViewController.h"@interface ViewController () - (IBAction)btnTestClickHandler:(id)sender; @end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad]; }- (IBAction)btnTestClickHandler:(id)sender {MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];NSArray *emailAddresses = [[NSArray alloc] initWithObjects:@"me@qq.com", nil];mailComposer.delegate = self;[mailComposer setToRecipients:emailAddresses];[self presentViewController:mailComposer animated:YES completion:nil]; }- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {switch (result){case MFMailComposeResultCancelled:NSLog(@"Mail cancelled");break;case MFMailComposeResultSaved:NSLog(@"Mail saved");break;case MFMailComposeResultSent:NSLog(@"Mail sent");break;case MFMailComposeResultFailed:NSLog(@"Mail sent failure: %@", [error localizedDescription]);break;default:break;}// Close the Mail Interface [self dismissViewControllerAnimated:YES completion:nil]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end?
地圖功能
通過使用iOS的Map Kit,可以將地圖嵌入到視圖中,并提供顯示該地圖所需的所有圖塊(圖像)。它在需要時(shí)處理滾動(dòng)、縮放和圖塊加載。Map Kit還能執(zhí)行反向地理編碼(reverse geocoding),即根據(jù)坐標(biāo)獲取位置信息(國家、州、城市、地址)。
無需編寫任何代碼就可使用Map Kit — 只需在Interface Builder將一個(gè)MKMapView實(shí)例加入到視圖中,并可在Attributes Inspector中設(shè)置多個(gè)屬性以進(jìn)一步定制它。記得需要添加鏈接庫MapKit.framework。
如果要以編程方式控制地圖,首先需要導(dǎo)入框架Map Kit的接口文件:
#import <MapKit/MapKit.h>需要操縱地圖時(shí),大多數(shù)情況下都需要添加框架Core Location并導(dǎo)入其接口文件:
#import <CoreLocation/CoreLocation.h>還需要定義一個(gè)地圖區(qū)域。區(qū)域(region)是一個(gè)MKCoordinateRegion結(jié)構(gòu)(而不是對(duì)象),它包含成員center和span。
center — CLLocationCoordinate2D結(jié)構(gòu),包含成員longitude(經(jīng)度)和latitude(緯度)。
span — 指定從中心出發(fā)向東西南北延伸多少度。一個(gè)緯度相當(dāng)于69英里;在赤道上,一個(gè)經(jīng)度也相當(dāng)于69英里。通過將區(qū)域的跨度(span)設(shè)置為較小的值,如0.2,可將地圖的覆蓋范圍縮小到繞重點(diǎn)幾英里。
MKCoordinateRegion mapRegion; mapRegion.center.longitude = 90.0; mapRegion.center.latitude = 33.0; mapRegion.span.longitudeDelta = 0.2; mapRegion.span.latitudeDelta = 0.2; [self.myMap setRegion:mapRegion animated:YES];在應(yīng)用程序中可以給地圖添加標(biāo)注,通常需要實(shí)現(xiàn)一個(gè)MKAnnotaion View子類,它描述了標(biāo)注的外觀以及應(yīng)顯示的信息。
對(duì)于加入到地圖中的每個(gè)標(biāo)注,都需要一個(gè)描述其位置的地點(diǎn)標(biāo)識(shí)對(duì)象(MKPlaceMark)。
//設(shè)置標(biāo)記 CLLocationCoordinate2D myCoordinate; myCoordinate.longitude = 90.0; myCoordinate.latitude = 33.0;MKPlacemark *myMarker = [[MKPlacemark alloc] initWithCoordinate:myCoordinate addressDictionary:self.myAddress]; [self.myMap addAnnotation:myMarker]; //刪除標(biāo)記 //[self.myMap removeAnnotation:myMarker];上面的代碼的myAddress來自@property (nonatomic, readonly) NSDictionary *myAddress;這里只簡(jiǎn)單的聲明了引用,實(shí)際應(yīng)用中可能是從地址簿條目中獲取的(通過郵編得到經(jīng)緯度),也可能是根據(jù)ASPerson參考文檔中Address屬性的定義手工創(chuàng)建的。
添加標(biāo)注時(shí),iOS自動(dòng)完成其他工作。Apple提供了一個(gè)MKAnnotationView子類—MKPinAnnotationView。當(dāng)您添加標(biāo)記時(shí),iOS自動(dòng)創(chuàng)建一個(gè)MKPinAnnotationView實(shí)例(一顆圖釘),如果要定制圖釘,必須實(shí)現(xiàn)地圖視圖的委托方法mapView:viewForAnnotation:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myspot"];pin.animatesDrop = YES; //是否以動(dòng)畫方式出現(xiàn)pin.canShowCallout = YES; //觸摸時(shí),是否能顯示額外信息pin.pinColor = MKPinAnnotationColorPurple; //圖標(biāo)顏色return pin; }需要設(shè)置MKMapView實(shí)例的delegate屬性才能生效。
轉(zhuǎn)載于:https://www.cnblogs.com/CoderWayne/p/3780394.html
總結(jié)
以上是生活随笔為你收集整理的在程序中集成地址簿、电子邮件和地图功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 旅游风景展示应用源码iPad版
- 下一篇: c#中页面之间传值传参的六种方法