Question

I have a simple index.html file like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script type="text/javascript">

      $(function() {

        setInterval(function(){

          if (typeof Cocoa !== 'undefined') {

            Cocoa.log('JQuery loaded...');

          }

        }, 3000);

      });

    </script>

  </head>
  <body>
    <h1>Testing script in objective c enviroment.</h1>
    <div id="container" class="col">
      <p>Paragraph 1</p>
      <p>content goes here</p>
    </div>
  </body>
</html>

loaded from a WebView subclass in my AppDelegate:

appDelegate.h

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property NSWindow *webWindow;
@property myWebView *myWV;

@end

// ...

appDelegate.m

#import <WebKit/WebKit.h>
#import "myWebView.h"

@implementation AppDelegate

// ...

- (void)applicationDidFinishLaunching:(NSNotification *)notification {


    CGRect webRect = CGRectMake(0, 0, 1000, 1000);

    self.webWindow = [[NSWindow alloc] initWithContentRect:webRect styleMask:NSTexturedBackgroundWindowMask backing:NSBackingStoreBuffered defer:NO];
    self.webWindow.contentView = [[NSView alloc] initWithFrame:webRect];

    self.myWV = [[myWebView alloc] initWithFrame:webRect frameName:@"myWV" groupName:@"webViews"];
    self.myWV.UIDelegate = self;
    self.myWV.resourceLoadDelegate = self;
    self.myWV.frameLoadDelegate = self;

    [self.webWindow.contentView addSubview:self.myWV];
    [self.webWindow orderFront:NSApp];

    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"index"
                                                         ofType:@"html"
                                                    inDirectory:@""];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
    [[self.myWV mainFrame] loadRequest:request];

}

// associate js "Cocoa.log" function with -logJavaScriptString: method
+ (NSString *)webScriptNameForSelector:(SEL)sel
{
    if(sel == @selector(logJavaScriptString:))
        return @"log";
    return nil;
}

//this allows JavaScript to call the -logJavaScriptString: method
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel
{
    if(sel == @selector(logJavaScriptString:))
        return NO;
    return YES;
}

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    NSLog(@"webView loaded");
    [[self.myWV windowScriptObject] setValue:self forKey:@"Cocoa"];   
}

- (void)logJavaScriptString:(NSString *)logText
{
    NSLog(@"JavaScript: %@",logText);
}

Why does nothing inside $(function() {}); get called? If I place the interval log function outside the JQuery onReady function, it does start to call the correct method after a few seconds.

Is there some cross-site policy issue going on that's preventing scripts from being loaded? If so, how can I disable it? Why isn't jQuery loading?

Was it helpful?

Solution

I changed

https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js

to

http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js

and now it works. If anyone knows why WebView doesn't like https, let me know in the comments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top