0

我有一个简单的 index.html 文件,如下所示:

<!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>

从我的 AppDelegate 中的 WebView 子类加载:

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);
}

为什么里面什么都没有$(function() {});被调用?如果我将间隔日志函数放在 JQuery onReady 函数之外,它确实会在几秒钟后开始调用正确的方法。

是否存在阻止加载脚本的跨站点策略问题?如果是这样,我该如何禁用它?为什么不加载 jQuery?

4

1 回答 1

0

我变了

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

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

现在它可以工作了。如果有人知道为什么WebView不喜欢 https,请在评论中告诉我。

于 2013-09-12T00:07:19.923 回答