【发布时间】: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