開發中常常會調用手機功能,今天來彙總一下,如有不足歡迎你們指出,下面分別介紹以下功能 :git
電話web
調用電話有下圖兩種不一樣樣式,相同的是,通話結束後均會返回你原界面
1- 直接跳至撥號界面
2- 先彈框提示,用戶確認後再跳至撥號界面瀏覽器
NSURL *url = [NSURL URLWithString:@"tel://10000000"]; [[UIApplication sharedApplication] openURL:url];
1- UIApplication打開URL服務器
NSURL *url = [NSURL URLWithString:@"telprompt://10000000"]; [[UIApplication sharedApplication] openURL:url];
2- UIWebView加載URLapp
//WebView若只實現打電話功能,能夠不設置尺寸,以防擋住其餘 UIWebView *_web; _web= [[UIWebView alloc] initWithFrame:CGRectZero]; //在須要調用的地方調用 [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10000000"]]];
短信框架
短信通常是服務器發
短信樣式同樣,都是直接跳至短信編輯界面,有兩種實現方式
1- UIApplication打開URL方式
跳至短信編輯頁面後,用戶手動編輯短信內容,完成後返回短信列表界面
缺點: 不能指定短信內容,不能自動回到原應用程序編碼
2- MFMessageComposeViewController方式
和方式1比:
能夠提早編輯好短信內容,跳至短信編輯界面時帶有內容
能夠羣發
完成後能夠返回原應用程序atom
NSURL *url = [NSURL URLWithString:@"sms://100000"]; [[UIApplication sharedApplication] openURL:url];
1.導入框架並實現協議 #import <MessageUI/MessageUI.h> @interface ViewController ()<MFMessageComposeViewControllerDelegate> 2.編輯短信內容,羣發對象,設置代理並彈出短信界面 MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init]; messageVC.body = @"你好,我是親愛的大倩倩"; messageVC.recipients = @[@"000000",@"111111",@"222222"]; messageVC.messageComposeDelegate = self; [self presentViewController:messageVC animated:YES completion:nil]; 3.實現代理:短信發完後的回調,在此方法中設置返回原應用程序 參數1: 短信控制器 參數2:短信發送結果 - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [controller dismissViewControllerAnimated:YES completion:nil]; NSString *messageResult; if (result == MessageComposeResultCancelled) messageResult = @"短信取消發送"; else if(result == MessageComposeResultSent) messageResult = @"短信已發送"; else messageResult = @"短信發送失敗!"; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:messageResult message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *myAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:myAction]; [self presentViewController:alertController animated:YES completion:nil]; }
郵件url
郵件有兩種實現方式:
1- UIApplication打開URL方式
不可提早編輯,發送後不會回到原應用程序
2- MFMailComposeViewController方式
可提早編輯,可羣發,可帶圖片,附件,視頻等,發送後退回原應用程序spa
NSURL *url = [NSURL URLWithString:@"mailto://0000000@qq.com"]; [[UIApplication sharedApplication] openURL:url];
1.導入框架並實現協議 #import <MessageUI/MessageUI.h> @interface ViewController ()<MFMailComposeViewControllerDelegate> //在觸發發送郵件的方法中設置2,3,4步 2.先判斷是否開啓了郵箱權限 if (![MFMailComposeViewController canSendMail]) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"不能發送郵件" message:@"請檢查郵箱設置" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *myAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:myAction]; [self presentViewController:alertController animated:YES completion:nil]; return; } 3.聲明MFMailComposeViewController對象,設置代理及其餘屬性 MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init]; mailVC.mailComposeDelegate = self; //設置收件人 [mailVC setToRecipients:@[@"000000@qq.com",@"111111@qq.com"]]; // //添加抄送及密送 // NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; // [mailVC setCcRecipients:ccRecipients]; // NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil]; // [mailVC setBccRecipients:bccRecipients]; //設置主題 [mailVC setSubject:@"全體通知"]; //添加郵件正文 [mailVC setMessageBody:@"今天16:00辦公室停電,你們提早下班吧" isHTML:NO]; //添加照片 UIImage *addPic = [UIImage imageNamed:@"icon_star_full@2x.png"]; NSData *imageData = UIImagePNGRepresentation(addPic); [mailVC addAttachmentData:imageData mimeType:@"" fileName:@"icon_star_full.png"]; //還能夠添加pdf文件及視頻 4.跳轉界面 [self presentViewController:mailVC animated:YES completion:nil]; 5.實現代理 - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { [controller dismissViewControllerAnimated:YES completion:nil]; NSString *mailResult; switch (result) { case MFMailComposeResultCancelled: mailResult = @"用戶取消編輯郵件"; break; case MFMailComposeResultSaved: mailResult = @"用戶成功保存郵件"; break; case MFMailComposeResultSent: mailResult = @"用戶點擊發送,將郵件放到隊列中,還沒發送"; break; case MFMailComposeResultFailed: mailResult = @"用戶試圖保存或者發送郵件失敗"; break; default: mailResult = @""; break; } UIAlertController *alertController = [UIAlertController alertControllerWithTitle:mailResult message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *myAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:myAction]; [self presentViewController:alertController animated:YES completion:nil]; }
郵件這個按鈕要打開,否則沒法發送
QQ網頁版收到的郵件以下圖:
通信錄
使用AddressBook和AddressBookUI框架實現
1- 導入框架
AddressBook.framework和AddressBookUI.framework
2- 導入頭文件
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>,
3- 實現協議並跳轉通信錄界面
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate> //在按鈕點擊事件中跳轉 ABPeoplePickerNavigationController *peopleVC = [[ABPeoplePickerNavigationController alloc] init]; peopleVC.peoplePickerDelegate = self; [self presentViewController:peopleVC animated:YES completion:nil];
4- 有不少代理方法,不一一闡述了
定位
使用CLLocationManager來實現定位
1- 導入CoreLocation.framework框架
2- 導入頭文件,實現協議
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
3- iOS8以上須要在Info.Plist文件中添加以下配置
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
4- 聲明CLLocationManager對象,開啓定位
@property (nonatomic, strong) CLLocationManager *locationManager; - (void)viewDidLoad { _locationManager=[[CLLocationManager alloc] init]; _locationManager.delegate=self; //多少米定位一次 // _locationManager.desiredAccuracy = 0; // _locationManager.distanceFilter = 500; //初次打開時會有彈框提示,是否容許定位 if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [_locationManager requestWhenInUseAuthorization]; } //開啓定位 [_locationManager startUpdatingLocation]; }
5- 實現代理方法,會返給你地理位置信息,須要本身解碼
1.聲明字典,用於接收解碼後的信息 @interface ViewController ()<CLLocationManagerDelegate> { NSDictionary *_addressDic; } 2.實現代理 #pragma mark - 位置信息更新後,獲取經緯度的代理方法 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { NSLog(@"定位成功"); CLLocation *location = [locations lastObject]; CLLocationCoordinate2D coordinate2D = location.coordinate; NSLog(@"經緯度爲----%f----%f",coordinate2D.latitude,coordinate2D.longitude); [_locationManager stopUpdatingLocation]; // 反編碼對象 CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *placemark = [placemarks lastObject]; _addressDic = placemark.addressDictionary; NSLog(@"地理位置信息:%@",_addressDic); NSString *cityStr = _addressDic[@"City"]; NSLog(@"city:%@",_addressDic[@"City"]); NSArray *arr = [cityStr componentsSeparatedByString:@"市"]; cityStr = [arr firstObject]; NSLog(@"截取的值爲:%@",cityStr); }]; }
跳轉應用
使用UIApplication打開URL的方法
跳轉應用就是在應用A中,某些操做後跳轉至應用B,拿個人兩個現有的應用舉例,一個應用名字爲"手機功能",就是寫這篇文章的Demo,另外一個應用名字爲"FQMusicPlayer",如今實現點擊"手機功能"界面中的button時,跳轉至"FQMusicPlayer"
1- 須要配置"FQMusicPlayer"的url地址
2- "手機功能"跳至這個url地址便可
也可配置下圖,跳轉時須要跳轉@"fq://iOS.cn"
NSURL *url = [NSURL URLWithString:@"fq://iOS.cn"]; [[UIApplication sharedApplication] openURL:url];
備註 : 若是跳轉時,是新打開"FQMusicPlayer",會調用didFinishLaunchingWithOptions方法,若它以前在後臺運行,不會調用此方法
若是一個應用被另一個應用打開,會調用下面的代理方法,能夠在該方法中能夠實現兩個應用之間數據的傳遞
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { NSLog(@"%@,%@",url,sourceApplication); return YES; }
跳轉至App Store
使用UIApplication打開URL方法
1- 首先拿到你要跳轉的App Store地址(url),例如咱們如今跳轉至節奏大師,它的地址是https://itunes.apple.com/cn/app/jie-zou-da-shi/id493901993?mt=8
2- 將 http:// 替換爲 itms:// 或者 itms-apps://,再調用代碼便可
NSString *str = @"itms://itunes.apple.com/cn/app/jie-zou-da-shi/id493901993?mt=8"; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
打開其它文件
拿pdf舉例
拿百度網址舉例吧,沒找到遠程的pdf文件 1.UIApplication打開URL NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; [[UIApplication sharedApplication] openURL:url]; 2.UIWebView打開URL NSURL *targetURL = [NSURL URLWithString:@"http://www.baidu.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [_web loadRequest:request];
訪問網址還須要配置下圖
1- 如果真機,將文件直接拖進來
2- 如果模擬器,打印你的沙盒路徑,打開Finder,command + shift + G,將文件放進去便可
NSString *path = [[NSBundle mainBundle] pathForResource:@"Swift" ofType:@"pdf"]; NSURL *url =[NSURL fileURLWithPath:path]; NSURLRequest*request =[NSURLRequest requestWithURL:url]; [_web loadRequest:request];