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

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

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

@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

你可能感兴趣的:(iOS开发中跳转到短信 彩信 iMessage)