iOS开发中跳转到短信 彩信 iMessage

从自己开发的软件跳转到系统短信界面,并且把内容和图片给显示在信息中

在调用系统的短信控件时需要先导入<MessageUI/MessageUI.h>
同时需要遵循代理方法
@interface ViewController ()<MFMessageComposeViewControllerDelegate>

@property (nonatomic, strong) UITextField * photoText;

@property (nonatomic, strong) UITextField * messageText;

@property (nonatomic, strong) UIImage    * imageCache;

@property (nonatomic, strong) NSString    * urlStr;

@end
系统控件MFMessageComposeViewController 方法的实现
这个方法可以把本地文件添加到彩信中,网络图片需要先加载到本地保存,然后添加过后再删除
- (void)sendPicBtnClick:(UIButton *)sendPic {
    if ([MFMessageComposeViewController canSendAttachments]) {
        MFMessageComposeViewController * messageVC = [[MFMessageComposeViewController alloc]init];
        messageVC.recipients = @[self.photoText.text];
        
        messageVC.body = self.messageText.text;
        //本地图片
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
//        if ([messageVC addAttachmentURL:[NSURL fileURLWithPath:path] withAlternateFilename:nil])
//        {
//            NSLog(@"添加成功");
//        }else{
//            NSLog(@"失败");
//        }
        //网络图片
//        NSData * imageData = UIImageJPEGRepresentation(_imageCache, 1.0);
         NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString * path = [documentsDirectoryPath stringByAppendingPathComponent:@"MyImage.png"];
        if ([messageVC addAttachmentURL:[NSURL fileURLWithPath:path] withAlternateFilename:nil])
        {
            NSLog(@"添加成功");
        }else{
            NSLog(@"失败");
        }
//        NSData * imageData = UIImagePNGRepresentation(_imageCache);
//        if ([messageVC addAttachmentData:imageData typeIdentifier:@"png" filename:@""]) {
//            NSLog(@"添加成功");
//        }else{
//            NSLog(@"添加失败");
//        }
        messageVC.messageComposeDelegate = self;
        /** 取消按钮的颜色(附带,可不写) */
        messageVC.navigationBar.tintColor = [UIColor redColor];
        [self presentViewController:messageVC animated:YES completion:nil];
    }else{
        NSLog(@"模拟器不支持发送短信");
    }
}
代理方法的实现
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    /** 发送完信息就回到原程序*/
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            NSLog(@"发送成功");
            break;
        case MessageComposeResultFailed:
            NSLog(@"发送失败");
            break;
        case MessageComposeResultCancelled:
            NSLog(@"发送取消");
        default:
            break;
    }
}
Demo地址 https://github.com/WindFlyCloud/AppTipiMessage
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容