iOS In-App Email Composition Won't Close

LegendaryW33nie

macrumors newbie
Original poster
Mar 4, 2014
7
0
Hey Everyone,

I've created an application and I have a button that allows user to email me suggestions for improvements on what could be better. I have successfully been able to add the compose email pop-up but it will not close no matter what. Sending, saving and deleting all do not close the mail window and I cannot get back to my application. The code is attached via image. Any help is appreciated and I am pretty new at app development so go easy on me!

Thanks,

RH
 

Attachments

xStep

macrumors 68020
Jan 28, 2003
2,013
100
Less lost in L.A.
Assuming you have confirmed that the delegate function has been called, perhaps that dismissal line should be
Code:
[COLOR="Red"]controller.[/COLOR]dismissViewControllerAnimated(false, completion: nil)
 

mjohnson1212

macrumors member
Nov 15, 2007
92
11
Your dismiss probably isn't getting called. I'd put a breakpoint on it.

If self is a view controller then your dismiss should work. I use MFMailComposeViewController in my app the same way.

presentViewController and dismiss inside the didFinishWithResult callback.
 

ChristianVirtual

macrumors 601
May 10, 2010
4,096
267
日本
ok, found your little typo:

you have declared the protocol function for the Mail Composer within your button action.

After your
Code:
else 
{
println("No eMail")
}
you need to place another
Code:
}
to close your action method first.

The method mailComposeController should be on your view controller (as the class to implement the protocol). The way you coded it the protocol implementation function is with in the action; syntax is correct; but intention is wrong.

based on this stackoverflow (your code look like the same ;) )

Code:
class ViewController: UIViewController , MFMailComposeViewControllerDelegate
{

   override func viewDidLoad()
   {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
   }

   override func didReceiveMemoryWarning()
   {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
   }

   override func viewDidAppear(animated: Bool)
   {
   }
   
   @IBAction func sendMail(sender: UIButton)
   {
      var emailTitle = "MR Support"
      var messageBody = "Mail Composer in Swift"
      var toRecipents = ["@me.com"]
      
      var mc: MFMailComposeViewController = MFMailComposeViewController()
      
      if MFMailComposeViewController.canSendMail()
      {
         mc.mailComposeDelegate = self
         mc.setSubject(emailTitle)
         mc.setMessageBody(messageBody, isHTML: false)
         mc.setToRecipients(toRecipents)
         
         self.presentViewController(mc, animated: true, completion: nil)
      }
      else
      {
         println("No Mail")
      }


   }        // <- this one is missing !!

   

   func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError)
   {
      switch result.value
      {
      case MFMailComposeResultCancelled.value:
         NSLog("Mail cancelled")
      case MFMailComposeResultSaved.value:
         NSLog("Mail saved")
      case MFMailComposeResultSent.value:
         NSLog("Mail sent")
      case MFMailComposeResultFailed.value:
         NSLog("Mail sent failure: %@", [error.localizedDescription])
      default:
         break
      }
      self.dismissViewControllerAnimated(false, completion: nil)
   }
}
Philosophical sidenote: the standard indent of the { and } as in Swift or ObjectiveC or other C's I don't like exactly for this reason; difficult to see the levels of brackets. I prefer to have them in open and close on the same position (I even spend always the time to change auto generated code; I just can't stand it.
But thats my style I feel comfortable with.
 
Last edited:

LegendaryW33nie

macrumors newbie
Original poster
Mar 4, 2014
7
0
That worked ChristianJapan! I'm surprised that one misplaced bracket caused that whole problem. Next time I will make sure to go over my code in more detail! Thank-you everyone for the help! Much Appreciated!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.