In-App Email Composition Won't Close

Discussion in 'iOS Programming' started by LegendaryW33nie, May 28, 2015.

  1. LegendaryW33nie macrumors newbie

    Joined:
    Mar 4, 2014
    #1
    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
     

    Attached Files:

  2. DannyBres macrumors 65816

    DannyBres

    Joined:
    Oct 30, 2007
    Location:
    UK
    #2
    I do not know any swift; Do you need break; statements in your switch?
     
  3. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #3
    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) 
     
  4. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #4
    No need for break in swift; except for empty cases you have included.
     
  5. mjohnson1212 macrumors member

    Joined:
    Nov 15, 2007
    #5
    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.
     
  6. ChristianJapan, May 29, 2015
    Last edited: May 29, 2015

    ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #6
    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.
     
  7. LegendaryW33nie thread starter macrumors newbie

    Joined:
    Mar 4, 2014
    #7
    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!
     

Share This Page