Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

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

  • Screen Shot 2015-05-28 at 10.09.56 PM.png
    Screen Shot 2015-05-28 at 10.09.56 PM.png
    135.3 KB · Views: 220
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)
 
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.
 
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:
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.