0

基本上我需要知道 webview 如何知道正在点击哪种文件扩展名(png、zip 等),然后推送另一个视图控制器。

我之前尝试过没有文件扩展名代码,它会推送另一个视图就好了。

- (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;
    }

非工作代码:

- (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;
}

大部分时间都在工作。

- (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;
}

我也尝试将其用作入门基础但没有成功,因为我不使用界面构建器,所以我不知道是否可以使用 IBActions(如果可以的话,我不知道如何正确实现它们)。如何从 UIWebView 下载文件并再次打开

任何帮助,将不胜感激。

4

1 回答 1

0

也许您应该将 if ([fileExtension isEqualToString:@"png"] || ...) 放入 if(navigationType == UIWebViewNavigationTypeLinkClicked){} 并再试一次?

于 2013-06-13T01:47:05.723 回答