简体   繁体   English

使用按钮和文本字段/视图(iPhone)打开消息应用程序(SMS应用程序)

[英]Open Message app (SMS app) with a button and a Text field/View (iPhone)

Would this be possible? 这可能吗?

The app will have a button and a Text Field or a Text View. 该应用程序将有一个按钮和一个文本字段或文本视图。

The user types in the phone number in the Text Field or Text View. 用户在文本字段或文本视图中键入电话号码。 when the user is done the user presses the button which will open the Message or SMS app in the iPhone. 当用户完成后,用户按下将在iPhone中打开消息或SMS应用程序的按钮。

How would I do this? 我该怎么做? If possible please provide some code! 如果可能请提供一些代码! :) :)

Thank you in advance! 先感谢您!

Take a look at the MessageComposer Sample App and the MFMessageComposeViewController Class . 查看MessageComposer示例应用程序MFMessageComposeViewController类

You then do something like this, though you should first check whether the MFMessageComposeViewController is actually available on your device (See the MessageComposer Sample): 然后你可以做这样的事情,虽然你应该先检查你的设备上是否有MFMessageComposeViewController实际可用(参见MessageComposer示例):

MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
picker.recipients = [NSArray arrayWithObjects:@"1234", @"2345", nil];
picker.body = yourTextField.text

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

You need to first import the MessageUI.framework ( see this answer ). 您需要先导入MessageUI.framework请参阅此答案 )。

Import it into your classes via #import <MessageUI/MessageUI.h> and add <MFMessageComposeViewControllerDelegate> in the .h file, eg like so: 通过#import <MessageUI/MessageUI.h>将其导入到您的类中,并在.h文件中添加<MFMessageComposeViewControllerDelegate> ,例如:

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

@interface YourClass : UIViewController <MFMessageComposeViewControllerDelegate> 
{
    // ...

Another short way, may help anyone much better :) 另一个简短的方法,可能会帮助更好的人:)

NSString *sms = @"sms:+1234567890&body=This is sms body.";
NSString *url = [sms stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

In Swift 3.0 在Swift 3.0中

static func sendMessageToNumber(number:String,message:String){

        let sms = "sms:\(number)&body=\(message)"
        let url = URL(string:sms)!
        let shared = UIApplication.shared

        if(shared.canOpenURL(url)){
            shared.openURL(url)
        }else{
            print("unable to send message")
        }
    }

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

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