0

I am only coding in two features. MailComposer and WebView. But when I run my app and go to the email interface tab it crashes "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)". When I go to the tab that displays the webview, it works fine. The Webview.loadRequest(request) is messing something up with the mail coding. But I don't know where I can put it to make both features work.

If anyone has any clue how to fix this, please let me know. Thanks.

Here is the coding for the View.Controller.Swift file...

import UIKit
import MessageUI


    class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

        @IBOutlet weak var Webview: UIWebView!


        @IBOutlet weak var Subject: UITextField!

        @IBOutlet weak var Body: UITextView!




         var URLPath = "http://google.com"

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


        }


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




        @IBAction func SendEmail(sender: AnyObject) {

            var SubjectText = "Inquiry:"
            SubjectText += Subject.text

            var MessageBody = Body

            var toRecipients = ["phillip.lebsack1@gmail.com"]

            var mc: MFMailComposeViewController = MFMailComposeViewController()
            mc.mailComposeDelegate = self
            mc.setSubject(SubjectText)
            mc.setMessageBody(MessageBody.text, isHTML: false)
            mc.setToRecipients(toRecipients)

            self.presentViewController(mc, animated: true, completion: nil)


        }


        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(true, completion: nil)



        }
        func loadAddressURL(){

            let requestURL = NSURL(string:URLPath)
            let request = NSURLRequest(URL: requestURL!)
            Webview.loadRequest(request)

        }
    }
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
Phillip
  • 193
  • 10
  • There are *numerous* questions/answers on this. The problem is that your `!` variables are not bound in Interface Builder and thus when they are first accessed the `!` causes the (expected) crash. (see http://stackoverflow.com/questions/27981488/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-value-lldb) – GoZoner Mar 06 '15 at 04:56
  • How do you bind them in the interface builder? – Phillip Mar 06 '15 at 05:24
  • @PhillipLebsack Make sure that your `!` variables are `!= nil` before doing anything with them – Jojodmo Mar 06 '15 at 05:29
  • Thank you... can you make a tutorial on how to do that? – Phillip Mar 06 '15 at 05:32
  • In Xcode, control-drag a UI element onto its `@IBOutlet`. Set up Xcode with 'Show the Assistant Editor' so you have the IB pane next to the source code pane. (There are other ways; an Xcode UI tutorial will have it). When the `@IBOutlet` is bound where will be a filled circle next to the `@IBOutlet` in the source code. – GoZoner Mar 06 '15 at 05:36
  • I connected them properly and the filled circles are displayed next to them. It still crashes though. – Phillip Mar 06 '15 at 05:49

1 Answers1

0

Make sure no warning message and bind all @IBOutlet in your ViewController.

enter image description here

Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • So how do I actually fix it? I've tried everything I can think of. The crash is saying it's the Webview.loadRequest(request) that's causing it. But it's bound to the UIWebView. So why does it crash when I try to go to the mail interface? – Phillip Mar 07 '15 at 03:25