在iOS中经常会有跳转到打电话,发短信,评论等界面,这些功能实际只需要几行代码就可以实现。
打电话
方法一:
NSURL *url = [NSURL URLWithString:@"tel://10086"];
[[UIApplication sharedApplication] openURL:url];
//缺点: 电话打完后不会自动回到原应用,直接停留在通话记录界面
方法二:
NSURL *url=[NSURL URLWithString:@"telprompt://10086"];
[[UIApplication sharedApplication]openURL:url];
//优点:打完电话会自动回到原应用
发短信
方法一:
NSURL *url = [NSURL URLWithString:@"sms://10010"];
[[UIApplication sharedApplication] openURL:url];
//直接跳到发短信界面,但是不能指定短信内容,而且不能自动回到原应用
方法二:
//如果想指定短信内容,那就得使用MessageUI框架
//包含主头文件
#import <MessageUI/MessageUI.h>
//显示发短信的控制器
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
// 设置短信内容
vc.body = @"吃饭了没?";
// 设置收件人列表
vc.recipients = @[@"10010", @"02010010"];
// 设置代理
vc.messageComposeDelegate = self;
// 显示控制器
[self presentViewController:vc animated:YES completion:nil];
//代理方法,当短信界面关闭的时候调用,发完后会自动回到原应用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
// 关闭短信界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MessageComposeResultCancelled) {
NSLog(@"取消发送");
} else if (result == MessageComposeResultSent) {
NSLog(@"已经发出");
} else {
NSLog(@"发送失败");
}
}
跳转到AppStrore下载
//找到应用程序的描述链接,比如:http://itunes.apple.com/gb/app/id391945719?mt=8
//然后将 http:// 替换为 itms:// 或者 itms-apps://:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/gb/app/id391945719?mt=8"]];
跳转到AppStrore评分界面
NSURL *webUrl=[NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/id791297521?mt=8"];
[[UIApplication sharedApplication]openURL:webUrl];
跳转到QQ聊天界面
//uin为要聊天的QQ
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]])
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSURL *url = [NSURL URLWithString:@"mqq://im/chat?chat_type=wpa&uin=619249956&version=1&src_type=web"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
webView.delegate = self;
[webView loadRequest:request];
[self.view addSubview:webView];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的手机上没有安装QQ应用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
}