Cindy Kim
Cindy Kim

Reputation: 31

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) with WebView SWIFT

I'm a beginner at Swift as well as on Stack Overflow. First things first, I have researched my question online and went through all the related questions.The answer I was hoping for was very vague (swift - EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) with dataTaskWithUrl) and so I was hoping for a clearer answer. I am trying to use Webview to display a url, and I keep on getting the error 'Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)' and 'fatal error: unexpectedly found nil while unwrapping an Optional value'. Could someone please help me? Thank you!

Here is the code:

 import UIKit


 class ViewController: UIViewController {


    @IBOutlet var MoongateWebView: UIWebView!
    @IBOutlet var ManagebacWebView: UIWebView!
    @IBOutlet var MoodleWebView: UIWebView!
    @IBOutlet var LibraryWebView: UIWebView!
    @IBOutlet var CCAWebView: UIWebView!
    @IBOutlet var gmailWebView: UIWebView!


    override func viewDidLoad() {
        super.viewDidLoad()


       //loading Moongate on the screen
        let mgurl = NSURL(string: "http://moongate.cis.edu.hk/")
        let mgrequest = NSURLRequest(URL: mgurl!)

        MoongateWebView.loadRequest(mgrequest) //Error Occurs

       //loading Managebac on the screen
        let mburl = NSURL(string:"https://cishk.managebac.com/login" )
        let mbrequest = NSURLRequest(URL: mburl!)

        ManagebacWebView.loadRequest(mbrequest)

        //loading Moodle on the screen
        let mdurl = NSURL(string: "http://moodle.cis.edu.hk/moodle/")
        let mdrequest = NSURLRequest(URL: mdurl!)

        MoodleWebView.loadRequest(mdrequest)

        //loading Library on the screen
        let liburl = NSURL(string: "http://library.cis.edu.hk/")
        let librequest = NSURLRequest(URL: liburl!)

        LibraryWebView.loadRequest(librequest)

        //loading CCAs on the screen
        let ccaurl = NSURL(string: "http://moongate.cis.edu.hk/content/page.aspx?e=F17A69B3-62F1-46FF-A474-8776C2D2492D")
        let ccarequest = NSURLRequest(URL: ccaurl!)

        CCAWebView.loadRequest(ccarequest)

        //loading gmail on the screen
        let gmailurl = NSURL(string: "https://mail.google.com/mail/u/0/")
        let gmailrequest = NSURLRequest(URL: gmailurl!)

        gmailWebView.loadRequest(gmailrequest)

     }
}

Upvotes: 0

Views: 919

Answers (2)

Alonso Urbano
Alonso Urbano

Reputation: 2261

Possible the problem is that you webview may be nil.

To quickly check this, you need to use the assistant editor (where you can see a split view) to show your storyboard on one side and the code on the other.

In this way you can check that the @IBOutlet is correctly bound. By positioning the mouse over the ribbon to the left of each of your web views, it will highlight to which element is bound:

enter image description here

If your variable is not bound, you can see an empty circle instead a full circle.

You first check that, to be sure the elements are correctly bound.

Upvotes: 1

Illya Krit
Illya Krit

Reputation: 945

  • It can be, that one of this UIWebViews is nil. That's why, you have the error 'fatal error: unexpectedly found nil while unwrapping an Optional value'. Proof that you have connected all your IBOutlets with webViews in Storyboard (I think, dan means it). And a possibility, change your code to:

    @IBOutlet var MoongateWebView: UIWebView?
    @IBOutlet var ManagebacWebView: UIWebView?
    @IBOutlet var MoodleWebView: UIWebView?
    @IBOutlet var LibraryWebView: UIWebView?
    @IBOutlet var CCAWebView: UIWebView?
    @IBOutlet var gmailWebView: UIWebView?
    
    override func viewDidLoad(){ 
      super.viewDidLoad()
    
        if let _ = MoongateWebView {
           let mgurl = NSURL(string: "http://moongate.cis.edu.hk/")
           let mgrequest = NSURLRequest(URL: mgurl!)
               MoongateWebView!.loadRequest(mgrequest) //Error Occurs
        }
    }
    
    • and so on for each webview

Upvotes: 0

Related Questions