【问题标题】:how to open window.open method in ios objective c safari using webview如何使用 webview 在 ios objective c safari 中打开 window.open 方法
【发布时间】:2017-10-29 09:36:28
【问题描述】:

我是 iOS 开发新手

我已经开发了一个应用程序来在UIWebView 中打开 url,但是该网站在safari 网络浏览器中运行良好,但是当我录制一个编码为window.open() 的链接时,它将加载相同的webview。这是我的代码

ViewController.h 文件

//
//  ViewController.m
//  
//
//  Created by Code Kadiya on 5/18/17.
//  Copyright © 2017 Code Kadiya. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UIWebViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.webView setDelegate:self];
    // Do any additional setup after loading the view, typically from a nib.

    NSURL *websiteUrl = [NSURL URLWithString:@"http://some.url"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:websiteUrl cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:15.0];
    [_webView loadRequest:request];
}

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSURL *url = [request URL];
        [[UIApplication sharedApplication] openURL: url options:@{} completionHandler:nil];
        return NO;
    }
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end

我在加载 Web 应用程序源代码时发现了这段代码

<a onclick="open_pdf('office')" target="_blank">


function open_pdf(type) {
    window.open("http://some.url?type=" + type );            
 }

无论如何,当我单击该链接时,我需要打开 Safari 浏览器,但我无法更改 Web 应用程序源代码

我觉得

[[UIApplication sharedApplication] openURL: url options:@{} completionHandler:nil];

不为那种类型的链接执行

【问题讨论】:

  • 希望对您有所帮助:stackoverflow.com/a/2899793/775896
  • @Code Kadiya 在if (navigationType == UIWebViewNavigationTypeLinkClicked) {}condition 中添加断点,看看它是否正在执行
  • @Maddy 不,伙计,它没有执行,因为有 UIWebViewNavigationTypeOther 类型,但我无法处理 window.open() by UIWebViewNavigationTypeLinkClicked

标签: javascript ios objective-c safari uiwebview


【解决方案1】:

我找到了解决方案 我们可以覆盖 javascript 将 javascript 作为字符串或文件注入这里是我添加到我的代码中的示例代码以及它对我正常工作

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType {
    if(navigationType == UIWebViewNavigationTypeLinkClicked){
        [[UIApplication sharedApplication] openURL: [request URL] options:@{} completionHandler:nil];
        return NO;
    } else {
        [self.webView stringByEvaluatingJavaScriptFromString:@"window.close = function () { window.history.back(); }"];
    }
    return YES;
}

不是window.open() 方法,我们可以覆盖任何方法,例如这个例子

谢谢!

【讨论】:

    猜你喜欢
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多