3

I'm using a WKWebview to run javascript scripts.

I am not using JSContext et.al since I need the javascript context to be able to perform XHTTP requests which aren't possible with JSContext alone.

This view is not added to the view hierarchy and I have no interest to do so.

The WKWebview is only used for its ability to run JS code in its engine.

The javascript code is working exactly as expected on the simulator.

The exact same code will also work in the context of certain other applications which I've tested with.

But for some reason in certain applications the WKWebview will not perform the javascript unless the WKWebview is added to the view hierarchy. The following code will work as expected. If removing the #warning code , stops working as expected

-(void)connect {
  //TODO: Handle multiple connect calls
  WKUserContentController *userContentController = [WKUserContentController new];

  [self addScriptMessageHandlersForSocketEvents:userContentController];

  NSString *socketFileContent = [self.class socketIOScript];

  WKUserScript *socketIOScript = [[WKUserScript alloc] initWithSource:socketFileContent
                                                        injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                                     forMainFrameOnly:NO];

  [userContentController addUserScript:socketIOScript];


  NSString *bridgeJs = [self.class bridgeScript];

  WKUserScript *bridgeScript = [[WKUserScript alloc] initWithSource:bridgeJs
                                                      injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                                   forMainFrameOnly:NO];


  [userContentController addUserScript:bridgeScript];

  NSMutableDictionary *d = [NSMutableDictionary new];
  for (NSURLQueryItem *item in _parameters) {
    d[item.name] = item.value;
  }

  NSString *params = json(d);
  NSString *socketURLScript = [NSString stringWithFormat:@"createSocket(%@,%@);log('created script')",stringify(_url),params];

  WKUserScript *createSocket = [[WKUserScript alloc] initWithSource:socketURLScript
                                                      injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
                                                   forMainFrameOnly:NO];


  [userContentController addUserScript:createSocket];

  WKWebViewConfiguration * wkconfiguration = [WKWebViewConfiguration new];
  wkconfiguration.userContentController = userContentController;

  _wv = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:wkconfiguration];

#warning REMOVE THIS AND THE WKWebview stops working on some devices (NOT ALL DEVICES) and works in some applications as expected
    [[UIApplication sharedApplication].keyWindow addSubview:_wv];
    // END REMOVE THIS
  _wv.navigationDelegate = self;


  dispatch_group_enter(_loadedSemaphore);
  [_wv loadHTMLString:@"<h1></h1>" baseURL:nil];
}
Avner Barr
  • 13,049
  • 14
  • 82
  • 152

1 Answers1

0

did you implement all the delegate methods for WKWebView ?

Autokrat
  • 1
  • 1
  • 2
  • The delegate methods are optional. I implemented only- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation – Avner Barr Nov 01 '16 at 15:11