I am developing an app that runs on all the deployment target simulators (5.0-6.1) and on my iPhone 3GS, 4, 4S and a gen 2 iPad. I had the opportunity today to try running it on an iPad Mini. I works everywhere except when I try to segue to a MFMailComposeViewController object to send an email, which causes it to crash with an exception.
I use the code directly from the MailComposer sample project, but it always crashes when it calls presentModalViewController:animated:. So I tried presentViewController:animated:completion: as the other method is deprecated, but it still doesn't work.
I linked to MessageUI.framework imported the classes:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
The delegate is set. Here is the code:
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Contact Email"];
// Set up recipient
NSArray *toRecipients = [NSArray arrayWithObject:#"info#foo.bar"];
[picker setToRecipients:toRecipients];
// [self presentModalViewController:picker animated:YES];
[self presentViewController:picker animated:YES completion:NULL];
}
Might there be a bug causing this in the iPad Mini? I don't have any other new devices I can try it on so I'm not sure if its a Mini problem or something bigger. Any help would be appreciated as I'm ready to submit to Apple but I sure don't want to do that with a crashing bug.
It's likely that a mail account has not been set up or for some other reason cannot send email.
Be sure to call the + (BOOL)canSendMail function of MFMailComposeViewController first.
Try wrapping your MFMailComposeViewController code with
if ( [MFMailCompseViewController canSendMail])
I'd guess the device doesn't have mail setup on it.
Related
UIModalPresentationPageSheet is not working in iOS8.
MyViewController *myViewController=[[MyViewController alloc] init];
myViewController.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:myViewController animated:YES completion:nil];
This code works just fine in iOS6 and iOS7. But with iOS8 it just gives a full screen view just like UIModalPresentationFullScreen.
How can I have iOS7-UIModalPresentationPageSheet-like view in iOS8? Thanks in advance.
Looks like an early beta bug. Make sure to open a bug report with Apple at https://bugreport.apple.com.
I was coding a new iPhone App with iOS 7 and iOS 7.1 Beta 5.
Not iOS 7.1 released for everybody and my MFMailComposeViewController isn't working anymore.
This is my code:
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
[mail setSubject:#"Test"];
[mail setToRecipients:#[#"alexander#sn0wfreeze.de"]];
[mail setMessageBody:#"Test" isHTML:NO];
[mail setMailComposeDelegate:self];
[self presentViewController:mail animated:YES completion:nil];
Well I think that has to be a serious bug in iOS 7.1
What do you say?
I tried it again in a sample App, which could only present the mail view controller.
The Problem is that it doesn't allows the user to interact and it doesn't set the recipient and the message body. As you can see in my screenshot below:
EDIT:
In the simulator it works perfectly!
So please test it on a device
Greetings,
Alexander Heinrich
MFMailComposeViewController * mailim = [[MFMailComposeViewController alloc]init];
[mailim setMailComposeDelegate:self];
NSString * mailara =#"xxx#yandex.ru";
NSArray *emailArray = [[NSArray alloc]initWithObjects:mailara, nil];
[mailim setToRecipients:emailArray];
[mailim setSubject:#"Bilgi Almak Istiyorum"];
[self presentViewController:mailim animated:YES completion:nil];
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
I fixed the problem by restarting the iPhone.
Never came across with such problems before...
I hope this won't occur more often
I want to apologize in advance because I am fairly new to programming, so if I am not as specific as I can be I am sorry but I will try to explain my problem as best as I can anyways, I am creating an app that needs to have the ability to send emails and I have looked everywhere, tried every sample code I could find and nothing seems to work every time I use code I get the following error:
2013-02-03 20:23:39.372 Tones[16409:c07] Warning: Attempt to present
on
whose view is not in the window hierarchy!
This is the code I am currently using in the viewcontroller.h file:
UIViewController <MFMailComposeViewControllerDelegate>
- (IBAction)Mail:(id)sender;
and this is in my viewcontroller.m file:
- (IBAction)Mail:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Subject"];
NSArray *recipient = [NSArray arrayWithObjects:#"mail#example.com", nil];
[mail setToRecipients:recipient];
NSString *body = #"body!";
[mail setMessageBody:body isHTML:NO];
[self presentModalViewController:mail animated:YES];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
}
I also get a message that says both self presentModalViewController and self dismissModalViewController is deprecated in IOS 6 so does that mean I cant use it or am I doing something wrong?
So any help on what I am doing wrong with the mail composer would be much appreciated and again im sorry if I was not specific enough thanks in advance
You can use presentModalViewController:animated: to show the modal viewcontroller, but it is now recommended to use the new one: presentViewController:animated:completion:. The new on owns a completion handler and you can get more control of the code. Be careful the new method required a iOS 5.0 above. If your target is iOS5.0 above, you should use the new method. And the same for dismissModalViewControllerAnimated:, use dismissViewControllerAnimated:completion: instead.
The warning "Warning: Attempt to present on whose view is not in the window hierarchy!" suggests the view is not connected in the Interface Builder or programmatically.
The Deprecated warnings result from Xcode checking the APIs you use in your project settings. If you set the Build Settings' IOS Deployment Target of your Xcode project to iOS 6, then any APIs (such as presentModalViewController and dismissModalViewController) that are marked by Apple as deprecated will be flagged.
Instead use presentViewController:animated:completion: and dismissViewControllerAnimated:completion:, respectively.
[self presentModalViewController:mail animated:YES];
can be replaced by
[self presentViewController:mail animated:YES completion:nil];
and
[self dismissModalViewControllerAnimated:YES];
by
[self dismissViewControllerAnimated:YES completion:nil];
Like Sudha said, use [self presentViewController:mail animated:YES/NO completion:nil];
From iOS6 onwards, presentModalViewController and dismissModalViewController are deprecated, they are used with completion, which would be nil for your case .
Hi you can check MFMailComposerViewController class present or not.
-(void)email{Class emailClass=(NSClassFromString(#"MFMailComposeViewController"));if emailClass!=nil)if ([emailClass canSendMail]{[self displayComposePage];
}
This is a very odd problem.
I include the .h file of the next view:
#import "MainGame.h"
The I have this code to switch rooms when I press the screen:
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
MainGame *newview = [[MainGame alloc] initWithNibName:#"MainGame" bundle:nil];
newview.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:newview animated:YES completion:NULL];
}
And it works fine in the iOS 5 simulator; but when I test it on my iPod running iOS 4.2 it always just crashes.
All my IBOutlets are connected correctly and everything, as I've already said it works fine in the simulator but not on my iPod.
I have had each individual view working on my iPod before, so I am not using any features which require iOS 5 or anything; it's just this code that switches views that is not working.
Am I missing something?
Thanks!
I believe its a version issue
[self presentViewController:newview animated:YES completion:NULL];
doesn't exist in iOS 4.2 but does in iOS 5
try this instead
[self presentModalViewController:newview animated:YES];
According to the Apple documentation, presentViewController is iOS 5.0 and above.
I have the following block of code which works fine in the simulator and on most devices, on some devices however (all on the same iOS version 4.2.1) the app is crashing when it gets to the [mailComposer release] call, does anyone have any insight on why this would be happening?
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:[self.webView stringByEvaluatingJavaScriptFromString:#"document.title"]];
[mailComposer setMessageBody:[NSString stringWithFormat:#"Hello, \n\n Here is the link we discussed. \n %#", [self.webView.request URL]] isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
I had this exact same problem and I really have NO idea why it would crash, because presentModalViewController is supposed to retain the view controller. After fighting with it, I finally just added a property on my view controller which retained the reference to the mfMailComposeViewController and it worked fine. :/
MFMailComposeViewController* mfMailComposeViewController;
#property (nonatomic, retain) MFMailComposeViewController *mfMailComposeViewController;
then..
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:subject];
[controller setBody:body];
self.mfMailComposeViewController = controller;
[controller release];
Ok so I managed to figure out what was causing my issue with the help of my new iPad 2.
The issue was caused by not having any mail accounts configured, simply adding
if ([MFMailComposeViewController canSendMail])
before I create and present the view prevents the crashing, in my else block I added an UIAlertView to let the user know they cannot access that feature without first configuring a mail account.