在新的 webview 视图控制器中打开外部 URL
Posted
技术标签:
【中文标题】在新的 webview 视图控制器中打开外部 URL【英文标题】:Opening external URL in new webview view controller 【发布时间】:2013-07-22 14:14:30 【问题描述】:我有一个 webview 应用程序,可以很好地加载我的网站。我想要实现的是让它确定点击的网址是否属于我的网站。如果没有,则打开一个新的视图控制器。
我知道我必须为此做点什么。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
if(navigationType == UIWebViewNavigationTypeLinkClicked)
但我不知道最好的方法。从我搜索的内容来看,它只显示了如何在 webview 中加载 url(没有问题),或者启动 safari(不是我想要的)。
任何信息或指导将不胜感激。
8/26 更新:
//Download manager
- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
if (navigationType == UIWebViewNavigationTypeLinkClicked)
#define kHostName @"http://www.mysite.com"
NSString *host = [[request URL] host];
if ([host isEqualToString:kHostName])
return YES;
else
WebImageViewController * vc = [[WebImageViewController alloc] initWithURL:[request URL]];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
return NO;
//External file links
NSURL *externalURL = [request URL];
NSString *externalFileExtension = [[externalURL absoluteString] pathExtension];
//External file extensions
NSLog(@"fileExtension is: %@", externalFileExtension);
if ([externalFileExtension hasSuffix:@"zip"] || [externalFileExtension hasSuffix:@"deb"] || [externalFileExtension hasSuffix:@"rar"] || [externalFileExtension hasSuffix:@"mp3"] || [externalFileExtension hasSuffix:@"pdf"] || [externalFileExtension hasSuffix:@"exe"] || [externalFileExtension hasSuffix:@"mp4"] || [externalFileExtension hasSuffix:@"flv"] || [externalFileExtension hasSuffix:@"torrent"] || [externalFileExtension hasSuffix:@"aiff"] || [externalFileExtension hasSuffix:@"css"] || [externalFileExtension hasSuffix:@"doc"] || [externalFileExtension hasSuffix:@"gz"] || [externalFileExtension hasSuffix:@"hqx"] || [externalFileExtension hasSuffix:@"htm"] || [externalFileExtension hasSuffix:@"html"] || [externalFileExtension hasSuffix:@"ico"] || [externalFileExtension hasSuffix:@"ipa"] || [externalFileExtension hasSuffix:@"mov"] || [externalFileExtension hasSuffix:@"mpg"] || [externalFileExtension hasSuffix:@"ppt"] || [externalFileExtension hasSuffix:@"ps"] || [externalFileExtension hasSuffix:@"ram"] || [externalFileExtension hasSuffix:@"rtf"] || [externalFileExtension hasSuffix:@"swf"] || [externalFileExtension hasSuffix:@"tar"] || [externalFileExtension hasSuffix:@"tiff"] || [externalFileExtension hasSuffix:@"txt"] || [externalFileExtension hasSuffix:@"viv"] || [externalFileExtension hasSuffix:@"wav"] || [externalFileExtension hasSuffix:@"wmv"] || [externalFileExtension hasSuffix:@"xml"] || [externalFileExtension hasSuffix:@"ipsw"])
NSError *error = nil; //error setting
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"downloads"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
//Download manager
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
[dlvc downloadURL:externalURL userInfo:nil];
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
[self.navigationController pushViewController:dlvc animated:YES];
dlvc.delegate = self;
else
//Internal file links
NSURL *internalURl = [request URL];
NSString *internalFileExtension = [internalURl pathExtension];
//Internal file extensions
NSLog(@"fileExtension is: %@", internalFileExtension);
if ([internalFileExtension hasSuffix:@"zip"] || [internalFileExtension hasSuffix:@"deb"] || [internalFileExtension hasSuffix:@"rar"] || [internalFileExtension hasSuffix:@"mp3"] || [internalFileExtension hasSuffix:@"pdf"] || [internalFileExtension hasSuffix:@"exe"] || [internalFileExtension hasSuffix:@"mp4"] || [internalFileExtension hasSuffix:@"flv"] || [internalFileExtension hasSuffix:@"torrent"] || [internalFileExtension hasSuffix:@"aiff"] || [internalFileExtension hasSuffix:@"css"] || [internalFileExtension hasSuffix:@"doc"] || [internalFileExtension hasSuffix:@"gz"] || [internalFileExtension hasSuffix:@"hqx"] || [internalFileExtension hasSuffix:@"htm"] || [internalFileExtension hasSuffix:@"html"] || [internalFileExtension hasSuffix:@"ico"] || [internalFileExtension hasSuffix:@"ipa"] || [internalFileExtension hasSuffix:@"mov"] || [internalFileExtension hasSuffix:@"mpg"] || [internalFileExtension hasSuffix:@"ppt"] || [internalFileExtension hasSuffix:@"ps"] || [internalFileExtension hasSuffix:@"ram"] || [internalFileExtension hasSuffix:@"rtf"] || [internalFileExtension hasSuffix:@"swf"] || [internalFileExtension hasSuffix:@"tar"] || [internalFileExtension hasSuffix:@"tiff"] || [internalFileExtension hasSuffix:@"txt"] || [internalFileExtension hasSuffix:@"viv"] || [internalFileExtension hasSuffix:@"wav"] || [internalFileExtension hasSuffix:@"wmv"] || [internalFileExtension hasSuffix:@"xml"] || [internalFileExtension hasSuffix:@"ipsw"])
NSError *error = nil; //error setting
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"downloads"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
//Download manager
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
[dlvc downloadURL:internalURl userInfo:nil];
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
[self.navigationController pushViewController:dlvc animated:YES];
dlvc.delegate = self;
else
///////////////
//IMAGE CODE///
///////////////
//Image file links
NSURL *imageURl = [request URL];
NSString *imageFileExtension = [[imageURl absoluteString] pathExtension];
//Image file extensions
NSLog(@"fileExtension is: %@", imageFileExtension);
if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"] || [imageFileExtension hasSuffix:@"gif"] || [imageFileExtension hasSuffix:@"bmp"] || [imageFileExtension hasSuffix:@"raw"] || [imageFileExtension hasSuffix:@"PNG"] || [imageFileExtension hasSuffix:@"JPG"] || [imageFileExtension hasSuffix:@"JPEG"] || [imageFileExtension hasSuffix:@"GIF"] || [imageFileExtension hasSuffix:@"BMP"] || [imageFileExtension hasSuffix:@"RAW"])
[webView stopLoading];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//Image manager
WebImageViewController * vc = [[WebImageViewController alloc] initWithURL:[request URL]];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
else
//Image file links
NSURL *imageURl = [request URL];
NSString *imageFileExtension = [imageURl pathExtension];
//Image file extensions
NSLog(@"fileExtension is: %@", imageFileExtension);
if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"] || [imageFileExtension hasSuffix:@"gif"] || [imageFileExtension hasSuffix:@"bmp"] || [imageFileExtension hasSuffix:@"raw"] || [imageFileExtension hasSuffix:@"PNG"] || [imageFileExtension hasSuffix:@"JPG"] || [imageFileExtension hasSuffix:@"JPEG"] || [imageFileExtension hasSuffix:@"GIF"] || [imageFileExtension hasSuffix:@"BMP"] || [imageFileExtension hasSuffix:@"RAW"])
[webView stopLoading];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//Image manager
WebImageViewController * vc = [[WebImageViewController alloc] initWithURL:[request URL]];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
else
//Image file links
NSURL *imageURl = [request URL];
NSString *imageFileExtension = [imageURl absoluteString];
//Image file extensions
NSLog(@"fileExtension is: %@", imageFileExtension);
if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"] || [imageFileExtension hasSuffix:@"gif"] || [imageFileExtension hasSuffix:@"bmp"] || [imageFileExtension hasSuffix:@"raw"] || [imageFileExtension hasSuffix:@"PNG"] || [imageFileExtension hasSuffix:@"JPG"] || [imageFileExtension hasSuffix:@"JPEG"] || [imageFileExtension hasSuffix:@"GIF"] || [imageFileExtension hasSuffix:@"BMP"] || [imageFileExtension hasSuffix:@"RAW"])
[webView stopLoading];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//Image manager
WebImageViewController * vc = [[WebImageViewController alloc] initWithURL:[request URL]];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
return NO;
return YES;
【问题讨论】:
【参考方案1】:尝试检查你提到的方法
[[request URL] absoluteString]
您可以尝试使用 hasPrefix:(NSString*)prefix 来检查 url 是否以您的网站名称开头。
[[[request URL] absoluteString] hasPrefix:@"http://www.yoursite.com"];
【讨论】:
【参考方案2】:你应该检查host
,如果是你自己的域,则返回YES
,否则返回NO
,然后手动打开新的视图控制器:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
if (navigationType == UIWebViewNavigationTypeLinkClicked)
NSString *host = [[request URL] host];
if ([host isEqualToString:kHostName])
return YES;
else
ViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"webview"];
controller.url = request.URL;
[self.navigationController pushViewController:controller animated:YES];
return NO;
return YES;
显然,如果您想进行模态演示,请将其调整为使用 presentViewController
。同样,如果使用 NIB,请将 instantiateViewControllerWithIdentifier
替换为 [[ViewController alloc] initWithNibName:nil bundle:nil];
(或其他)。我的视图控制器有一个公共属性NSURL *url
,但你可以使用任何你想要的属性。但希望这能说明这个想法。
【讨论】:
会试试这个。唯一的问题是我没有使用 IB,它都是代码。我将发布我所有的 shouldStartLoadWithRequest 以提供任何进一步的帮助。 @ChrisOSX 没关系。只需像往常一样通过代码实例化您的视图控制器,设置您希望该控制器打开的 url 的属性,然后转换到它(通过presentViewController
或pushViewController
,视情况而定)。但想法是一样的:检查主机,如果是你的,就做你现有的代码,如果不是,对上面的代码做一个排列。
无论我如何尝试配置它,它似乎都会在我的自定义视图控制器中打开所有链接。这个“kHostName”也会引发错误。想让我把它改成“kCFHostNames”。
@ChrisOSX 在我的代码 sn-p 中,我假设您将定义一些常量 kHostName
,并将其设置为您的主机名。抱歉,如果不清楚。
明白了。好的,我定义了。然而,属于我的网站的 url 会推送我的自定义 vc,但像 dropbox 这样的网站仍然在 web 视图中打开。用我当前的代码更新了 OP,因为自我最初的问题以来它发生了很大变化。以上是关于在新的 webview 视图控制器中打开外部 URL的主要内容,如果未能解决你的问题,请参考以下文章