【问题标题】:UIWebView download from <a href=myFileName.myFileType>从 <a href=myFileName.myFileType> 下载 UIWebView
【发布时间】:2014-02-26 21:02:20
【问题描述】:

我希望我的 UIwebview 在单击链接时触发文件的下载...它只是在 webview 中打开它,即它呈现文件的内容,一个 JSON 字符串。

  • 我已经注册了 mimetype。我试过添加 download="target.myfiletype" 到锚标签,但它仍然只是 在 UIWebview 中呈现内容 (json)。

  • 当我通过电子邮件发送它时,它只会在我想要的正确应用程序中打开...

    如何让我的 UIWebview(在同一个应用程序中)触发打开 我的应用程序中的文件,与电子邮件相同吗?

文件部分的邮件源如下:

...
 --Apple-Mail-38441BAA-F4DD-4BF1-B2CC-9AF9C829566A
Content-Type: application/myfiletype;
    name="ExtremeSomething"
Content-Disposition: attachment;
    filename="ExtremeSomething"
Content-Transfer-Encoding: 7bit

{
//FILECONTENT
}
...

我的应用通过书实现文件加载,当我点击电子邮件中的文件时它可以工作:

-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url {

    if (url != nil && [url isFileURL]) {
        [self loadFile:url];
    }
    return YES;
}

我试图用负返回值覆盖 shouldStartLoadWithRequest:

//Load initial page:
-(void) viewDidAppear:(BOOL)animated{
    NSString *urlString = [NSString stringWithFormat:@"%@", @"http://users.student.lth.se/et08dc0/getWork.html"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    loadedNowDownloadMode = NO;
}

//Resort to download move where all <a> clicks result in downloads:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    loadedNowDownloadMode = YES;
}

//Logic for overriding shouldStartLoadWithRequest:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request
 navigationType:(UIWebViewNavigationType)navigationType {
    //Handle the download...
    NSURL* url = [[NSURL alloc] initWithString:[[request URL] absoluteString]];
    [DELEGATE loadFile:url];

    //Prevent page load
    return !loadedNowDownloadMode;
}

在宏 DELEGATE 引用的类中:

-(void) loadFile:(NSURL*)url{
    NSError *error;
    NSString *jsonString = [[NSString alloc]
                                     initWithContentsOfURL:url
                                     encoding:NSUTF8StringEncoding
                                     error:&error];
    if (jsonString == nil) {
        NSLog(@"Error reading file at %@\n%@",
              url, [error localizedFailureReason]);
        return;
    }
    NSLog(@"Should load file!");
    [self.coreDataHelper importNSData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    [((ListViewController*)listsViewController) reloadTableData];

}

【问题讨论】:

    标签: html ios objective-c uiwebview mime-types


    【解决方案1】:

    您可以在webView:shouldStartLoadWithRequest:navigationType: 委托方法中处理您的特定网址,返回NO 并在您的应用中打开内容

    【讨论】:

    • 如果我只设置 return no 它无法打开我点击的链接,而不是查看不下载......还有更多的事情吗? (查看更新的答案)
    • 在返回NO之前,需要将URL放入处理它的代码中,应该和AppDelegate中的一样
    • 我通过调用适当的委托方法更新了我的答案 :) 有效!
    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 2019-04-09
    • 2019-06-03
    • 2012-09-01
    • 1970-01-01
    • 2018-02-17
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多