html - UIWebView 从 <a href=myFileName.myFileType> 下载

标签 html ios objective-c uiwebview mime-types

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

  • 我已经注册了 mimetype。我试过添加 download="target.myfiletype"到 anchor 标签,但它仍然只是 在 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];

}

最佳答案

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

关于html - UIWebView 从 <a href=myFileName.myFileType> 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053188/

相关文章:

html - 如何将两列并排放置

ios - UITableView 委托(delegate)

ios - Paypal Ruby SDK 未获取刷新 token

ios - iOS 不支持 Facebook OAuthSwift redirect_uri

iphone - 在安装或更新后以编程方式在应用程序中还原以前的应用程序内购买吗?

objective-c - UINavigationController 仅显示一半的导航栏

javascript - flask 倒计时

html - 拉伸(stretch)或滚动父 flexbox

html - CSS3 Firefox 径向渐变消失

java - 针对 iOS 开发者的 Android 指南提示?