-
Re: How to grab the value of a WebView in Swift
eskimo Oct 13, 2016 3:55 AM (in response to balistrerin)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/Hardwarelet myEmail = "eskimo" + "1" + "@apple.com"
-
Re: How to grab the value of a WebView in Swift
balistrerin Oct 13, 2016 8:13 AM (in response to eskimo)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"
test1.swift
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? { didSet{ label.stringValue = (buildProduct?.product_name)! label2.stringValue = (buildProduct?.product_price)! setupAppIconImage() setupAppVideo() } } override func viewDidLoad() { super.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 { print(error) return } DispatchQueue.main.async(){ self.productImageView.image = NSImage(data: data!) } }).resume() } } func setupAppVideo(){ if let appVideoURL = buildProduct?.product_video{ let url = NSURL(string: appVideoURL) print(url) let request = NSURLRequest(url: url as! URL) self.videoTemp.load(request as URLRequest) } } }
-
Re: How to grab the value of a WebView in Swift
eskimo Oct 14, 2016 2:09 AM (in response to balistrerin)I think this unexpected nil relates to
videoTemp
. Add this before line 64 to see if it fails.assert(self.videoTemp != nil, "videoTemp is nil");
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardwarelet myEmail = "eskimo" + "1" + "@apple.com"
-
Re: How to grab the value of a WebView in Swift
balistrerin Oct 14, 2016 7:58 AM (in response to eskimo)Instead of trying to move the web view from one view controller to another I simply passed the URL and handled the URL on the second Controller. But you are correct that my videoTemp was nil. Thank you!
-
-
-