概述
掌握
常用小功能(打电话、打开网址、发邮件、短信)
真机调试
UI综合练习(网易彩票)
一、常用小功能
1、打电话
1)方法一:
NSURL *url = [NSURL URLWithString:@"tel://10010"];
[[UIApplication sharedApplication] openURL:url];
缺点:不会自动回到原应用,直接停留在通话记录页面
2)方法二:拨号之前会弹框询问用户是否拨号,拨完号之后能自动回到原应用
方法二 Expand source
NSURL *url = [NSURL URLWithString:@"telprompt://10010"];
[[UIApplication sharedApplication] openURL:url];
缺点:因为是私有API,可能会审核不通过
3)方法三:创建一个UIWebView来加载URL,拨完号之后能自动回到原界面(推荐)
方法三 Expand source
if (_webView == nil) {
_webView = [[UIWebView alloc] initWithFrame:CGRectZero];
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];
注意点:这个UIWebView不要添加到界面,否则会挡住其他界面
2、发短信
1)方法一:直接跳到短信界面
方法一 Expand source
NSURL *url = [NSURL URLWithString:@"sms://10010"];
[[UIApplication sharedApplication] openURL:url];
缺点:不能指定短信内容,不能自动回到原应用
2)方法二:MessageUI框架--modal方法进行控制器间的切换
方法二
#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(@"发送失败");
}
}
3、发邮件
1)方法一:用自带的邮件客户端
方法一 Expand source
缺点:发完邮件后不会自动返回原界面
NSURL *url = [NSURL URLWithString:@"mailto://1887405@139.com"];
[[UIApplication sharedApplication] openURL:url];
2)方法二: MFMailComposeViewController
使用框架发送邮件 Expand source
//2)方法二: MFMailComposeViewController
MFMailComposeViewController *mailVC =[[MFMailComposeViewController alloc]init];
//设置邮件
[mailVC setSubject:@"邮件主题:test:---------"];
//设置邮件内容
[mailVC setMessageBody:@"邮件内容: test------" isHTML:NO];
//设置收件人列表
[mailVC setToRecipients:@[@"zang_kn@icloud.com",@"hang_kn@hisuntech.com"]];
//设置抄送列表
[mailVC setCcRecipients:@[@"84924492@qq.com",@"hang_kn@hisuntech.com"]];
//设置密送列表
[mailVC setBccRecipients:@[@"94934863@qq.com",@"90977255@qq.com"]];
//添加附件--Adds the specified data as an attachment to the message.
UIImage *image = [UIImage imageNamed:@"about_logo"];
NSData *date = UIImagePNGRepresentation(image);//Returns the data for the specified image in PNG format
/**
The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be nil.
*/
[mailVC addAttachmentData:date mimeType:@"image/png" fileName:@"test.png"];
//设置代理
[mailVC setMailComposeDelegate:self];
[self presentViewController:mailVC animated:YES completion:nil];
#pragma mark - MFMailComposeViewControllerDelegate 监听didFinishWithResult,进行关闭邮件界面
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
/*enum MFMailComposeResult {
MFMailComposeResultCancelled,
MFMailComposeResultSaved,
MFMailComposeResultSent,
MFMailComposeResultFailed
};*/
[controller dismissViewControllerAnimated:YES completion:^{
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"%@",@"发送取消");
break;
case MFMailComposeResultFailed:
NSLog(@"%@",@"发送失败");
break;
case MFMailComposeResultSent:
NSLog(@"%@",@"发送成功");
break;
case MFMailComposeResultSaved:
NSLog(@"%@",@"MFMailComposeResultSaved");//发送取消,并选择了save Draft
break;
}
}];
}
4、打开其他常见文件(htm、txt、pdf)
1) UIWebView:只需要告诉UIWebView文件的URL即可
2)至于打开一个远程的共享资源,比如http协议的,也可以调用系统自带的Safari浏览器:
打开网址http
NSURL *url = [NSURL URLWithString:@”http://www.baidu.com"];
[[UIApplication sharedApplication] openURL:url];
5、应用间跳转(A-》B)
首先:B应用有自己的URL地址(Info.plist中配置)
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>www.hisunpay.com</string>
<key>CFBundleURLSchemes</key>
<array>
<string>kn</string>
</array>
</dict>
</array>
B应用的URL地址就是:kn://www.hisunpay.com
其次:A应用使用UIApplication完成跳转
完成应用间跳转
NSURL *url = [NSURL URLWithString:@"kn://www.hisunpay.com"];
[[UIApplication sharedApplication] openURL:url];
B 应用可以在 AppDelegate 中处理A应用返回的信息。
6、应用评分
跳转到AppStore,并且展示自己的应用
NSString *appid= @"444934666";
NSString *str= [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
//跳转到App Store评分
[scoreItem setOptionBlock:^{
NSString *appId = @"425349261";//网易新闻
NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/app/id%@?mt=8",appId];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}];