WKFrameInfo request

Hi.


It is not possible to get WKFrameInfo.request if request is null with swift 3.0. With swift 2.3 it was working.


In the delegate method decidePolicy for I try to access navigationAction.targetFrame.request. This causes the app to crash

if thr request is null.


libswiftFoundation.dylib`static Foundation.URLRequest._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSURLRequest>) -> Foundation.URLRequest:

function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Dead> of Foundation.URLRequest.init (_bridged : __ObjC.NSURLRequest) -> Foundation.URLRequest

Replies

In fact many optionals have changed in Swift 3, we have to adapt.

Well, so you should test for nil before ?

This is not possible, the app crashes directly when accessing navigationAction.targetFrame.request.

Can you show this part of code, in its context (the complete func where you call it).

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (@escaping (WKNavigationActionPolicy) -> Void)) {

print("decidePolicyForNavigationAction url so far : \(String(describing: webView.url))")

print("decidePolicyForNavigationAction : " + navigationAction.description)

print("decidePolicyForNavigationAction mainDocumentURL : \(String(describing: navigationAction.request.mainDocumentURL))")

let navType = navigationType(navigationAction.navigationType);

print("decidePolicyForNavigationAction : " + (navType as String))

if (navigationReference != nil) && (navigationAction.navigationType != WKNavigationType.linkActivated)

&& (navigationAction.request.mainDocumentURL == currentURL || navigationAction.targetFrame?.request.url == nil

|| (navigationAction.targetFrame?.request.url != nil && navigationAction.targetFrame?.request.url?.absoluteString == "")) {

decisionHandler(.allow)

print("decidePolicyForNavigationAction middle of a request (pass thru)")

return

}

if nil == navigationAction.targetFrame {

webView.load(navigationAction.request);

}

decisionHandler(.allow)

}

As request may be nil, you should test everywhere :


navigationAction.targetFrame?.request?.url


instead of


navigationAction.targetFrame?.request.url

WKFrameInfo request is a non optional value- navigationAction.targetFrame?.request?.url gives an error.

In the SDK that ships with Xcode 8.3, the "request" property is non-optional. That indicates that this API has been audited, or verified to not return nil. That's what allows the underlying Obj-C method to be presented in Swift as non-optional.


If "request" is actually nil, then that's an error in the SDK — the API has been wrongly audited, and the crash is not your fault. You should submit a bug report.


There may be something else going on here (perhaps the nil really isn't supposed to be possible, but there's a failure elsewhere that's letting it slip through). One workaround would be to provide your own Obj-C method or function that wraps the Obj-C "request" property but returns an optional or IUO to your Swift code so that you can test it.

I submitted a bug report plus TSI yesterday. Still waiting for a reply.

You can handle it with objective-c. It worked fine.