如何在iPhone中以编程方式发送消息

时间:2013-07-09 06:06:01

标签: iphone

我使用了以下代码,但没有用。

我的代码是:

MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
controller.body = @"Check out 'Invite Me' for iOS and be my friend here.";
controller.recipients = arrSelectedPhoneNumbers;
NSLog(@"%@",controller.recipients);
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];

1 个答案:

答案 0 :(得分:1)

使用以下方式:

//在.h文件中添加委托

@interface ContactsViewController : UIViewController<MFMessageComposeViewControllerDelegate>

//在.m文件中添加

 MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
                if(picker) {
                    picker.messageComposeDelegate = self;
                    picker.recipients = [NSArray arrayWithObject:number];
                    picker.body = @"body content";
                    [self presentViewController:picker animated:NO completion:nil];
                    picker = nil;
                }
                NSLog(@"SMS fired");

    - (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:
                    NSLog(@"Result: not sent");
                    break;
            }
            //[self dismissModalViewControllerAnimated:YES];
            [self dismissViewControllerAnimated:YES completion:nil];
        }