objective-c - 设置 windowScriptObject 时永远不会调用 isSelectorExcludedFromWebScript

标签 objective-c macos cocoa

我尝试遵循Apple文章Calling Objective-C Methods From JavaScript让 WebView 中的 JS 访问一些 Objective-C 函数。

最终得到一个如下所示的中间层对象:

// FILE: RTFInterop.h

#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>

@protocol RTFInteropDelegate <NSObject>
@end

@interface RTFInterop : NSObject

- (id)initWithWebView:(WebView*)webView andDelegate:(id<RTFInteropDelegate>)delegate;
- (void)onHeightUpdated:(int)height;
- (void)onAnswerBlocksUpdated:(NSString*)answerBlockJSON;

+ (NSString *)webScriptNameForSelector:(SEL)sel;
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector;
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name;

@end

// FILE: RTFInterop.m

#import "RTFInterop.h"

@implementation RTFInterop {
    WebView *webView;
    id<RTFInteropDelegate> delegate;
}

- (id)initWithWebView:(WebView*)aWebView andDelegate:(id<RTFInteropDelegate>)aDelegate {
    self = [self init];

    if (self) {
        webView = aWebView;
        delegate = aDelegate;
        [webView.windowScriptObject setValue:self forKey:@"RTFInterop"];
    }

    return self;
}

+ (NSString *)webScriptNameForSelector:(SEL)sel {
    NSString *name;

    if (sel == @selector(onHeightUpdated:)) {
        name = @"onHeightUpdated";
    } else if (sel == @selector(onHeightUpdated:)) {
        name = @"onAnswerBlocksUpdated";
    }

    return name;
}

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel {
    if (sel == @selector(onHeightUpdated:)) {
        return NO;
    } else if (sel == @selector(onHeightUpdated:)) {
        return NO;
    }

    return YES;
}

+ (BOOL)isKeyExcludedFromWebScript:(const char *)name {
    return YES;
}

// Called from JS
- (void)onHeightUpdated:(int)height {

}

// Called from JS
- (void)onAnswerBlocksUpdated:(NSString*)answerBlockJSON {

}

@end

示例用法如下:

self.webView = [[WebView alloc] init];
self.interop = [[DXRichTextInterop alloc] initWithWebView:self.webView andDelegate:nil];

NSURL *url = [NSURL URLWithString:@"file:///Users/example/dev/testembedd.html"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

[self.webView.mainFrame loadRequest:urlRequest];
[self.webView setFrame:CGRectMake(0, 0, 100, 100)];

webView 显示正确,但问题是它似乎没有注入(inject) JavaScript,isSelectorExcludedFromWebScript: 中的断点永远不会被命中。

问题:嵌入为 JS 时是否有一些我错过的文章中未包含的要求?比如在WebView生命周期中你可以注入(inject)JS。或者只是其他一些错误?

最佳答案

发现,当 JS 尝试调用注入(inject)的函数或访问注入(inject)的键时,会调用 webScriptNameForSelectorisSelectorExcludedFromWebScriptisKeyExcludedFromWebScript

关于objective-c - 设置 windowScriptObject 时永远不会调用 isSelectorExcludedFromWebScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29209186/

相关文章:

ios - UILocalNotification iOS8 handleActionWithIdentifier 从未调用过

macos - 为什么 Sass 会倒在 HighSierra 上?

javascript - 使用 Javascript 以编程方式触发 mac 屏幕截图

objective-c - Xcode Playground 框架导入

Objective-C 到 Swift 按位异或等于运算符 ^=

ios - 在 MKMapSnapshotter 上绘制一个 MKCircleRenderer

iphone - 方法的条件实现

objective-c - 如何在 mac osx 中为文件类型设置自定义图标

objective-c - 在应用程序或运行循环之外使用 Grand Central Dispatch

swift - 如何通过终端或 Swift 禁用三指点击手势?