Search code examples
iosswiftwebviewnsurl

Swift WebViews not working as expected


I have done lots of research, yet I am stilling having the same problem. I am trying to load a website onto a web view in swift, although every time I run the project, it is giving me the following error message:

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0)

The following two lines of code are what I am attempting to use to load the url onto the web view. I want to let you know that these 2 lines of code came from the following video: https://www.youtube.com/watch?v=rcVv1N1hReQ. Here are the lines of code:

var URL = NSURL(string: "https://www.google.com")
petInfo.loadRequest(NSURLRequest(URL: URL!))

In case you are wondering what the outlet for petInfo looks like, it is displayed in the following block of code:

@IBOutlet weak var petInfo: UIWebView!

The outlet is getting initialized immediately before the viewDidLoad() function. The url is being loaded inside the viewDidLoad() function. Thank you in advance for any help.


Solution

  • I finally figured the answer out. The problem was that my WebView was equal to nil, although my function was requesting a value, so the app crashed. Instead of using the ! to unwrap the outlet, I changed it to the following:

    if let pet = petInfo{
         //do normal function stuff like loading the web views.
    }
    

    I want to thank you for the answer, although it was not what I was looking for. If anybody doesn't understand the code below, just type up a comment (make sure to add +Andrew_Wilson214) and then I will explain it to you further.