简体   繁体   English

Swift中的MFMessageComposeViewController初始化

[英]MFMessageComposeViewController Initialization in Swift

I am currently developing an app where I would like to be able to send SMS messages to confirm the user has the number they are sending from. 我目前正在开发一个应用程序,希望在该应用程序中发送短信,以确认用户具有从其发送的号码。 Then I do some back end work and query the server to see if the number has been validated. 然后,我进行一些后端工作,并查询服务器以查看号码是否已验证。

The only issue is that I am unable to send the SMS. 唯一的问题是我无法发送短信。 The only tutorials and documentation online from what I can find is Objective-C. 从网上可以找到的唯一教程和文档是Objective-C。

I have copied it to the best of my ability, however there is one portion that I cannot find a Swift equivalent too. 我已尽我所能复制了它,但是有一部分我也找不到Swift的等效项。 The "init". “初始化”。

func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {

    if (result.value == MessageComposeResultCancelled.value) {
        NSLog("Message was cancelled.");
    }
    else if (result.value == MessageComposeResultFailed.value) {
        var warningAlert : UIAlertView = UIAlertView.alloc();
        warningAlert.title = "Error";
        warningAlert.message = "Failed to send SMS!";
        warningAlert.delegate = nil;
        warningAlert.show();
        NSLog("Message failed.");
    } else {
        NSLog("Message was sent.");
    }
    self.dismissViewControllerAnimated(true, completion: nil);
}

func showSMS() {
    if (!MFMessageComposeViewController.canSendText()) {
        var warningAlert : UIAlertView = UIAlertView.alloc();
        warningAlert.title = "Error";
        warningAlert.message = "Your device does not support SMS.";
        warningAlert.delegate = nil;
        warningAlert.show();
        return;
    }

    var recipients : NSArray = ["1234567890"];
    var message : NSString = "Message.";


    // This is the problem line I think....
    let messageController : MFMessageComposeViewController = MFMessageComposeViewController.alloc();

    /* In Objective-C it is: */
    // MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];

    // I have also tried this...
    let messageC : MFMessageComposeViewController! = MFMessageComposeViewController();


    messageController.messageComposeDelegate = self;
    messageController.recipients = recipients;
    messageController.body = message;

    self.presentViewController(messageController, animated: true, completion: nil);
}

I believe the issue is that when I run this the MFMessageComposeViewController is not fully instantiated. 我相信问题是,当我运行此消息时,MFMessageComposeViewController尚未完全实例化。 Therefore when it hits this code, instead of opening a message dialog the screen just turns black. 因此,当它命中此代码时,无需打开消息对话框,屏幕只会变黑。 I found that this is the issue when the same line is not init'ed in Objective-C. 我发现这是在Objective-C中未初始化同一行时的问题。

Thanks in advance for the help. 先谢谢您的帮助。

You are quite right. 你说的很对。 It is never being init -ed — because you are never init -ing it. 这是从来没有被init -ed -因为你永远不会init -ing它。 You are calling alloc , which is wrong. 您正在调用alloc ,这是错误的。

You never call alloc in Swift. 永远不会在Swift中调用alloc Well, you might, but not normally. 好吧,您可能会,但不是正常情况。 Just initialize. 只是初始化。 Like this: 像这样:

let messageController = MFMailComposeViewController()

That is in fact a call to init which is exactly what you want. 实际上,这就是您真正想要的对init的调用。

(You might want to stop and read up on the Swift language and how it calls initializers before you get into this any further, since you are clearly doing this utterly wrong everywhere in your code.) (您可能想先停止并继续学习Swift语言以及它如何调用初始化程序,因为显然在代码的所有地方这样做都是完全错误的。)

In Swift, to initialize: 在Swift中,要初始化:

 let smsComposer:MFMessageComposeViewController = MFMessageComposeViewController()

do not forget to set the delegate: 不要忘记设置委托:

  smsComposer.messageComposeDelegate = self

Objective -c Repo 目标-c回购

Swift Repo 迅捷回购

Apple docs 苹果文档

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

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