欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

短信|彩信

程序员文章站 2023-11-25 10:44:34
"iOS系统开发:发“彩信” CSDN博客" ......

1.初始MessageUI.framework



在MessageUI.framework中主要有两个控制器类分别用于发送短信
(MFMessageComposeViewController)和邮件(MFMailComposeViewController),它们均继承于UINavigationController。

2.发送彩信 

a、创建对象

@property(nonatomic, weak)IBOutlet UITextField *consignee;//收件人
@property(nonatomic, weak)IBOutlet UITextField *mainBody;//正文
@property(nonatomic, weak)IBOutlet UITextField *subject;//主题
@property(nonatomic, weak)IBOutlet UITextField *attachments;//附件
       b、发彩信
- (IBAction)sendMessageClick:(UIButton *)sender{

     (1)判断是否可以发送文本: 
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *messageControlller = [[MFMessageComposeViewController alloc]init];
         //收件人 :
        messageControlller.recipients = [self.consignee.text componentsSeparatedByString:@","];
        //信息正文:
        messageControlller.body = self.mainBody.text;
        //设置代理(<**MFMessageComposeViewControllerDelegate**>) 
        messageControlller.**messageComposeDelegate** = self;
      (2)判断运营商是否支持主题 
        if ([MFMessageComposeViewController canSendSubject]) {
            messageControlller.subject = self.subject.text;
        }else{
                    NSLog(@"不支持主题");
        }
      (3)判断运营商是否支持附件  
          if ([MFMessageComposeViewController canSendAttachments]) {
         // 发送纯文本形式
            NSArray *attachments = [_attachments.text componentsSeparatedByString:@","];
            if (attachments.count > 0) {
                [attachments enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString *path = [[NSBundle mainBundle]pathForResource:obj ofType:nil];
                    NSURL *url = [NSURL fileURLWithPath:path];
                    [messageControlller addAttachmentURL:url
                                   withAlternateFilename:obj];
                }];
            }
          // 发送照片类型 
            NSString *path = [[NSBundle mainBundle]pathForResource:@"photo.jpg" ofType:nil];
            NSURL *url = [NSURL URLWithString:path];
            NSData *data = [NSData dataWithContentsOfURL:url];
            [messageControlller addAttachmentData:data typeIdentifier:@"public.image" filename:@"photo.jpg"];

        }else{

            NSLog(@"不支持附件");
        }
           [self presentViewController:messageControlller animated:YES completion:nil];
    }

}
3、协议方法
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"取消发送");
            break;
        case MessageComposeResultSent:
            NSLog(@"发送成功");
            break;
        case MessageComposeResultFailed:
            NSLog(@"发送失败");
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}