本文介绍了在Objective-C代码上找不到windowScriptObject方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIWebView 与我的应用程序,并希望从JavaScript调用 Objective-C 方法。
我发现的所有文档和教程,我看到的只是使用 windowScriptObject 方法,但在我的代码 Xcode 说,webView没有实例方法 -windowScriptObject

I have an UIWebView with my application and want to call Objective-C method from JavaScript.All documentation and tutorial that I found, I see is just uses the windowScriptObject method, but in my code Xcode says that webView dont have instance method -windowScriptObject.

出现了什么问题?

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"];
    NSURL *url = [NSURL fileURLWithPath:indexPath];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

    [self.view addSubview:webView];

    id win = [webView windowScriptObject]; // In this line, Xcode alert 'Instance method "-windowScriptObject" not found'
    [win setValue:@"yes" forKey:@"isExtended"];
}


推荐答案

windowScriptObject 似乎在iOS上不可用。

windowScriptObject seems not to be available on iOS. Reference

此外,请检查类似的问题:

Also, check similar question: how to use windowScriptObject on the iPhone?

这篇关于在Objective-C代码上找不到windowScriptObject方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:56