2

我是 iPhone 开发的新手。我想传递一些带有额外短信细节的字符串。我正在使用下面的代码向特定号码发送短信,但是如果我想用短信字符串发送一些数据,那我该怎么办?谁能告诉我那个格式?

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

2 回答 2

1

SMSURL 方案不提供任何填充 SMS 正文的方法。因此,您预先填写短信的唯一选择是使用MFMessageComposeViewController


您可以使用 Messaging 框架设置文本消息正文,您需要将其添加到框架中。添加#import <MessageUI/MessageUI.h>到导入列表。

确保视图控制器实现MFMessageComposeViewControllerDelegate,这是关闭MFMessageComposeViewController.

用于MFMessageComposeViewControllerDelegate

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    [controller dismissModalViewControllerAnimated:YES];
}

然后在将显示消息编写器的方法中放置以下代码:

MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
messageController.body = @"Your message here.";
messageController.recipients = [NSArray arrayWithObject:@"9016098909891"];
[self presentModalViewController:messageController animated:YES];

[messageController release], messageController = nil;
于 2012-04-23T07:45:46.593 回答
1

您可以在应用内使用短信功能,您可以在其中设置短信正文。

第一步:将MessageUI框架导入你的项目并导入头文件#import <MessageUI/MessageUI.h>

第二步:发送短信如下

    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello from Mugunth";
        controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
        controller.messageComposeDelegate = self;
            controller.delegate = self;
        [self presentModalViewController:controller animated:YES];
    }

第 3 步:将代表处理为

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"Cancelled");
            break;
        case MessageComposeResultFailed:
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"
                                                           delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];
            [alert show];
            [alert release];
            break;
        case MessageComposeResultSent:

            break;
        default:
            break;
    }

    [self dismissModalViewControllerAnimated:YES];
}

在这里查看文章http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/

于 2012-04-23T07:45:59.560 回答