iOS常用的功能(打电话、发短信、发邮件等)

注释:要引入MessageUI.framework库

#import "ViewController.h"
#import <MessageUI/MessageUI.h>

@interface ViewController ()<MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>

@property (strong, nonatomic) UIWebView *webView;

@end

@implementation ViewController

- (UIWebView *)webView
{
    if (!_webView)
    {
        _webView = [[UIWebView alloc] init];
    }
    
    return _webView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self sendMail2];
}

/**
 URL:表示网络资源
 1) http:// -> 协议头
 2) www.wanmeixingcheng.com:8089 -> 主机名
 3) /mantisbt/my_view_page.php -> 资源路径
 */

#pragma mark --- 打电话功能

/**
 *  打完电话无法回到当前应用程序
 */
- (void)call1
{
    NSURL *url = [NSURL URLWithString:@"tel://10086"];
    [[UIApplication sharedApplication] openURL:url];
}

/**
 *  打完电话能够回到当前应用程序
 *  telprompt属于苹果的私有API,使用这个API程序无法上架!
 */
- (void)call2
{
    NSURL *url = [NSURL URLWithString:@"telprompt://10086"];
    [[UIApplication sharedApplication] openURL:url];
}

/**
 * 打完电话能够回到当前应用程序(最优方法)
 * 注意:不要将webView添加到self.view,否则会遮挡其他的界面
 */
- (void)call3
{
    NSURL *url = [NSURL URLWithString:@"tel://10086"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

#pragma mark --- 发短信功能

/**
 *  简单
 *  不能返回到应用程序界面
 *  只能给一个人发送
 *  不能指定短信内容
 */
- (void)sendMessage1
{
    NSURL *url = [NSURL URLWithString:@"sms://10086"];
    [[UIApplication sharedApplication] openURL:url];
}

/**
 *  苹果推(最优方法)
 */
- (void)sendMessage2
{
    if (![MFMessageComposeViewController canSendText]) return;
 
    // 不是自己开发的视图控制器,通常都是临时使用的,统一使用Modal方式展现
    MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
    
    messageVC.recipients = @[@"10086", @"10010"];
    messageVC.subject = @"表白情书";
    messageVC.body = @"我喜欢你很久了!";
    // messageVC.attachments = @[]; // 短信附件
    messageVC.messageComposeDelegate = self;
    
    // 谁展现,谁关闭
    [self presentViewController:messageVC animated:YES completion:nil];
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    if (result == MessageComposeResultSent)
    {
        NSLog(@"发送成功");
    }
    else if (result == MessageComposeResultCancelled)
    {
        NSLog(@"发送取消");
    }
    else if (result == MessageComposeResultFailed)
    {
        NSLog(@"发送失败");
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark --- 发邮件功能
/**
 * 发完邮件无法回到当前应用程序
 */
- (void)sendMail1
{
    NSURL *url = [NSURL URLWithString:@"mailto://903503936@qq.com"];
    [[UIApplication sharedApplication] openURL:url];
}
/**
 *  发完邮件能回到当前应用程序(最优方法)
 */
- (void)sendMail2
{
    if (![MFMailComposeViewController canSendMail]) return;
   
    MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
    
    [mailVC setToRecipients:@[@"903503936@qq.com"]];
    // 指定cc收件人(需要知道该邮件内容的人,抄送给经理,收件人和抄送的彼此知道这封邮件谁能收到)
    [mailVC setCcRecipients:@[]];
    // 指定bcc收件人(密送,除了发件人,其他人都不知道这封信bcc能够看到)
    [mailVC setBccRecipients:@[]];
    [mailVC setSubject:@"表白情书"];
    
    UIImage *image = [UIImage imageNamed:@"btn_image_text_weixin"];
    NSData *data = UIImagePNGRepresentation(image);
    [mailVC addAttachmentData:data mimeType:@"image/png" fileName:@"附件"];
    
    [mailVC setMessageBody:@"我喜欢你很久了!" isHTML:NO];
    mailVC.mailComposeDelegate = self;
    
    [self presentViewController:mailVC animated:YES completion:nil];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    if (result == MFMailComposeResultSent)
    {
        NSLog(@"发送成功");
    }
    else if (result == MFMailComposeResultCancelled)
    {
        NSLog(@"发送取消");
    }
    else if (result == MFMailComposeResultFailed)
    {
        NSLog(@"发送失败");
    }
    else if (result == MFMailComposeResultSaved)
    {
        NSLog(@"发送保存");
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

posted @ 2016-01-06 20:13  滴血雄鹰  阅读(204)  评论(0编辑  收藏  举报