简体   繁体   English

如何在iPhone中创建短信应用程序

[英]How to Create SMS application in iPhone

I am new to iphone development, i want to create SMS application in my application.I have created mail application using "messageUI.framework".Is there any framework for creating SMS application.I have no idea of it, so tell me the way of approaching this task. 我是iPhone开发的新手,我想在我的应用程序中创建SMS应用程序。我已经使用“ messageUI.framework”创建了邮件应用程序。是否有用于创建SMS应用程序的框架。我不知道,所以告诉我方法完成这项任务。 Please guide me to achieve my task.Please help me out.Thanks. 请指导我完成任务。请帮助我。谢谢。

Unfortunately, there is no built-in view controller for sending SMS like MFMailComposeViewController for email. 不幸的是,没有内置的视图控制器可以发送MFMailComposeViewController之类的SMS来发送电子邮件。

As of iOS 4.0, you can use the MFMessageComposeViewController, a counterpart to the email-only MFMailComposeViewController. 从iOS 4.0开始,您可以使用MFMessageComposeViewController,它是仅用于电子邮件的MFMailComposeViewController的副本。 This lets you lay out and send an SMS message. 这使您可以布局并发送SMS消息。

Additionally, you can use the SMS URL scheme, like is decribed in this question . 另外,您可以使用SMS URL方案,如本问题所述 However, it appears that you cannot prepopulate the body of an SMS message this way. 但是,您似乎无法以这种方式预填充SMS消息的正文

You can use the MFMessageComposeViewController class, as documented by Apple . 您可以使用Apple记录的MFMessageComposeViewController类。

For that, first add MessageUI.framework to your project. 为此,首先将MessageUI.framework添加到您的项目中。

// in .h file make the following change //在.h文件中进行以下更改

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>

@interface YourViewController: UIViewController <MFMessageComposeViewControllerDelegate>

// then in .m file do the following. //然后在.m文件中执行以下操作。

- (void)viewDidLoad {
    [super viewDidLoad];

SMSLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 340.0, 260.0, 30.0)];
    SMSLabel .frame = CGRectMake(30.0, 340.0, 260.0, 30.0);
    SMSLabel .adjustsFontSizeToFitWidth = YES;
    SMSLabel .hidden = YES;
    SMSLabel .text = @"";
    SMSLabel .userInteractionEnabled = NO;
    SMSLabel.alpha=0.0;
    [self.view addSubview:SMSLabel ];  


}

-(void)ComposerSheet 
{
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate = self;

    picker.recipients = [NSArray arrayWithObject:@"1234567"];  
    picker.body = @"iPhone OS4";

    [self presentModalViewController:picker animated:YES];
    [picker release];

}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    SMSLabel.alpha=1.0;

    switch (result)
    {
        case MessageComposeResultCancelled:
            SMSLabel .text = @"Result: canceled";
            NSLog(@"Result: canceled");
            break;
        case MessageComposeResultSent:
            SMSLabel .text = @"Result: sent";
            NSLog(@"Result: sent");
            break;
        case MessageComposeResultFailed:
            SMSLabel .text = @"Result: failed";
            NSLog(@"Result: failed");
            break;
        default:
            SMSLabel .text = @"Result: not sent";
            NSLog(@"Result: not sent");
            break;
    }

    [self dismissModalViewControllerAnimated:YES];

}

Sending messages is somewhat easy — you can usually send an email to a specially formatted number like 555.555.5555@verizon.net (example only, not sure of real format) and it will send to the device. 发送消息有点容易-您通常可以将电子邮件发送到特殊格式的号码,例如555.555.5555@verizon.net(仅作为示例,不确定真实格式),然后它将发送到设备。 There is not going to be an easy way to receive sms messages in your app natively. 本地将不会有一种简单的方法来接收短信。

You can, however, try using one of many free sms apis such as ZeepMobile 但是,您可以尝试使用许多免费的短信 API之一,例如ZeepMobile

MFMessageComposeViewController will send the message through iMessage app. MFMessageComposeViewController将通过iMe​​ssage应用发送消息。 But If you want to send sms through network career of your iPhone then following code will help you 但是,如果您想通过iPhone的网络生涯发送短信,则以下代码将为您提供帮助

    NSString *phoneToCall = @"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM