Cancel button does not dismiss ComposeMail window in iOS simulator - objective-c

My implementation is pretty simple:
In the .h file, I'm implementing MFMailComposeViewControllerDelegate
And in the .m file, I have the following bit of code:
-(void)MailCurrentViewAsAttachment
{
if ( [MFMailComposeViewController canSendMail] ) {
MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.delegate = self;
[mailComposer addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"attachment.jpg"];
[self presentViewController:mailComposer animated:YES completion:nil];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
The variable imageData above is of the UIImage type, and I know for sure there's nothing wrong with it: the required image shows up properly in the compose mail window.
However clicking the Cancel button does not dismiss the Compose window. What am I missing?
Note: I'm using iOS 6 with the latest version of xcode, and my app is a Universal app.

You are setting the wrong delegate. You want:
mailComposer.mailComposeDelegate = self;
MFMailComposeViewController extends UINavigationController. So setting delegate is for the UINavigationControllerDelegate.

Related

nav bar nonresponsive after UIImagePickerController dismissed

On iOS 7 only, the navigation bar in my app does not respond to any touches after a UIImagePickerController is used and then dismissed (whether a pic has been selected or not). The screen below the navigation bar functions as normal, but it is now impossible to navigate Back in the app; the user is stuck on this screen.
I am launching the UIImagePickerController from code, though the rest of the app is laid out in storyboards.
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
mediaUI.mediaTypes = [NSArray arrayWithObject:(NSString *) kUTTypeImage];
mediaUI.allowsEditing = NO;
mediaUI.delegate = self;
[controller presentModalViewController: mediaUI animated: YES];
Thanks in advance for any help.
I hope you are performing these steps correctly!
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
And when you are calling
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
Cheers!
I noticed that the log was showing "Unbalanced calls to begin/end appearance transitions for" the launching controller. I was launching the image picker controller immediately from another controller when it appeared. This works OK on iOS 8 but there needs to be a delay on iOS 7. I fixed it by calling my method after a brief delay:
[self performSelector:#selector(takePicture) withObject:nil afterDelay:.1];

UIPopoverController and UIImagePickerController crash

Here is the set up of my view:
When the UIBarButtonItem is clicked, it should bring up a UIImagePickerController. I have to do this using a UIPopoverController, which is invoked by clicking on the "reset" button, because it is necessary on the iPad. Here is my code:
-(IBAction) btnReset:(id)sender {
[self chooseImage];
}
-(void) chooseImage {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.allowsEditing = NO;
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.navigationBar.opaque = true;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagepicker];
[popoverController presentPopoverFromBarButtonItem:reset permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
} else {
[self presentModalViewController:imagepicker animated:YES];
}
}
}
However, when this is called, the view crashes with the error:
'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
What am I doing wrong? Thank you in advance.
It looks like you are trying to create a popover on an item which is not on the view hierarchy. If this method is being invoked by your button then change the method header to -(void) chooseImage:(id)sender and try presenting the popover from the UIBarButton you have on your toolbar.
Also if you are using ARC (which it looks like you are) you have to hold on to your UIPopover otherwise it will be released when it is still required see this stack overflow post. You may already be doing this but I thought I would raise it as a I can't see if/how you have specified your popoverController.

Dismiss mailComposeController

I am trying to dismiss the mail from my app after it's done if user sends or cancels.
But for some reasone this never dismisses. I tried almost everything.
I have also logged this so I will see if it went to dissmiss method. And the problem is there since it never enters the dismiss method.
What am i doing wrong???
- (IBAction)sendmail:(id)sender{
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imageData = UIImageJPEGRepresentation(image, 1.0);
if ( [MFMailComposeViewController canSendMail] ) {
MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.delegate = self;
[mailComposer addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"attachment.jpg"];
[mailComposer setSubject:#"Hello from My App!"];
NSString *emailBody = #"Sent from My App, Still not in AppStore!";
[mailComposer setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:mailComposer animated:YES];
}
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
NSLog (#"mail finished"); // NEVER REACHES THIS POINT.
}
you could replace this line:
[self dismissModalViewControllerAnimated:YES];
with the following line:
[controller dismissModalViewControllerAnimated:YES];
MFMailComposeViewController class inherits from UINavigationController and so its delegate property is 'delegate' for navigation controller 'part' of the class. To handle specific mail composer delegate methods you need to set your object as mailComposeDelegate property:
mailComposer.mailComposeDelegate = self;
SWIFT 5.0:
If you implement the MFMailComposeViewControllerDelegate protocol, you only have to contain the following function in ViewController:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
{
controller.dismiss(animated: true, completion: nil)
}
This function handles everything for you. If the user sends the email the view disappears automatically.
Any further information:
mailComposeController

presentModalViewController:picker not working in iPad MFMailComposeViewController

i am using the following code for the mail composer sheet in iPad application. I used the same code for iPhone. It worked.
I am writing the game in iPad using cocos2d. The game is in landScape mode. The control in EmailScene is stopping at [picker presentModalViewController:picker animated:YES]; It is not giving any error. Should I change my code for iPad ?
#interface EmailScene : CCScene <MFMailComposeViewControllerDelegate>
{
MFMailComposeViewController *picker;
}
-(void)displayComposerSheet;
#end
#implementation EmailScene
- (id) init {
self = [super init];
if (self != nil) {
[self displayComposerSheet];
}
return self;
}
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{
[[CCDirector sharedDirector] pause];
picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
//Fill in the email as you see fit
NSArray *toRecipients = [NSArray arrayWithObject:#"srikanth.rongali786#gmail.com"];
[picker setToRecipients:toRecipients];
//display the view
[[[CCDirector sharedDirector] openGLView] addSubview:picker.view];
[[CCDirector sharedDirector] stopAnimation];
//When I commented the following two lines the mail page is opening.
//[picker presentModalViewController:picker animated:YES];
//[picker release];
}
But, the problem is my game is in landscape mode and the mail sheet is displayed in portrait mode.
Thank you.
You are using -presentModalViewController:… wrongly. This method should be called on the topmost view controller before the "picker" is presented.
[topmostViewController presentModalViewController:picker animated:YES];
(You shouldn't add picker.view as a subview of the -openGLView either.)

UIViewController dismissModalViewControllerAnimated: causes main window to disappear

I want to present a modal mail dialogue like so in the iPad app:
MFMailComposeViewController* picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:title];
[picker setMessageBody:[NSString stringWithFormat:[self emailBody], title, [link absoluteString]] isHTML:YES];
[self.viewController presentModalViewController:picker animated:YES];
The following delegate is called when the user sends/cancels:
- (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self.viewController dismissModalViewControllerAnimated:YES];
}
This works great in portrait mode. In landscape mode the right hand pane of the UISplitViewController completely disappears.
You can only present these from the primary view of your application. In this case, presenting from the UISplitViewController works.