您的位置:首页 > 移动开发 > IOS开发

iOS 常用小功能

2016-12-05 15:19 260 查看
小功能简介
iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话、打开网址、发邮件、发短信等 

 

打电话-方法3

创建一个UIWebView来加载URL,拨完后能自动回到原应用
if (_webView ==
nil) {
    _webView = [[UIWebView
alloc] initWithFrame:CGRectZero];
}
[_webView
loadRequest:[NSURLRequest
requestWithURL:[NSURL
URLWithString:@"tel://10010"]]];

拨号之前会弹框询问用户是否拨号,拨完后能自动回到原程序
注意:这个webView千万不要设置尺寸,不然会挡住其他界面,他只是用来打电话,不需要显示 

 
发短信-方法2

如果想指定短信内容,那就得使用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(@"发送失败");
    }
}

邮件发送后的代理方法回调,发完后会自动回到原应用

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
   
关闭邮件界面
    [controller
dismissViewControllerAnimated:YES
completion:nil];
    
    if (result ==
MFMailComposeResultCancelled) {
       
NSLog(@"取消发送");
    }
else if (result == MFMailComposeResultSent) {
       
NSLog(@"已经发出");
    }
else {
       
NSLog(@"发送失败");
    }
}

打开其他常见文件

如果想打开一些常见文件,比如html、txt、PDF、PPT等,都可以使用UIWebView打开
只需要告诉UIWebView文件的URL即可
至于打开一个远程的共享资源,比如http协议的,也可以调用系统自带的Safari浏览器:
NSURL *url = [NSURL
URLWithString:@”http://www.baidu.com"];
[[UIApplication
sharedApplication]
openURL:url];

应用评分
为了提高应用的用户体验,经常需要邀请用户对应用进行评分,应用评分无非就是跳转到AppStore展示自己的应用,然后由用户自己撰写评论,如何跳转到AppStore,并且展示自己的应用

方法
NSString
*appid =
@"725296055”;
NSString *str = [NSString
stringWithFormat:
                
@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];

[[UIApplication
sharedApplication]
openURL:[NSURL
URLWithString:str]];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: