I am trying to pull up a Mail compose view controller after pushing a button from a UIAlertController. In simulator, I get the normal crash and error message I always get in Simulator when trying to open mail, but in app, I get nothing. No controller, no crash, nothing. Here is the code.
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Email" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self emailThem];
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
-(void) emailThem {
NSLog(#"EMAIL");
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"To P3 Media"];
[mail setMessageBody:#"Replace this text with your message for all the information you would like on P3 Media." isHTML:NO];
[mail setToRecipients:#[#"p3media2014#gmail.com"]];
[self presentViewController:mail animated:YES completion:NULL];
}
What is the problem?
Try This Code
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Email" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self emailThem];
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
-(void) emailThem {
NSLog(#"EMAIL");
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"To P3 Media"];
[mail setMessageBody:#"Replace this text with your message for all the information you would like on P3 Media." isHTML:NO];
[mail setToRecipients:#[#"p3media2014#gmail.com"]];
dispatch_async(dispatch_get_main_queue(),^{
[self presentViewController:mail animated:YES completion:NULL];
});
}
Related
My application terminates showing Message from debugger: Terminated due to signal 9 after doing rigorous searching didn't find any thing , i have also checked for memory leaks but does not find any..
Problem Statement - When i open camera from my app and after capturing image when i select use image my app terminates.
My Code
- (void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I used to select image from both camera and gallery using imagepicker with uialertactionsheet. This code is working for me, so you try this. And use these delegate's
- (IBAction)Camera_Click:(id)sender
{
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// Photos from Gallery by calling Method
UIAlertAction *choose = [UIAlertAction actionWithTitle:#"Select from photos" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self performSelector:#selector(choosePhotoFromGallery)];
[alertView dismissViewControllerAnimated:YES completion:nil ];
}];
//Take New Photo From Camara
UIAlertAction *Capture = [UIAlertAction actionWithTitle:#"Capture from camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self performSelector:#selector(takePhotoFromCamara)];
[alertView dismissViewControllerAnimated:YES completion:nil ];
}];
UIAlertAction *Cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
}];
//Adding the alertView Actions
[alertView addAction:choose];
[alertView addAction:Capture];
[alertView addAction:Cancel];
//Presents the AlertView
[self presentViewController:alertView animated:YES completion:nil];
}
- (void)takePhotoFromCamara
{
UIImagePickerController *Take_pic = [[UIImagePickerController alloc] init];
Take_pic.delegate = self;
Take_pic.allowsEditing = YES;
Take_pic.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:Take_pic animated:YES completion:NULL];
}
- (void)choosePhotoFromGallery
{
UIImagePickerController *Choose_picker = [[UIImagePickerController alloc] init];
Choose_picker.delegate = self;
Choose_picker.allowsEditing = YES;
Choose_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:Choose_picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *imgPicker = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.your_imgView.image = imgPicker;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
**Finally you add this two in your app info.plist file, otherwise app will crash.
<key>NSPhotoLibraryUsageDescription</key>
<string><$YourAppname$>We access your photo library.</string>
<key>NSCameraUsageDescription</key>
<string><$YourAppname$>We access your camera.</string>**
So I know that i would have to use the MFMailComposeResultSent. (At least I think that's what i'm supposed to use) This is the code that i have and it sends the email and everything fine but the mail composer stays up.
EDIT: Here is my code
if ([condition isEqual: #"Excellent"] && [MFMailComposeViewController canSendMail]) {
NSString *emailBody = [NSString stringWithFormat:#"Product:%# Make:%# Year Manufactured:%# Description:%# Condition:Excellent Email:%#",inputProduct,inputMake,inputYear,inputDescript, inputEmail];
NSArray *recipient = [NSArray arrayWithObject:#"LoveShackElectronics#gmail.com"];
MFMailComposeViewController *SuperLovedEmail = [[MFMailComposeViewController alloc]init];
[SuperLovedEmail setTitle:emailTitle];
[SuperLovedEmail setToRecipients:recipient];
[SuperLovedEmail setMessageBody:emailBody isHTML:NO];
[SuperLovedEmail setUserActivity:false];
[self presentViewController:SuperLovedEmail animated:YES completion:nil];
}
else {
UIAlertController *emailAlert = [UIAlertController alertControllerWithTitle:#"Oh No!" message:#"Your Phone is not able to send an email currently or you have not chosen a condition. Please make sure you have chosen a condition and that you are signed in through Mail" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *emailAlertAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * emailAlertAction) {}];
[emailAlert addAction:emailAlertAction];
[self presentViewController:emailAlert animated:YES completion:nil];
}
You need to set the mailComposeDelegate property of your SuperLovedEmail object to be self, then handle the didFinishWithResult message, like this:
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[controller dismissViewControllerAnimated:true completion:nil];
}
You might find it useful to evaluate the MFMailComposeResult to see whether sending actually happened.
I have created a new app under xcode 6 and an old cold no more work.
Since iOS 8 we use UIalertcontroller to show action sheet
I use it to lauch photolibrary and select a picture but when I want to dismiss the picker, my app crash and I don't know why.
Below my code:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
UIAlertController * uiViewActionSheet= [UIAlertController
alertControllerWithTitle:#"Action"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* chooseExisting = [UIAlertAction
actionWithTitle:NSLocalizedString(#"CHOOSE_EXISTING",#"Choose Existing")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
[uiViewActionSheet dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[uiViewActionSheet dismissViewControllerAnimated:YES completion:nil];
}];
[uiViewActionSheet addAction:chooseExisting];
[uiViewActionSheet addAction:cancel];
[self presentViewController:uiViewActionSheet animated:YES completion:nil];
Below the imagepicker function :
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.uImageProfile.image = chosenImage;
self.lstatutImage.text = #"save";
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
}
__block NSString* resultSaveImage = #"";
//Save image onthe server
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
resultSaveImage = [controllerObject saveProfileImage:self.uImageProfile.image];
dispatch_async(dispatch_get_main_queue(), ^{
if(![resultSaveImage isEqualToString:#"\"ok\""])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"ERROR",#"Error") message:resultSaveImage delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
});
});
}
My code crash here :
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Have you any idea ?
You're dismissing the picker, and then calling it:
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera) {...
You also don't need the [uiViewActionSheet dismissViewControllerAnimated:YES completion:nil]; lines!
Try moving the dismiss line to the end. If it still crashes, it might be that the presentingViewController is not alive anymore.
I am very new to iOS development and have encountered an error that I just can't seem to find a solution for. I have searched for solutions everywhere, but maybe it is my newness that is preventing me from seeing the problem.
The exact warning that is printed in the log is:
Attempt to dismiss from view controller <_UIAlertShimPresentingViewController: 0x7aaa4b90> while a presentation or dismiss is in progress!
It occurs right after I touch a button on the actionSheet.
Here is the code:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
if (buttonIndex == 0) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
else if (buttonIndex == 1) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
else if (buttonIndex == 2) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:nil];
}
} else {
if (buttonIndex == 0) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:NO completion:NULL];
}
else if (buttonIndex == 1) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:NULL];
}
}
}
The implementation of the actionSheet, I have the IBAction connected a toolbar button located an the .xib file.
- (IBAction)addImage:(id)sender {
UIActionSheet *popUpSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles: nil];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[popUpSheet addButtonWithTitle:#"Camera"];
[popUpSheet addButtonWithTitle:#"Photo Library"];
[popUpSheet addButtonWithTitle:#"Camera Roll"];
[popUpSheet addButtonWithTitle:#"Cancel"];
popUpSheet.cancelButtonIndex = popUpSheet.numberOfButtons-1;
} else {
[popUpSheet addButtonWithTitle:#"Photo Library"];
[popUpSheet addButtonWithTitle:#"Camera Roll"];
[popUpSheet addButtonWithTitle:#"Cancel"];
popUpSheet.cancelButtonIndex = popUpSheet.numberOfButtons-1;
}
[popUpSheet showFromBarButtonItem: self.toolbarItems[0] animated:YES]; }
Everything has been delegated correctly from what I can tell:
DetailViewController.m
#interface DetailViewController () < UINavigationControllerDelegate, UIImagePickerControllerDelegate >
DetailViewController.h
#interface DetailViewController : UIViewController <UIActionSheetDelegate>
Any insight would be greatly appreciated and extremely helpful.
Your code looks correct. Try using the:
actionSheet:didDismissWithButtonIndex:
method. It's sent to the delegate after the animation ends. Hope it helps.
Finally figured out the problem. The compiler was giving me this warning because Apple recently combined UIActionsheet and UIAlert into one controller type called UIAlertController. Used the new UIAlertController and my problem was solved.
- (IBAction)addImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
UIAlertController *popUpSheet = [UIAlertController alertControllerWithTitle:nil message:#"Select your Choice" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *photoLibrary = [UIAlertAction actionWithTitle:#"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
NSLog(#"Photo Library was touched");
[popUpSheet dismissViewControllerAnimated:YES completion: nil];
}];
UIAlertAction *recentPhotos = [UIAlertAction actionWithTitle:#"Camera Roll" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:nil];
NSLog(#"Camera Roll was touched");
[popUpSheet dismissViewControllerAnimated:YES completion: nil];
}];
UIAlertAction *camera = [UIAlertAction actionWithTitle:#"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
NSLog(#"Camera was touched");
[popUpSheet dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[popUpSheet dismissViewControllerAnimated:YES completion:nil];
}];
[popUpSheet addAction:photoLibrary];
[popUpSheet addAction:recentPhotos];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[popUpSheet addAction:camera];
}
[popUpSheet addAction:cancel];
[self presentViewController:popUpSheet animated:YES completion:nil];
}
Here is my code
-(IBAction)emailButtonPressed :(UIButton *)sender {
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"Mail has not been set up on this device" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
return;
}
NSString *targetFile = [self saveCompleteImage];
MFMailComposeViewController *mailpicker = [[MFMailComposeViewController alloc] init];
[mailpicker setMailComposeDelegate:self];
NSString *mimeType = [StringHelper getMimeType:targetFile];
[mailpicker addAttachmentData:[NSData dataWithContentsOfFile:targetFile] mimeType:mimeType fileName:[targetFile lastPathComponent]];
[mailpicker setSubject:[self.currentDocument getNameForUntitled]];
mailpicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self.presentedViewController presentViewController:mailpicker animated:YES completion:nil];
}
It's not presenting mailpicker. Kindly tell me where am i wrong.
Issue is with this code:
[self.presentedViewController presentViewController:mailpicker animated:YES completion:nil];
You need to use:
[self presentViewController:mailpicker animated:YES completion:nil];
or
[self.presentingViewController presentViewController:mailpicker animated:YES completion:nil];
Check ModalViewControllers Reference to understand the difference between presentedViewController and presentingViewController