0

I am trying to create a WebView in a storyboard Swift 2.3 iOS application. However, whenever I navigate to the View with the WebView, the app crashes. I am posting the storyboard and code for the WebView. Does this give any indications as to what I am doing wrong?

Storyboard scene:

<!--WebView View-->
    <scene sceneID="oin-Tm-kjC">
        <objects>
            <viewController title="WebView View" id="iti-7S-ISp" customClass="webViewViewController" sceneMemberID="viewController">
                <layoutGuides>
                    <viewControllerLayoutGuide type="top" id="3bg-3D-Wcg"/>
                    <viewControllerLayoutGuide type="bottom" id="Mnm-nM-yzb"/>
                </layoutGuides>
                <view key="view" contentMode="scaleToFill" id="gnf-vw-fwi">
                    <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    <subviews>
                        <webView contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="rVb-rt-chb">
                            <rect key="frame" x="0.0" y="64" width="375" height="603"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            <connections>
                                <outlet property="delegate" destination="iti-7S-ISp" id="E7m-JN-oGx"/>
                            </connections>
                        </webView>
                    </subviews>
                    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                </view>
                <connections>
                    <outlet property="webView" destination="rVb-rt-chb" id="ldC-1p-1XM"/>
                </connections>
            </viewController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="Dj4-qu-aUe" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="248.80000000000001" y="-631.0344827586207"/>
    </scene>

webViewViewController.swift:

import UIKit

class webViewViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!

    let url = "https://www.google.com"

    override func viewDidLoad() {
        super.viewDidLoad()

        let nsUrl = NSURL(string: url)
        let nsUrlRequest = NSURLRequest(URL: nsUrl!)
        webView.loadRequest(nsUrlRequest)
    }
}

I see this line in the console: "Unknown class webViewViewController in Interface Builder file"

Followed by: "*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key webViewViewController.'".

Additionally, the app runs fine if I remove the coupling to the webViewViewController.swift. In which case it just shows an empty UIWebView.

Nik S.
  • 51
  • 8
  • 1
    Share app crash reason (it may print a statement in console area) when your app crashes –  Mar 01 '17 at 08:48
  • @RyanHeitner he's using `NSURL` & `NSURLRequest` because he's still using the soon-to-be-totally-gone-as-of-Xcode-8.3 Swift 2.3 – Michael Dautermann Mar 01 '17 at 08:59
  • @RyanHeitner From the storyboard XML it seems the outlet is hooked – humblePilgrim Mar 01 '17 at 09:00
  • Thanks @Monster I was a little quick to post.. I have added to description. – Nik S. Mar 01 '17 at 09:15
  • I have this wierd feeling that `webViewViewController` might be conflicting with something *within* the iOS SDK, but I don't know this for sure yet. In the meantime, a style recommendation for Nik: class names [**should start with upper-case-letters**](https://code.tutsplus.com/articles/9-confusing-naming-conventions-for-beginners--net-15584). – Michael Dautermann Mar 01 '17 at 09:18
  • pblm is navigating check the class name `navigate to the View with `webViewViewController` is connected or not – Anbu.Karthik Mar 01 '17 at 09:21
  • see this once http://stackoverflow.com/questions/13793162/setvalueforundefinedkey-this-class-is-not-key-value-coding-compliant-for-the-k – Anbu.Karthik Mar 01 '17 at 09:24
  • 1
    Share snapshot of storyboard (webViewController) with connection inspector & Class Name and storyboard id. Either you may have set wrong connection in connection inspector or You class could not be connected to your project –  Mar 01 '17 at 09:25
  • can you attach your project , it is easy ti resolve – Anbu.Karthik Mar 01 '17 at 09:27
  • Thank you for the suggestions @MichaelDautermann I just tried removing, adding new UIWebView, and coupling to same class with a unique name. Same result. – Nik S. Mar 01 '17 at 09:29
  • this question hasn't had attention in a couple hours. Another suggestion from me: change the name from `webViewViewController` (or `WebViewViewController`) to something else, and make sure you make the matching change to the name of your custom class in your Storyboard. – Michael Dautermann Mar 01 '17 at 11:19
  • Thanks @MichaelDautermann I tried 'starting over' using a different class name with no risk of conflict. It did not have any effect. – Nik S. Mar 03 '17 at 06:12
  • I ended up leaving the UIWebView out of the storyboard entirely and adding it programmatically instead. This works perfectly, but I will leave the question open, and try any other suggestions that may come in. – Nik S. Mar 07 '17 at 05:43

1 Answers1

0
import UIKit

class webViewViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!

    let url = "https://www.google.com"

    override func viewDidLoad() {
        super.viewDidLoad()

        let nsUrl = NSURL(string: url)
        let nsUrlRequest = NSURLRequest(url: nsUrl! as URL)
        webView.loadRequest(nsUrlRequest as URLRequest)
    }
}
iAnkit
  • 1,940
  • 19
  • 25