4 Replies
      Latest reply on Oct 14, 2016 7:58 AM by balistrerin
      balistrerin Level 1 Level 1 (0 points)

        Suppose I have a label that is a NSTextField. I know I can access the value of that label by:

        @IBOutlet weak var label: NSTextField!
        let labelValue = label.stringValue


        And then I can use that variable accordingly.

        The same is easily said for an NSImage:

        @IBOutlet weak var productImageView: NSImageView!
        let img = productImageView.image


        My question is how do I grab the value of a WebView. I am unsure which property allows me to use a WebView.


        @IBOutlet weak var videoTemp: WebView!
        let videoPassed = videoTemp


        How do I access this videoTemp's value much like .stringvalue and .image

        I have URL's to Youtube videos stored in a database that I have already pulled in via JSON and I am trying to pass them from one view to another. I have a method to setup my videos but when I try to access the values I am unsure which property of WebView is necessary to access them much like .stringValue can access a string and .image can access an image

        • Re: How to grab the value of a WebView in Swift
          eskimo Apple Staff Apple Staff (13,945 points)

          Why are you using WebView rather than the more modern WKWebView?

          Regardless, web views don’t expose their content directly like image views.  Rather you’d typically ask the web view for its URL and pass that around.

          Having said that, I suspect you’ve got the wrong end of the stick here.  In general you only need to get the content out of a view if the user is editing.  So, for example, in the case of an image view you’d very rarely read the image property.  Rather, your view controller would know what image it put into the view because it put it there.

          So, unless you’re trying to get the URL that the user has navigated to, you’d normally just keep a copy of the URL that you used to set up the web view in the first place, which avoids this issue entirely.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: How to grab the value of a WebView in Swift
              balistrerin Level 1 Level 1 (0 points)

              Thank you.  I did not know there was a more modern WKWebView.  When I switched over to the more modern WKWebView I started to get a fatal error which occurs on line 64.  My print(url) statement works and prints out all of my urls to my videos on Youtube.  As you can see I am not using a static hard coded URL.  My URL's are dynamic based on clicking on a specific collection view item.    


              "fatal error: unexpectedly found nil while unwrapping an Optional value"




              import Cocoa
              import WebKit
              class test1: NSCollectionViewItem {
                  @IBOutlet weak var label: NSTextField!
                  @IBOutlet weak var label2: NSTextField!
                  @IBOutlet weak var productImageView: NSImageView!
                  @IBOutlet weak var videoTemp: WKWebView!
                  var buildProduct: ProductModel? {
                          label.stringValue = (buildProduct?.product_name)!
                          label2.stringValue = (buildProduct?.product_price)!
                  override func viewDidLoad() {
                  func setupAppIconImage() {
                      if let appIconImageURL = buildProduct?.product_image {
                          let url = NSURL(string: appIconImageURL)
                          URLSession.shared.dataTask(with: url! as URL,completionHandler:{(data, response, error) in
                              if error != nil {
                              self.productImageView.image = NSImage(data: data!)
                  func setupAppVideo(){
                      if let appVideoURL = buildProduct?.product_video{
                          let url = NSURL(string: appVideoURL)
                          let request = NSURLRequest(url: url as! URL)
                          self.videoTemp.load(request as URLRequest)