【发布时间】: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()byUIWebViewNavigationTypeLinkClicked
标签: javascript ios objective-c safari uiwebview