应用内发短信/邮件,messenger、line、whatsapp分享

代码:

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

@interface ViewController ()<MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>

@end

@implementation ViewController

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

#pragma mark - message
- (IBAction)message:(id)sender {
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController* composeVC = [[MFMessageComposeViewController alloc] init];
        composeVC.messageComposeDelegate = self;
        
        // Configure the fields of the interface.
        composeVC.recipients = @[@"15001238888"];
        composeVC.body = @"Hello from California!";
        
        if ([MFMessageComposeViewController canSendSubject]) {//无效,UI没有显示,有待研究
            NSLog(@"可以发送主题");
            composeVC.subject = @"我是主题";
        }
        
        if ([MFMessageComposeViewController canSendAttachments]) {//无法正常显示,有待研究
            NSLog(@"可以发送附件");
            NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"QQ20170411" ofType:@"png"];
            NSURL *url = [NSURL fileURLWithPath:imgPath isDirectory:NO];
            BOOL success = [composeVC addAttachmentURL:url withAlternateFilename:@"越狱"];
            NSLog(@"添加附件1:%@",@(success));
            success = [composeVC addAttachmentData:[NSData dataWithContentsOfURL:url] typeIdentifier:@"image/png" filename:@"越狱"];
            NSLog(@"添加附件2:%@",@(success));
            
        }
        
        if ([MFMessageComposeViewController isSupportedAttachmentUTI:@"image/png"]) {
            NSLog(@"SupportAttachmentUTI");
        }
        
        
        // Present the view controller modally.
        [self presentViewController:composeVC animated:YES completion:^{
            
        }];
        
    } else {
        NSLog(@"不支持发送短信");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]];
        
    }
}
#pragma mark - MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    NSLog(@"%@", @(result));
    switch (result) {
        case MessageComposeResultCancelled:
        {
            
        }
            break;
            
        case MessageComposeResultSent:
        {
            
        }
            break;
            
        case MessageComposeResultFailed:
        {
            
        }
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - mail
- (IBAction)mail:(id)sender {
    if ([MFMailComposeViewController canSendMail]) {
        
        MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init];
        composeVC.mailComposeDelegate = self;
        
        // Configure the fields of the interface.
        [composeVC setToRecipients:@[@"address@example.com"]];
        [composeVC setCcRecipients:@[@"Ccaddress@example.com"]];
        [composeVC setBccRecipients:@[@"Bccaddress@example.com"]];
        [composeVC setSubject:@"Hello!"];
        [composeVC setMessageBody:@"Hello from California!" isHTML:NO];
        
        NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"QQ20170411" ofType:@"png"];
        NSData *data = [NSData dataWithContentsOfFile:imgPath];
        [composeVC addAttachmentData:data mimeType:@"image/png" fileName:@"越狱"];
        
        // Present the view controller modally.
        [self presentViewController:composeVC animated:YES completion:nil];
        
    } else {
        NSLog(@"Mail services are not available.");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];
    }
    
}

#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    NSLog(@"%@", @(result));
    switch (result) {
        case MFMailComposeResultCancelled:
        {
            
        }
            break;
            
        case MFMailComposeResultSaved:
        {
            
        }
            break;
            
        case MFMailComposeResultSent:
        {
            
        }
            break;
            
        case MFMailComposeResultFailed:
        {
            
        }
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//messenger只能分享链接
- (void)MessengerAction{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb-messenger://"]]) {
        NSString *encodedValue= [self.sharedUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
        NSString *url = [NSString stringWithFormat:@"fb-messenger://share/?app_id=com.blued.international&link=%@",encodedValue];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    } else {
        NSLog(@"没有安装Messenger");
    }
}

//line只能分享链接
- (void)LineAction{
    NSString *urlString = [NSString stringWithFormat:@"line://msg/%@", [self.sharedUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURL *url = [NSURL URLWithString:urlString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }else{
        NSLog(@"没有安装Line");
    }
}

//whatsapp可以分享内容和链接
- (void)WhatsAppAction{
    if ([WhatsAppKit isWhatsAppInstalled]) {
        [WhatsAppKit launchWhatsAppWithMessage:self.sharedContentAndUrl];
    } else {
        NSLog(@"没有安装WhatsApp");
    }
}
@end

效果图

短信/邮件

上一篇:UISearchController使用
下一篇:UIView局部透明/打洞,点击事件穿透

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,322评论 25 709
  • 7、不使用IB是,下面这样做有什么问题? 6、请说说Layer和View的关系,以及你是如何使用它们的。 1.首先...
    AlanGe阅读 4,077评论 0 1
  • 1、在人生这趟公车上,死神就是个小偷,生命是贵重物品。虽然你尽量躲着小偷,但他悄悄地黏着你,还可以隐形。而我想做那...
    curtainl阅读 3,369评论 0 0
  • 放爱一条生路体验日。 你我各不相干,不管不顾中。 你可以肆无忌惮的唱歌跳舞玩游戏看跑男吃关东煮喝饮料,我就是个隐身...
    MiluJoy阅读 1,228评论 0 1
  • 通常,我们需要对 UITextField 的最大输入字符数进行限制。在编辑的时候进行限制总比提交表单后提示错误强。...
    CyrusCao阅读 5,183评论 7 48