在APP開發(fā)中,可能會(huì)涉及到打電話袖瞻、發(fā)短信忠寻、發(fā)郵件等功能。
1.撥打電話有三種方式:
第一種1.1: 直接撥打,跳轉(zhuǎn)到撥號(hào)界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", @“你所需要撥打的電話號(hào)碼”]]];
第二種1.2: 提示撥打(蘋果原生自帶)
撥號(hào)之前會(huì)彈框詢問瑟蜈,打完電話后能自動(dòng)回到原應(yīng)用。
提示撥打電話效果圖.png
1.2.1渣窜、 創(chuàng)建一個(gè)UIWebView來加載URL铺根,撥完后能自動(dòng)回到原應(yīng)用。建議使用這種方案乔宿!
// 提示撥打方法1:
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@", @"你所需要撥打的電話號(hào)碼"];
UIWebView * callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[self.view addSubview:callWebview];
1.2.2位迂、 缺點(diǎn):私有API,因此可能通不過蘋果官方審核。如果是企業(yè)級(jí)應(yīng)用(不需要上線appStore)掂林,可以使用這個(gè)方法臣缀。
// 提示撥打方法2:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@", @"你所需要撥打的電話號(hào)碼"]];
// 調(diào)用openURL:
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
第三種1.3: 彈框提示撥打(根據(jù)UI設(shè)計(jì)界面修改)
需要封裝自定義彈框文件: QTXAlterView
@property (nonatomic, strong) QTXAlterView *telAlterView; // 撥打電話的彈框
QTXAlterView *alter = [[QTXAlterView alloc] initWithMessage:[NSString stringWithFormat:@"你所需要撥打的電話號(hào)碼"] delegate:self rightButtonTitle:@"確定" otherButtonTitles:@"取消"];
[alter show];
self.telAlterView = alter;
#pragma mark - QTXAlterViewDelegate
- (void)alertView:(QTXAlterView *)alertView clickedAtIndex:(NSInteger)buttonIndex {
if (alertView == self.telAlterView) { // 撥打電話
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", @“你所需要撥打的電話號(hào)碼”]]];
}
}
}
2.發(fā)短信的方式有兩種:
(1)直接跳轉(zhuǎn)到發(fā)短信界面。
缺點(diǎn):不能定義發(fā)送短信的內(nèi)容泻帮,且發(fā)完短信后不能自動(dòng)回到原應(yīng)用精置。
NSURL *url = [NSURL URLWithString:@"sms://10010"];[[UIApplication sharedApplication] openURL:url];
(2)使用MessageUI 框架發(fā)送短信, 建議使用第二種方法。
需要包含頭文件 #import <MessageUI/MessageUI.h>
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc]init];
//設(shè)置短信內(nèi)容
vc.body = @"吃飯了沒";
//設(shè)置收件人列表
vc.recipients = @[@"10010",@"10086"];
//設(shè)置代理
vc.messageComposeDelegate = self;
//顯示控制器
[self presentViewController:vc animated:YES completion:nil];
// 實(shí)現(xiàn)代理函數(shù): 點(diǎn)擊取消按鈕會(huì)自動(dòng)調(diào)用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[controller dismissViewControllerAnimated:YES completion:nil];
}
3.發(fā)郵件的方式有兩種:
(1)用自帶的郵件客戶端.
缺點(diǎn):發(fā)完郵件后不會(huì)自動(dòng)回到原應(yīng)用
NSURL *url = [NSURL URLWithString:@"mailto://test@qq.com"];[[UIApplication sharedApplication] openURL:url];
(2)類似于發(fā)短信的第二種方法锣杂,使用MessageUI脂倦,
if(![MFMailComposeViewController canSendMail]) return;
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
//設(shè)置郵件主題
[vc setSubject:@"測(cè)試郵件"];
//設(shè)置郵件內(nèi)容
[vc setMessageBody:@"測(cè)試內(nèi)容" isHTML:NO];
//設(shè)置收件人列表
[vc setToRecipients:@[@"test@qq.com"]];
//設(shè)置抄送人列表
[vc setCcRecipients:@[@"test1@qq.com"]];
//設(shè)置代理
vc.mailComposeDelegate = self;
//顯示控制器
[self presentViewController:vc animated:YES completion:nil];
// 實(shí)現(xiàn)代理方法:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[controller dismissViewControllerAnimated:YES completion:nil];
}