打电话、发短信
电话、短信是手机的基础功能,iOS中提供了接口,让我们调用。这篇文章简单的介绍一下iOS的打电话、发短信在程序中怎么调用。
1、打电话
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];//打电话
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];//打电话
使用openURL这个API打电话结束后,返回的是系统的拨打电话界面,如何才能返回自己的应用呢?有两种方法与大家分享。
第一种是用UIWebView加载电话,这种是合法的,可以上App Store的。
代码如下:
- UIWebView*callWebview =[[UIWebView alloc] init];
- NSURL *telURL =[NSURL URLWithString:@"tel:10010"];
- [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
- //记得添加到view上
- [self.view addSubview:callWebview];
UIWebView*callWebview =[[UIWebView alloc] init];
NSURL *telURL =[NSURL URLWithString:@"tel:10010"];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
//记得添加到view上
[self.view addSubview:callWebview];
第二种是私有方法,不能上App Store的(自己没试过)。
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10010"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10010"]];
上面的代码只是把第一个方法中的tel为telprompt.
2、发短信
iOS中可以使用两种方式发送短信,最简单是使用openURL:
- [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10010"]];//发短信
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10010"]];//发短信
上面方式无法指定短信内容,iOS4.0新加入了MFMessageComposeViewCont
MFMessageComposeViewController提供了操作界面使用前必须检查canSendText方法,若返回NO则不应将这个controller展现出来,而应该提示用户不支持发送短信功能.
messageComposeDelegate :代理,处理发送结果
recipients :收信人<列表,支持群发>
Frameworks中要引入MessageUI.framework
#import
添加协议:<MFMessageComposeViewCont
- #import <MessageUI/MessageUI.h>
- @interface DemoViewController : UIViewController <MFMessageComposeViewControllerDelegate>
- @end
#import <MessageUI/MessageUI.h>
@interface DemoViewController : UIViewController <MFMessageComposeViewControllerDelegate>
@end
调用MFMessageComposeViewController,同时实现协议MFMessageComposeViewControllerDelegate。
- - (void)showMessageView
- {
- if( [MFMessageComposeViewController canSendText] ){
- MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; //autorelease];
- controller.recipients = [NSArray arrayWithObject:@"10010"];
- controller.body = @"测试发短信";
- controller.messageComposeDelegate = self;
- [self presentModalViewController:controller animated:YES];
- [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"测试短信"];//修改短信界面标题
- }else{
- [self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];
- }
- }
- //MFMessageComposeViewControllerDelegate
- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
- [controller dismissModalViewControllerAnimated:NO];//关键的一句 不能为YES
- switch ( result ) {
- case MessageComposeResultCancelled:
- [self alertWithTitle:@"提示信息" msg:@"发送取消"];
- break;
- case MessageComposeResultFailed:// send failed
- [self alertWithTitle:@"提示信息" msg:@"发送成功"];
- break;
- case MessageComposeResultSent:
- [self alertWithTitle:@"提示信息" msg:@"发送失败"];
- break;
- default:
- break;
- }
- }
- - (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
- message:msg
- delegate:self
- cancelButtonTitle:nil
- otherButtonTitles:@"确定", nil];
- [alert show];
- }
- (void)showMessageView
{
if( [MFMessageComposeViewController canSendText] ){
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; //autorelease];
controller.recipients = [NSArray arrayWithObject:@"10010"];
controller.body = @"测试发短信";
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"测试短信"];//修改短信界面标题
}else{
[self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];
}
}
//MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
[controller dismissModalViewControllerAnimated:NO];//关键的一句 不能为YES
switch ( result ) {
case MessageComposeResultCancelled:
[self alertWithTitle:@"提示信息" msg:@"发送取消"];
break;
case MessageComposeResultFailed:// send failed
[self alertWithTitle:@"提示信息" msg:@"发送成功"];
break;
case MessageComposeResultSent:
[self alertWithTitle:@"提示信息" msg:@"发送失败"];
break;
default:
break;
}
}
- (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"确定", nil];
[alert show];
}