美文网首页iOS Developer
应用内发短信/邮件,messenger、line、whatsap

应用内发短信/邮件,messenger、line、whatsap

作者: BoomLee | 来源:发表于2017-04-14 19:09 被阅读712次

    代码:

    #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局部透明/打洞,点击事件穿透

    相关文章

      网友评论

        本文标题:应用内发短信/邮件,messenger、line、whatsap

        本文链接:https://www.haomeiwen.com/subject/onkgattx.html