2

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

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • use this [self presentViewController:mail animated:YES completion:nil]; and in use this [self dismissViewControllerAnimated:YES completion:nil]; – Sudha Tiwari Feb 04 '13 at 05:28

5 Answers5

0

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.

onevcat
  • 4,591
  • 1
  • 25
  • 31
  • [self presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>, [self dismissViewControllerAnimated:<#(BOOL)#> completion:<#^(void)completion#>, what do I put inside the parenthesis for both present and dismiss, sorry this is new to me – user1985904 Feb 04 '13 at 05:18
  • It is called block and the code in the block will be called when the show/dismiss operation over. You can put whatever you want to do in it. If you do not want to do anything you can write ^{} as the completion. Like this `[self presentViewController:mailVC animated:YES completion:^{}];` Of course you can put your own code between the {}: `[self presentViewController:mailVC animated:YES completion:^{NSLog(@"Hello block");}];` – onevcat Feb 04 '13 at 05:58
  • everything is good now I get now errors but in the simulator when the button is clicked it still comes up as saying "whose view is not in the window hierarchy" and I have no idea how to fix that – user1985904 Feb 04 '13 at 06:04
  • That means your viewcontroller is not in the view hierarchy now. It seems there is something wrong with the viewcontroller connection or maybe you call the -Mail: from a wrong place? Could you paste some code about how did you add your viewcontroller? And you can have a check in Interface Builder's file owner to see if the "view" property is connected correctly. Maybe you can have a look at [this post](http://stackoverflow.com/questions/11862883/whose-view-is-not-in-the-window-hierarchy) – onevcat Feb 04 '13 at 06:14
  • I just dragged the action I created over to the button on the view controller and selected touch up inside is that what you mean – user1985904 Feb 04 '13 at 06:35
0

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.

Global nomad
  • 1,037
  • 12
  • 25
  • [self presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>, [self dismissViewControllerAnimated:<#(BOOL)#> completion:<#^(void)completion#>, what do I put in the areas that come up highlighted, sorry as I said this is new to me – user1985904 Feb 04 '13 at 05:12
  • For presentViewController:animated:completion: the first parameter modalViewController is the view that you intend to display the MFMailComposer above. In other words, the view displayed before you display MFMailComposer. The animated parameter is where you specify if you want the MFMailComposer to be animated (think of it as a visual effect) as it is displayed. Similarly for dismissViewControllerAnimated: As for the completion parameter, you can pass NULL if you're not using a completion handler. – Global nomad Feb 04 '13 at 05:23
  • I am still getting, when I click on my button, "whose view is not in the window hierarchy" – user1985904 Feb 04 '13 at 05:40
  • You need to connect the view either in Interface Builder or programmatically. – Global nomad Feb 04 '13 at 05:46
  • how is that done is there any code that you could show me or what I need to do, I am sorry you probably think I am dumb – user1985904 Feb 04 '13 at 05:52
0

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 .

DD_
  • 7,230
  • 11
  • 38
  • 59
Gautam Jain
  • 2,913
  • 30
  • 25
0
[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];
DD_
  • 7,230
  • 11
  • 38
  • 59
0

Hi you can check MFMailComposerViewController class present or not. -(void)email{Class emailClass=(NSClassFromString(@"MFMailComposeViewController"));if emailClass!=nil)if ([emailClass canSendMail]{[self displayComposePage]; }

  • if anyone wants to target their app **below iOS3.0** that advice is brilliant. otherwise... – holex May 18 '15 at 15:28