MessageUI框架发短信

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10000"]];

这样的方式无法指定短信内容,可以使用MessageUI框架。

1. 导入框架:MessageUI.framework

2. #import <MessageUI/MessageUI.h>
3. 添加协议:<MFMessageComposeViewControllerDelegate>

- (void)shortMessage{

    if( [MFMessageComposeViewControllercanSendText] )

    {

        MFMessageComposeViewController * controller = [[MFMessageComposeViewControlleralloc] init];

        controller.body =@"sssssssssssssss";

        controller.messageComposeDelegate =self;

        

        [[[[controllerviewControllers] lastObject] navigationItem] setTitle:@"分享"];

        [selfpresentViewController:controller animated:YEScompletion:nil];;

        [controller release];

    }

    else

    {

       // NSLog(@"该设备不支持短信发送");

    }

}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

{

    [controller dismissViewControllerAnimated:NOcompletion:nil];//关键的一句  不能为YES

    switch ( result ) {

        caseMessageComposeResultCancelled:

        {

            //NSLog(@"发短信取消");

        }

            break;

        caseMessageComposeResultFailed:// send failed

        {

            //NSLog(@"发短信失败");];

        }

            break;

        caseMessageComposeResultSent:

        {

            // NSLog(@"发短信成功");

        }

            break;

        default:

            break;

    }

}

由于短信内容是request请求获得的,在

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result方法里要结束请求。


你可能感兴趣的:(MessageUI框架发短信)