views:

116

answers:

3

I am trying to send an SMS on an iPhone using MFMessageComposeVieController and I want to add a listener that recognizes when the SMS is sent (in other words, when the user presses "Send"). What is the syntax for this?

For example, I know that with a textField, an example of a listener would be: [textField addTarget:self action:@selector(methodName) forControlEvents:UIControlEventEditingDidEndOnExit];

A: 

You want to add a delegate to your MFMessageComposeViewController. In the delegate's messageComposeViewController:didFinishWithResult: method, you can check the result parameter to see whether the user canceled or sent the SMS.

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller 
                 didFinishWithResult:(MessageComposeResult)result {
        switch(result) {
        case MessageComposeResultCancelled:
            // user canceled sms
            break;
        case MessageComposeResultSent:
            // user sent sms
            break;
        case MessageComposeResultFailed:
            // sms send failed
            break;
        default:
            break;
}
Art Gillespie
So there is no way to do it without using a delegate?
Robert Eisinger
No you need to implement the delegate. That is what it is for. You could use the delegate to fire a Notification if you require elsewhere to find out about the SMS being sent.
JonB
+1  A: 

Google is very helpful...

Third result is an SMS tutorial.

Relevant code:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                 didFinishWithResult:(MessageComposeResult)result
{
    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"Cancelled");
            break;
        case MessageComposeResultFailed:
            UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:@"MyApp" 
                                message:@"Unknown Error"
                               delegate:self 
                      cancelButtonTitle:@”OK” 
                      otherButtonTitles:nil];
            [alert show];
            [alert release];
            break;
        case MessageComposeResultSent:

            break;
        default:
            break;
    }

    [self dismissModalViewControllerAnimated:YES];
}

Implement the MessageComposeResultSent case to know when the message has been sent.

Matt Long
A: 

hi Thank you for your post....Can i know whether we can auto send the sms at a particular time and date....

Ramkumar