简体   繁体   中英

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.

I have copied it to the best of my ability, however there is one portion that I cannot find a Swift equivalent too. 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. 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.

Thanks in advance for the help.

You are quite right. It is never being init -ed — because you are never init -ing it. You are calling alloc , which is wrong.

You never call alloc in Swift. 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.

(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.)

In Swift, to initialize:

 let smsComposer:MFMessageComposeViewController = MFMessageComposeViewController()

do not forget to set the delegate:

  smsComposer.messageComposeDelegate = self

Objective -c Repo

Swift Repo

Apple docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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