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