0

WebView に実装された WebView とダウンロード マネージャーがあります。ファイル拡張子を判別し、ダウンロードまたはキャンセル ボタンを押すためのアクション シートを作成することに成功しました。ただし、ダウンロードをタップすると、ファイル全体ではなくテキストのみがダウンロードされます。そのダウンロード コントロール メソッドを shouldStartLoadWithRequest セクションに配置すると、ファイル全体がダウンロードされますが、アクション シートは表示されません。以下は、私が取り組んでいるコードの 2 つのセクションです。どんな情報でもいただければ幸いです。

//Download manager
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if(navigationType == UIWebViewNavigationTypeLinkClicked) {

        NSURL *theResourcesURL = [request URL];
        NSString *fileExtension = [theResourcesURL pathExtension];

        NSLog(@"fileExtension is: %@", fileExtension);
        if ([fileExtension hasSuffix:@"zip"] || [fileExtension hasSuffix:@"deb"] || [fileExtension hasSuffix:@"rar"] || [fileExtension hasSuffix:@"mp3"] || [fileExtension hasSuffix:@"pdf"] || [fileExtension hasSuffix:@"exe"] || [fileExtension hasSuffix:@"mp4"] || [fileExtension hasSuffix:@"flv"] || [fileExtension hasSuffix:@"torrent"] || [fileExtension hasSuffix:@"png"] || [fileExtension hasSuffix:@"jpg"] || [fileExtension hasSuffix:@"jpeg"]) {

            //Action sheet
            UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:[[self->webView.request URL] absoluteString]
                                                                      delegate:self
                                                             cancelButtonTitle:NSLocalizedString(@"Cancel", @"Action sheet cancel button")
                                                        destructiveButtonTitle:nil
                                                             otherButtonTitles:
                                           NSLocalizedString(@"Download", @"Action sheet button"),
                                           nil]
                                          autorelease];
            [actionSheet showFromToolbar:self.navigationController.toolbar];

            //Error setting
            NSError *error = nil;

            // Get documents folder
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"downloads"];

            //Create folder
            if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
                [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];


        }
            return NO;
            }
    return YES;
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex != actionSheet.cancelButtonIndex) {
        NSInteger adjustedIndex = buttonIndex - actionSheet.firstOtherButtonIndex;
        switch(adjustedIndex) {
            case 0:;
                NSURL *theResourcesURL = self->webView.request.URL;

                //Download controller
                HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
                [dlvc downloadURL:theResourcesURL userInfo:nil];
                dlvc.delegate = self;


                //Configure download view controller
                [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];                break;
        }
    }
}
4

0 に答える 0