码迷,mamicode.com
首页 > 移动开发 > 详细

iOS常用小功能的实现

时间:2015-01-20 22:16:36      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:ios

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常用小功能的实现

标签:ios

原文地址:http://blog.csdn.net/casablaneca/article/details/42929849

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!