MFMailComposeViewController model does not display - iphone

I am presenting MFMailComposeViewController in my app for Email.
But the view is not getting disply , only upper SEND and CANCEL buttons getting displayed.
This is the screen shot of view that is getting display after clicking on "Email to Subscribe" button.
I have checked out all the related codes. I have added "MessageUI" framework and imported
MessageUI/MessageUI.h , MessageUI/MFMailComposeViewController.h
I have used following code :
- (void)viewWillAppear:(BOOL)animated {
[self.view setFrame:CGRectMake(0, 62, 320, 418)];
[APPDELEGATE.window addSubview:self.view];
[self.navigationController.navigationBar setHidden:NO];
self.navigationItem.title=#"Free Subscription";
[super viewWillAppear:animated]; }
-(void)viewWillDisappear:(BOOL)animated {
[self.view removeFromSuperview];
[super viewWillDisappear:animated]; }
-(IBAction)btnEmailPressed {
MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
if (Apicker != nil)
{
[Apicker setSubject:#"Free Subscription"];
[Apicker setMessageBody:#" " isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObjects:#"info#xyz.com", nil];
[Apicker setToRecipients:toRecipients];
Apicker.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
[self presentModalViewController:Apicker animated:YES];
}
else
{
}
[Apicker release];
} }
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError
*)error{
NSString *message;
switch (result) {
case MFMailComposeResultCancelled:
message =#"You have canceled your email.";
break;
case MFMailComposeResultFailed:
message=#"Your email is failed";
break;
case MFMailComposeResultSent:
message=#"Your email was successfully sent.";
break;
case MFMailComposeResultSaved:
message=#" Your email has been saved";
break;
default:
message=#" Your email is not send";
break;
}
UIAlertView *alt=[[UIAlertView alloc]initWithTitle:#"" message:message delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alt show];
[alt release];
[self dismissModalViewControllerAnimated:YES];
}
I could not get the reason for this problem.
Thanks.

Its for IOS 6
-(void)email{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"xyz#gmail.com", nil]];
[composer setSubject:#""];
// [composer.setSubject.placeholder = [NSLocalizedString(#"This is a placeholder",)];
[composer setMessageBody:#"" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:composer animated:YES completion:nil];
}
else {
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#",[error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Just try
For iOS 5
-(void)email:(id)sender{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"xyz#gmail.com", nil]];
[composer setSubject:#""];
// [composer.setSubject.placeholder = [NSLocalizedString(#"This is a placeholder",)];
[composer setMessageBody:#"" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
}
else {
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#",[error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
else {
[self dismissModalViewControllerAnimated:YES];
}
}

try modifying your code like this :-
-(IBAction)btnEmailPressed
{
MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
if (Apicker != nil)
{
[Apicker setSubject:#"Free Subscription"];
[Apicker setMessageBody:#" " isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObjects:#"info#xyz.com", nil];
[Apicker setToRecipients:toRecipients];
Apicker.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
[self presentModalViewController:Apicker animated:YES];
}
else
{
NSString *recipients = #"mailto:info#xyz.com?cc=&subject=";
NSString *body = #"&body=";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
[Apicker release];
}
}

add these two delegate in .h file MFMessageComposeViewControllerDelegate and MFMailComposeViewControllerDelegate
UPDATE
if ([MFMailComposeViewController canSendMail]) {
appDelegate.imgCapture = [self captureView];
[appDelegate.imgCapture retain];
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
NSString *mailBody = #"Set Your Body Message";
[mailComposeViewController setMessageBody:mailBody isHTML:NO];
mailComposeViewController.mailComposeDelegate = self;
[self presentViewController:mailComposeViewController animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"e-Mail Sending Alert"
message:#"You can't send a mail"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}

Related

Using MFMailController in UITabbar controller

I am using UITabbarcontroller in one of the viewcontroller, In one tabbar i need to have only Mail controller, when i am doing so , then that is entering into infinite loop, how would i overcome it, The Code related to
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mailViewController setToRecipients:[NSArray arrayWithObject:#"k.sourish.k#gmail.com"]];
[mailViewController setSubject:#"Subject Goes Here."];
[mailViewController setMessageBody:#"Your message goes here." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(#"Device is unable to send email in its current state.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#" Please Configure Your Mail Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[mailViewController release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { NSString *message = #"";
// Notifies users about errors associated with the interface
switch (result) {
case MFMailComposeResultCancelled:
message = #"Mail: canceled";
break;
case MFMailComposeResultSaved:
message = #"Mail: saved";
break;
case MFMailComposeResultSent:
message = #"Mail: sent";
break;
case MFMailComposeResultFailed:
message = #"Mail: failed";
break;
default:
message = #"Mail: not sent";
break;
}
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[controller dismissModalViewControllerAnimated:YES];
}
This is how I would do it
Drag and drop a UITabBarButton object on your UITabBar and call it "Email". Now create this IBAction
-(IBAction)composeMyEmail
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mailViewController setToRecipients:[NSArray arrayWithObject:#"k.sourish.k#gmail.com"]];
[mailViewController setSubject:#"Subject Goes Here."];
[mailViewController setMessageBody:#"Your message goes here." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(#"Device is unable to send email in its current state.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#" Please Configure Your Mail Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[mailViewController release];
}
Connect your IBAction to your "Email" button.
Remove all the code you have under viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

Present Social View Controller Cocos2d

Im trying to use the social framework to present the "Post To Facebook" view controller from within Cocos2d. This is the code I would usually use in a storyboard app
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebook = [[SLComposeViewController alloc] init];
facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebook setInitialText:[NSString stringWithFormat:#"text"]];
[self presentViewController:facebook animated:YES completion:nil];
[facebook setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Posted";
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setInteger:1 forKey:#"Shared"];
[ud synchronize];
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
}];
}
How would I get this up and running from within Cocos2d? currently it throws up a warning for the line
[self presentViewController:facebook animated:YES completion:nil];
Thanks in advance
In cocos2d 2.0 you can use [CCDirector sharedDirector] instead of self.
[[CCDirector sharedDirector] presentViewController:facebook animated:YES completion:nil];
This works because CCDirector inherits from UIViewController.
This works for me....
-(void) facebookWithInitialText:(NSString*) text {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
CCLOG( #"can post to Facebook");
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:text]; // set initial text
[controller addImage:[UIImage imageNamed:#"Icon-72.png"]]; //add an image
[controller addURL:[NSURL URLWithString:#"http://www.cartoonsmart.com"]]; //add a URL to it
[[app navController] presentViewController:controller animated:YES completion:nil ];
[controller setCompletionHandler:^(SLComposeViewControllerResult result){
[[app navController] dismissModalViewControllerAnimated:YES];
NSString *outout = [[NSString alloc] init];
switch (result) {
case SLComposeViewControllerResultCancelled:
outout = #"Post Cancled";
break;
case SLComposeViewControllerResultDone:
outout = #"Post Done";
default:
break;
}
UIAlertView *myalertView = [[UIAlertView alloc]initWithTitle:#"Facebook" message:outout delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[myalertView show];
}];
} else {
CCLOG( #"Facebook not accessible or one account not setup.");
}
}

How to have a precomposed SMS message for the user in iOS SDK

I used the sample code on the Apple Dev site to learn how to set pre-composed emails, but is there a way to set precomposed SMS messages, similarly?
First you have to add the framework MessageUI to your project and import the library "MessageUI/MessageUI.h". Then conform to the protocol <MFMessageComposeViewControllerDelegate>.
Now to send an SMS:
- (IBAction) sendSMS:(id)sender
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"The body of the SMS you want";
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
[controller release];
}
To catch the result of the sending operation:
- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch(result)
{
case MessageComposeResultCancelled: break; //handle cancelled event
case MessageComposeResultFailed: break; //handle failed event
case MessageComposeResultSent: break; //handle sent event
}
[self dismissModalViewControllerAnimated:YES];
}
The body property on MFMessageComposeViewController allows you to set the message body just like you can for an email.
Check out the documentation: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html
See this article on Apple's Dev Center:
Sending an SMS Message
PresentModalViewController is now deprecated in IOS 6. So i used
[self presentViewController:controller animated:YES completion:nil];
whole code is as following
-(IBAction)sendSMSButtonTouchupInside:(id)sender
{
MFMessageComposeViewController *controller =
[[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Whatever you want";
controller.recipients = [NSArray arrayWithObjects:#"03136602888", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyApp" message:#"Unknown Error"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
[alert show];
break;
case MessageComposeResultFailed:
[alert show];
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

mail application are not launching!

i am trying to implement the functionality of email in my application. i have added MessageUI-framework, along with header and MFMailComposeViewControllerDelegate protocol but i am facing problem. here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
if(isViewPushed == NO) {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
target:self action:#selector(email)] autorelease];
}
}
-(void) email
{
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:#"<html><body>"] retain];
[emailBody appendString:#"<p>type text here</p>"];
UIImage *emailImage = [UIImage imageNamed:#"20-gear2.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
[emailBody appendString:[NSString stringWithFormat:#"<p><b><img src='data:image/png;base64.....,.......%#'></b></p>",imageData]];
[emailBody appendString:#"</body></html>"];
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setSubject:#"My Inline Image Document"];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
if(! [MFMailComposeViewController canSendMail])
{
UIAlertView *cantMailAlert = [[UIAlertView alloc]
initWithTitle:#"cant email"
message:#"nt able to send email"
delegate:NULL
cancelButtonTitle:#"ok"
otherButtonTitles:NULL];
[cantMailAlert show];
[cantMailAlert release];
return;
}
MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease];
[mailController setMessageBody:#"can send my mail" isHTML:NO];
mailController.mailComposeDelegate = self;
[self presentModalViewController:mailController animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
if (error)
{
UIAlertView *cantMailAlert = [[UIAlertView alloc]
initWithTitle:#"mail error"
message: [error localizedDescription]
delegate:NULL
cancelButtonTitle:#"ok"
otherButtonTitles:NULL];
[cantMailAlert show];
[cantMailAlert release];
return;
}
NSString *resultString;
switch (result)
{
case MFMailComposeResultSent:
resultString = #"sent mail";
break;
case MFMailComposeResultSaved:
resultString = #"saved";
break;
case MFMailComposeResultCancelled:
resultString = #"cancel";
break;
case MFMailComposeResultFailed:
resultString = #"failed";
break;
}
if (resultString = #"saved")
{
NSString *msg = [NSString stringWithFormat:#"%# at %#\n", resultString, [NSDate date]];
UIAlertView *MailAlert = [[UIAlertView alloc]
initWithTitle:#"status"
message: msg
delegate:NULL
cancelButtonTitle:#"ok"
otherButtonTitles:NULL];
[MailAlert show];
[MailAlert release];
return;
}
[controller dismissModalViewControllerAnimated:YES];
[controller release];
//[self email];
}
but when i click on mail button then applictaion terminates and starts loading . it says can't able to store privious value!!
Why are you presenting two instances of MFMailcomposeviewcontroller in viewDidLoad? Why are you creating two objects namely emailDialog and mailController and presenting them?

MailComposeViewController - when i press button "Cancel" i don't see the panel with "Draft" ,"Save Draft" and "Cancel" buttons

I am using the MFMailComposeViewController Controller like this:
MFMailComposeViewController *picker1 = [[MFMailComposeViewController alloc] init];
picker1.mailComposeDelegate = self;
[picker1 setSubject:#"I have a pencil for you"];
UIImage *roboPic = [UIImage imageNamed:#"RobotWithPencil.jpg"];
NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
[picker addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"RobotWithPencil.jpg"];
NSString *emailBody = #"This is a cool image of a robot I found. Check it out!";
[picker1 setMessageBody:emailBody isHTML:YES];
picker1.navigationBar.barStyle = UIBarStyleBlack;
[self presentModalViewController:picker1 animated:YES];
[picker1 release];
When I press the "Cancel" button I don't see the panel with "Draft", "Save Draft" and "Cancel" buttons, the screen is locked/frozen but the panel with buttons mentioned above doesn't appear.
I will be glad to get any assistance.
Thanks in advance
Moshe
you have to use this for mail composing. and in interface file write this delegte MFMailComposeViewControllerDelegate . also import messageUi framework. its working.
#pragma mark Compose Mail
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposer
{
if(![MFMailComposeViewController canSendMail])
{
[self setUpMailAccount];
return;
}
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Strpy's Revange"];
[[picker navigationBar] setTintColor:[UIColor blackColor]];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#""];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"", #"", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#""];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
if([UIAppDelegate gameCodeFlag]==0)
{
NSString *emailBody = [NSString stringWithFormat:#"Game Score! %d",[UIAppDelegate scorePost]];
[picker setMessageBody:emailBody isHTML:NO];
}
else {
[UIAppDelegate readFriendPlist];
NSString *emailBody = [NSString stringWithFormat:#"Game Code! %#",[UIAppDelegate gameCode]];
[picker setMessageBody:emailBody isHTML:NO];
}
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Sending..."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed:
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Sending Failed - Unknown Error :-("
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[self dismissModalViewControllerAnimated:YES];
if([UIAppDelegate gameCodeFlag]==1)
{
[[CCDirector sharedDirector] pushScene:[StorePage scene]];
}
}