Reputation: 702
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.
Upvotes: 1
Views: 4264
Reputation: 1133
In Swift, to initialize:
let smsComposer:MFMessageComposeViewController = MFMessageComposeViewController()
do not forget to set the delegate:
smsComposer.messageComposeDelegate = self
Upvotes: 0
Reputation: 535989
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.)
Upvotes: 2