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

iOS常用小功能的实现

2015-01-20 21:29 281 查看
iOS应用开发中有许多非常实用的小功能, 这些小功能的实现也非常的简单, 本文将这些小功能汇总,用于备忘。

 

1. 打电话功能的实现

 

实现打电话功能的方式有多种,其中最好的方式如下:

//利用UIWebView打电话
if (_webView == nil) {
//WebView不需要显示,只需要实现打电话功能
_webView = [[UIWebView alloc] initWithFrame:CGRectZero];
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];


2. 发短信功能的实现

实现发短信的功能也有多种,但是下面的方式最佳:

//2.使用MessageUI框架封装的功能
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
messageVC.body = @"你吃翔了没?";
messageVC.recipients = @[@"10010", @"10086"];
//设置代理
messageVC.messageComposeDelegate = self;
[self presentViewController:messageVC animated:YES completion:nil];
#pragma mask MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
//关闭短信界面
[controller dismissViewControllerAnimated:YES completion:nil];

if (result == MessageComposeResultCancelled) {
NSLog(@"取消发送");
}else if(result == MessageComposeResultSent){
NSLog(@"发送成功");
}else{
NSLog(@"发送失败");
}

}


3. 发邮件功能的实现

发邮件的功能实现有多种方式,推荐使用下面的方式:

// 使用MessageUI框架封装的功能
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
//设置邮件主题
[mailVC setSubject:@"会议"];
//设置邮件内容
[mailVC setMessageBody:@"今天下午你去吃翔吧!" isHTML:NO];
//设置收件人列表
[mailVC setToRecipients:@[@"123456@qq.com"]];
//设置抄送人列表
[mailVC setCcRecipients:@[@"987654321@qq.com"]];
//设置密送人列表
[mailVC setBccRecipients:@[@"7654@qq.com"]];

//添加一张附图(一张图片)
UIImage *image = [UIImage imageNamed:@"aaa.png"];
//压缩图片
NSData *data = UIImagePNGRepresentation(image);
[mailVC addAttachmentData:data mimeType:@"image/png" fileName:@"aaa.png"];
//设置代理
mailVC.mailComposeDelegate = self;
[self presentViewController:mailVC animated:YES completion:nil];

#pragma mask MFMailComposeViewControllerDelegate
- (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(@"发送失败");
}
}


4. 应用评分功能

下面的代码用于实现应用评分功能:

//拿到应用的ID
NSString *appid = @"123456";
NSString *str = [NSString stringWithFormat:@"itms-apps://ituns.apple.com/app/id%@?mt=8", appid];
NSURL *url = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:url];


5. 打开常用的文件

在iOS开发中,如果想打开一些常见文件,比如html/txt/pdf/ppt等,都可以选择使用UIWebView打开,只需要告诉UiwebView的URL即可,如果想打开一个远程的共享资源,比如http协议下的资源,也可以调用系统自带的Safari浏览器

NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];
[[UIApplication sharedApplication] openURL:url];


6. 关联到其他应用

有时候,需要在当前应用A中打开其他应用B,需要经过下面两步:

1.)首先,B应用有自己的URL地址(在info.plist中配置)



配置好的B应用的URL地址为: mj://ios.itcast.cn

 

2.) 接着在A应用中使用UIApplication完成跳转

NSURL *url = [NSURL URLWithString:@"mj://ios.itcast.cn"];
[[UIApplication sharedApplication] openURL:url];


Demo下载地址: http://download.csdn.net/detail/luozhonglan/8381037
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios