0

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.

Community
  • 1
  • 1
ChrisOSX
  • 724
  • 2
  • 11
  • 28

1 Answers1

0

Maybe you should put your if ([fileExtension isEqualToString:@"png"] || ...) inside if(navigationType == UIWebViewNavigationTypeLinkClicked){} and try again?

Hunter
  • 136
  • 1
  • 9
  • Like: if (navigationType == UIWebViewNavigationTypeLinkClicked)([fileExtension isEqualToString:@"png"]){ ? or take out the UIWebViewNavigationTypeLinkClicked and use, (navigationType == [fileExtension isEqualToString:@"png"]) ? – ChrisOSX Jun 13 '13 at 01:53
  • I mean this: if(navigationType == UIWebViewNavigationTypeLinkClicked) { if ([fileExtension isEqualToString:@"png"] || ...) } – Hunter Jun 13 '13 at 02:50
  • No dice, I'm getting a "Expected statement" with that. It was point to a }. When fixing the statement, it wont pass the view controller. – ChrisOSX Jun 13 '13 at 03:04
  • I gave it a try, it seems worked. if(navigationType == UIWebViewNavigationTypeLinkClicked) { NSURL *theRessourcesURL = [request URL]; NSString *fileExtension = [theRessourcesURL pathExtension]; if ([fileExtension isEqualToString:@"png"] || ...) { TestViewController *vc = [[TestViewController alloc] init]; [self.navigationController pushViewController:vc animated:YES]; }else{} } – Hunter Jun 13 '13 at 05:38
  • While the above code does make sense, it's still just opening the png link, not pushing the vc. Post updated above. – ChrisOSX Jun 13 '13 at 11:15
  • May I see the code where navigationController is initialized? Maybe that's the problem. – Hunter Jun 13 '13 at 12:34
  • Yep will post later as I'm away from my computer for the day. It will push the vc without the file extension code, like tapping on a link, but having that file extension code doesn't seem to pass the type of file. But I will update my post later. Thank you for your help so far. – ChrisOSX Jun 13 '13 at 12:44
  • The navigation controller appears to work, like I said when the file extension is not implemented, say when I tap on any url, it will open. But having that code in there it won't show. – ChrisOSX Jun 13 '13 at 22:26
  • Ok now we're getting somewhere. Code updated above. I'm guessing it has something to do with my forum skin and how its set up. That NSLog code showed one file extension php, so I set the extension to php and it worked. Issue is now, that it won't download a simple png . Never did before, but I have a test png, it just opens another web view. I don't know if that is because of my forum again, or another coding issue. – ChrisOSX Jun 14 '13 at 00:55
  • So what do you do in your method downloadURL: userInfo:? By the way, the link you post up there seemed work. – Hunter Jun 14 '13 at 02:31
  • Yes it worked, but it only works once. I have to close the app completely and open again to download. I don't know if theres something else I need to set in the code so it. UPDATE, ok so I narrowed it down I think to having my nav bar hidden. If I unhide it, I can nav back to my main view and download again. I guess my next question is, How can I hide the nav bar on my web view not on the download view, then hitting back button, hide the nav bar again? – ChrisOSX Jun 14 '13 at 03:40
  • Try this,add code "self.navigationController.navigationBar.hidden = YES" in the method "- (void)viewWillAppear:(BOOL)animated" of web view,and add code "self.navigationController.navigationBar.hidden = NO" in method "- (void)viewDidLoad" of download view.May it works. – Hunter Jun 14 '13 at 03:54
  • I had this thought initially. The thing was i have 2 main view controllers. 1 for app load which loads static images after launch, and another for the table view tap, which won't show the images, just loads the view. I forgot about this while just trying to get it to work. Implementing the download vc into that vc worked. So now I can nav to another vc and do another download. issue now I want to try and fix is to keep that download vc active to complete downloads. It works if I hit back and go to the page i started the download from, but any other navigating stops it. – ChrisOSX Jun 14 '13 at 13:00
  • Also I'm going to accept your answer as that set me in the right direction. – ChrisOSX Jun 14 '13 at 13:16
  • My pleasure,very glad to help. But what you say about " It works if I hit back and go to the page i started the download from" bothers me a little.Maybe it worked because of a memory leak, as you can see, "dlvc" never release after allocated.Just a guess. – Hunter Jun 14 '13 at 13:59
  • What I mean by that is, nav to test page->tap file to download (starts download). Tap back button in nav bar->goes to test page. The file still downloads as yes it never gets released, which is what I want until the download finishes. But say I follow above steps but instead of going back to test page, I go to another vc like test 2 page, the download will stop. – ChrisOSX Jun 14 '13 at 14:14
  • What about multithread,like main thread displaying UI,yet another thread is downloading something.Just a thought. – Hunter Jun 14 '13 at 15:02
  • yah that's a thought, I'll look into something like that. As it stands, the downloader works, that was my main goal. Again thank you for your help and input. – ChrisOSX Jun 14 '13 at 15:12