window. -location不设置 / webview应该不调用loadRequest



我一直在从stackoverflow上找到的几个主题工作,但我似乎无法正确处理。

我的应用程序在本地存储HTML页面,我正在尝试让一些JavaScript使用我的OBJ-C代码。

这是我的JavaScript文件:

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
    window.location.href = "ios:webToNativeCall";
    alert(window.location.href);
}
</script>

这是我的OBJ-C方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"test");
    if ([[[request URL] absoluteString] hasPrefix:@"ios:"]){
        NSLog(@"test");
        [self webToNativeCall];
        return NO;
    }

我会发布两个单独的主题,但我觉得这些问题是链接的。在JS文件中,警报在我的系统中打印了.html文件的正确路径。NSLog(@"test")均未执行。

我已经设置了WebView委托:

@interface WebBrochureViewController : UIViewController <UIWebViewDelegate>

感谢任何帮助或提示。

您的故事板设置可能有问题。您应该首先尝试一个简单的案例作为概念证明,然后学习如何使其适应更大的局面。

这是一个很好的简单视图控制器。

@interface MyViewController : UIViewController <UIWebViewDelegate>
@property (nonatomic, weak) IBOutlet UIWebView *webView;
@end
@implementation MyViewController
- (void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    [view addSubview:({
        UIWebView *webView = [[UIWebView alloc] initWithFrame:view.bounds];
        webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        webView.delegate = self;
        self.webView = webView;
    })];
    self.view = view;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
    NSAssert(url!=nil, @"Could not find URL for index.html");
    NSData *data = [NSData dataWithContentsOfURL:url];
    [self.webView loadData:data
                  MIMEType:@"text/html"
          textEncodingName:@"utf-8"
                   baseURL:nil];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"[%@] Delegate method started.", NSStringFromSelector(_cmd));
    if ([request.URL.scheme isEqualToString:@"ios"]) {
        NSLog(@"[%@] Communication Received.", NSStringFromSelector(_cmd));
        return NO;
    }
    return YES;
}
@end

最新更新