Reputation: 5
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
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
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
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