9 Replies
      Latest reply on Sep 8, 2017 3:12 AM by oohed
      abracadabra Level 1 Level 1 (15 points)

        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

        • Re: WKFrameInfo request
          Claude31 Level 8 Level 8 (6,515 points)

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

          Well, so you should test for nil before ?

            • Re: WKFrameInfo request
              abracadabra Level 1 Level 1 (15 points)

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

                • Re: WKFrameInfo request
                  Claude31 Level 8 Level 8 (6,515 points)

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

                    • Re: WKFrameInfo request
                      abracadabra Level 1 Level 1 (15 points)

                      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)

                          }

                        • Re: WKFrameInfo request
                          Claude31 Level 8 Level 8 (6,515 points)

                          As request may be nil, you should test everywhere :

                           

                          navigationAction.targetFrame?.request?.url

                           

                          instead of

                           

                          navigationAction.targetFrame?.request.url

                            • Re: WKFrameInfo request
                              abracadabra Level 1 Level 1 (15 points)

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

                                • Re: WKFrameInfo request
                                  QuinceyMorris Level 8 Level 8 (6,020 points)

                                  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.