Basically I need to know how a webview knows what kind of file extension is being tapped, (png, zip, etc) and then push another view controller.
I have tried this before without the file extension code and it will push another view just fine.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *theRessourcesURL = [request URL];
DetailViewController *vc = [[DetailViewController alloc] init];
[vc downloadURL:theRessourcesURL userInfo:nil];
[self.navigationController pushViewController:vc animated:YES];
dlvc.delegate = self;
}
return YES;
}
Non-working code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *theRessourcesURL = [request URL];
NSString *fileExtension = [theRessourcesURL pathExtension];
if ([fileExtension isEqualToString:@"png"]) {
MYViewController *vc = [[MYViewController alloc] init];
[dlvc downloadURL:theRessourcesURL userInfo:nil];
[self.navigationController pushViewController:vc animated:YES];
vc.delegate = self;
}
else{}
}
return YES;
}
Working for the most part.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *theRessourcesURL = [request URL];
NSString *fileExtension = [theRessourcesURL pathExtension];
NSLog(@"fileExtension is: %@", fileExtension);
if ([fileExtension isEqualToString:@"php"] || [fileExtension isEqualToString:@".png"] || [fileExtension isEqualToString:@".zip"] || [fileExtension isEqualToString:@".deb"] || [fileExtension isEqualToString:@".jpg"] || [fileExtension isEqualToString:@".mp3"]) {
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
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
[dlvc downloadURL:theRessourcesURL userInfo:nil];
[self.navigationController pushViewController:dlvc animated:YES];
dlvc.delegate = self;
return NO;
}
else{}
}
return YES;
}
I also tried to use this as a starter base without success, as I don't use interface builder, so I don't know if IBActions can be used (if they can I dont know how to implement them properly). How to download files from UIWebView and open again
Any help would be appreciated.