我有一个 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;
}