iOS中的很多小功能都是非常簡單的,幾行代碼就搞定了,比如打電話、打開網(wǎng)址、發(fā)郵件、發(fā)短信等
打電話
第一種方式
NSURL *url = [NSURL URLWithString:@"tel://10010"]; // iOS 10以前直接跳到撥號界面,打完電話不會回到原應(yīng)用
NSURL *url = [NSURL URLWithString:@"telprompt://10010"];iOS 10以前在撥號之前會詢問用戶是否撥號,撥完后會回到原應(yīng)用
iOS 10以后,上述兩種方式相同,在撥號之前都會詢問用戶是否撥號,撥完號之后會回到原應(yīng)用
ios10之后 openURL:已廢棄,可用下面的方法替換,注意 options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} //參數(shù)是一個字典
NSURL *url = [NSURL URLWithString:@"tel://10010"];
[[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
// 成功回調(diào)
if(!success){
//失敗回調(diào)
UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"不能完成跳轉(zhuǎn)" message:@"請確認(rèn)App已經(jīng)安裝" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"確定"style:UIAlertActionStyleCancel handler:nil];
[aler addAction:cancelAction];
[self presentViewController:aler animated:YES completion:nil];
}else{
[self dismissViewControllerAnimated:YES completion:nil];
}
}];
第二種方式
創(chuàng)建一個UIWebView來加載url,撥打完之后會自動跳到原應(yīng)用
if (_webView == nil) {
_webView = [[UIWebView alloc] initWithFrame:CGRectZero];
//注意:這個webView千萬不要設(shè)置尺寸,不然會擋住其他界面,他只是用來打電話,不需要顯示
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];
發(fā)短信
第一種方法
直接跳到發(fā)短信的界面,但是不能指定短信的內(nèi)容而且不能返回原應(yīng)用
NSURL *url = [NSURL URLWithString:@"sms://10010"];
[[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
}];
第二種方式
若想指定短信內(nèi)容,那就得使用MessageUI框架
// 包含主頭文件
#import <MessageUI/MessageUI.h>
- (IBAction)sendMessageTwo {
// 顯示發(fā)短信的控制器
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
// 設(shè)置短信內(nèi)容
vc.body = @"Hello China Unicom ?";
// 設(shè)置收件人列表
vc.recipients = @[@"10010", @"02010010"];
// 設(shè)置代理,并遵守MFMessageComposeViewControllerDelegate協(xié)議
vc.messageComposeDelegate = self;
// 顯示控制器
[self presentViewController:vc animated:YES completion:nil];
}
#pragma mark MFMessageComposeViewControllerDelegate 的代理方法
/**
當(dāng)短信界面關(guān)閉的時候調(diào)用
@param controller 發(fā)送短信控制器
@param result 發(fā)送結(jié)果回調(diào)
*/
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
// 關(guān)閉短信界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MessageComposeResultCancelled) {
NSLog(@"取消發(fā)送");
} else if (result == MessageComposeResultSent) {
NSLog(@"已經(jīng)發(fā)出");
} else {
NSLog(@"發(fā)送失敗");
}
}
發(fā)郵件
第一種方法
使用自帶的郵件客戶端,發(fā)完之后不會回到原應(yīng)用
- (IBAction)sendAddressOne {
NSURL *url = [NSURL URLWithString:@"mailto://dengerxuan@163.com"];
[[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
if(success){
NSLog(@"發(fā)送成功");
}else{
NSLog(@"發(fā)送失敗");
}
}];
}
第二種方法
使用MessageUI框架
// 包含頭文件
#import <MessageUI/MessageUI.h>
#pragma mark - 在應(yīng)用內(nèi)發(fā)送郵件
//激活郵件功能
- (IBAction)sendAddressTwo {
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (!mailClass) {
[self alertWithMessage:@"當(dāng)前系統(tǒng)版本不支持應(yīng)用內(nèi)發(fā)送郵件功能,您可以使用mailto方法代替"];
return;
}
if (![mailClass canSendMail]) {
[self alertWithMessage:@"用戶沒有設(shè)置郵件賬戶"];
return;
}
[self displayMailPicker];
}
- (void)displayMailPicker{
// 發(fā)送郵件控制器
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
// 設(shè)置代理
mailPicker.mailComposeDelegate = self;
// 設(shè)置主題
[mailPicker setSubject: @"eMail主題"];
// 添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @"dengerxuan@163.com"];
[mailPicker setToRecipients: toRecipients];
// 添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"dengerxuan@163.com", @"1158035983@qq.com", nil];
[mailPicker setCcRecipients:ccRecipients];
// 添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"1158035983@qq.com", nil];
[mailPicker setBccRecipients:bccRecipients];
// 添加一張圖片
UIImage *addPic = [UIImage imageNamed: @"girl.png"];
NSData *imageData = UIImagePNGRepresentation(addPic); // png
// 關(guān)于mimeType:http://www.iana.org/assignments/media-types/index.html
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
// 添加一個pdf附件
NSString *file = [[NSBundle mainBundle] pathForResource:@"iOS開發(fā)進階(唐巧).pdf" ofType:nil];
// NSString *file = [self fullBundlePathFromRelativePath:@"iOS開發(fā)進階(唐巧).pdf"]; // 此方法廢棄
NSData *pdf = [NSData dataWithContentsOfFile:file];
[mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"iOS開發(fā)進階(唐巧).pdf"];
// 設(shè)置正文
NSString *emailBody = @"<font color='red'>eMail</font> 正文";
[mailPicker setMessageBody:emailBody isHTML:YES];
// [self presentModalViewController: mailPicker animated:YES];
[self presentViewController:mailPicker animated:YES completion:nil];
}
/**
抽取提示框彈出的方法
@param message 提示信息
*/
- (void)alertWithMessage:(NSString *)message{
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
[alertVc addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了確定按鈕");
}]];
[self presentViewController:alertVc animated:YES completion:nil] ;
}
#pragma mark MFMailComposeViewControllerDelegate 的代理方法
/**
郵件發(fā)送后的代理方法回調(diào)
@param controller 發(fā)送郵件的控制器
@param result 發(fā)送結(jié)果
@param error 發(fā)送失敗
*/
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// 關(guān)閉郵件界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MFMailComposeResultCancelled) {
NSLog(@"取消發(fā)送");
} else if (result == MFMailComposeResultSent) {
NSLog(@"已經(jīng)發(fā)出");
} else {
NSLog(@"發(fā)送失敗");
}
}
打開其他常見文件
如果想打開一些常見文件,比如html、txt、PDF、PPT等,都可以使用UIWebView打開
只需要告訴UIWebView文件的URL即可
至于打開一個遠程的共享資源,比如http協(xié)議的,也可以調(diào)用系統(tǒng)自帶的Safari瀏覽器:
NSURL *url = [NSURL URLWithString:@”http://www.baidu.com"];
[[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {}
應(yīng)用間跳轉(zhuǎn)
有時候需要在本應(yīng)用中打開其他的應(yīng)用,比如,從A應(yīng)用中跳到B應(yīng)用中
- 首先B應(yīng)用要有自己的URL地址(在B的Info.plist文件中配置)
abc.png
此時B的URL為 mj:// ios.itcast.com
- 接著在A應(yīng)用中使用UIApplication來完成跳轉(zhuǎn)
- (IBAction)openAnotherApp {
NSURL *url = [NSURL URLWithString:@"mj://ios.itcast.com"];
[[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
if(!success){
//失敗回調(diào)
UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"不能完成跳轉(zhuǎn)" message:@"請確認(rèn)App已經(jīng)安裝" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"確定"style:UIAlertActionStyleCancel handler:nil];
[aler addAction:cancelAction];
[self presentViewController:aler animated:YES completion:nil];
}else{
[self dismissViewControllerAnimated:YES completion:nil];
}
}];
}
應(yīng)用評分
為了提高應(yīng)用的用戶體驗,經(jīng)常需要邀請用戶對應(yīng)用進行評分,應(yīng)用評分無非就是跳轉(zhuǎn)到AppStore展示自己的應(yīng)用,然后由用戶自己撰寫評論,如何跳轉(zhuǎn)到AppStore,并且展示自己的應(yīng)用
NSString *appid = @"您app的appid”;
NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str ]options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
}