Tommy Wine'ass
Tommy Wine'ass

Reputation: 5

MFMessageComposeViewController dismiss keyboard

i'm having troubles with my MFMessageComposeViewController. I would like to use SMS in-app. Everything work fine for sending SMS, so far so good. But when i hit the cancel button (or send button too) top of my view disapeared but the keyboard did not. It's maybe because i don't use modale view, but only a addSubview.

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{

    if (result == MessageComposeResultCancelled)
    {
        NSLog(@"Message annulé");
        [controller resignFirstResponder];
        [controller.view removeFromSuperview];  
        [controller release]; 
     }
    else if (result == MessageComposeResultSent)
    {
        NSLog(@"Message envoyé");
        ...
    }
    else 
    {
        NSLog(@"Message non envoyé");
        ...
    }
}

-(void)sendSMS:(NSString *)bodyOfMessage :(Phone *)recipient
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

    if([MFMessageComposeViewController canSendText])
    {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate = self;

        NSMutableArray *toRecipients = [[NSMutableArray alloc]init];
        [toRecipients addObject:recipients.phoneNumber];
        [picker setRecipients:(NSArray *)toRecipients];
        [toRecipients release];

        NSString *bodyString = nil;
        bodyString = bodyOfMessage;
        [picker setBody:bodyString];

        [self addSubView:picker.view];
        [picker release];
     }  
}

Any idea ? Had I to use only modalView ?

sorry for spelling mistake...

Thank you. Tommy

Upvotes: 0

Views: 1857

Answers (3)

a_alexeev
a_alexeev

Reputation: 278

Try close existing keyboard, before presenting modal view controller with MFMessageComposeViewController:

[self.view endEditing:YES]; //close keyboard if it opened
[self presentModalViewController:messageController animated:YES];

Upvotes: 0

Bob de Graaf
Bob de Graaf

Reputation: 2722

Yes, you have to use the modalviewcontroller.

[self presentModalViewController:picker];

Also, you're creating two instances of the MFMessageComposeViewController, first for checking if it can send text and then another to actually show it. I advise to create just one, it's better for the memory :) also the first one is leaking since you didn't release it. Good luck!

Upvotes: 1

NeverBe
NeverBe

Reputation: 5038

    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate = self;
        NSString *bodyString = nil;

        NSMutableArray *toRecipients = [[NSMutableArray alloc]init];
        [toRecipients addObject:@"phone here"];
        [picker setRecipients:(NSArray *)toRecipients];
        [toRecipients release];

        bodyString = [NSString stringWithFormat: @"Message body"];
        [picker setBody:bodyString];

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

Upvotes: 0

Related Questions