How to share text to Message in the app you are building(iOS platform)

本文介绍了一种在iOS应用中轻松实现短信分享功能的方法,无需使用UIActivityViewController或ShareSDK等组件。通过直接调用MFMessageComposeViewController并遵循其代理方法,可以方便地将文本信息发送给其他用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I find it is relatively easy to share text to message and mail(build-in app on ios).

No need to use UIActivityViewController or ShareSDK.

you should import <MessageUI/MessageUI.h> on your .h file.

the source code as follows:

.h file:

@interface ViewController : UIViewController<MFMessageComposeViewControllerDelegate>
- (IBAction)shareAction:(UIButton *)sender;
@end

.m file:

- (IBAction)shareAction:(UIButton *)sender {
    MFMessageComposeViewController *msgVC = [[MFMessageComposeViewController alloc] init];
    msgVC.messageComposeDelegate = self;
    msgVC.navigationBar.tintColor = [UIColor redColor];
    msgVC.body = @"test";
//    msgVC.recipients = [NSArray arrayWithObjects:@"", nil];
    [self presentViewController:msgVC animated:YES completion:NULL];
    
    
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MessageComposeResultSent:
            NSLog(@"Result: Sent");
            break;
        case MessageComposeResultFailed:
            NSLog(@"Result: Failed");
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:NULL];

}

Hope to help you guys!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值