iphone - 在iphone的webview中打开下载的文件

标签 iphone objective-c ios uiwebview

我是 iPhone 开发的新手。我正在开发一个 iphone 应用程序,我正在使用带有以下代码的 Web 服务下载文件:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath =  [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:@"/filename.pdf"];
NSData *fileData = [self decodeBase64WithString:soapResults];
[fileData writeToFile:fileLoc atomically:YES];

我的文件在路径中:/Users/myusername/Library/Application Support/iPhone Simulator/4.0/Applications/7A627E64-DEAB-40FA-BE04-35ED50580B65/filename.pdf

如何使用 uiwebview 打开此文件???我试过这个:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath =  [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:@"/filename"];
[self loadDocument:fileLoc inView:mywebview];

我的 loadDocument 方法是:

-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];  
}

但它显示错误......我认为位置是问题所在......我如何在模拟器和原始设备中找到路径???

请帮帮我...

最佳答案

pathForResource:ofType: 只能用于加载资源文件,这些文件作为应用程序包的一部分提供。您正在尝试从 Documents 目录加载下载的文件。所以你应该能够做到

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath =  [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:@"/filename.pdf"];
NSURL *url = [NSURL fileURLWithPath:fileLoc];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request]; 

关于iphone - 在iphone的webview中打开下载的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6871191/

相关文章:

iphone - ECSliding,在LeftViewController下更改

ios - UIActivityItemProvider 的 -item 方法 : waiting for an NSURLConnection?

ios - 如何实例化(附加) Storyboard并将其推送到我的应用程序中?

ios - 面试 - 在中心定位和重置球

ios - getBytes 方法的 Swift 3 更改

iphone - 从后台线程访问实例变量

iphone - NSURLConnection 委托(delegate) didReceiveData 数据中的尾随字符

ios - 适用于 iOS 的 Dropbox 集成

ios - UIButton 奇怪的状态变化(UIControlState)

ios - 处理向同一方向滚动的嵌套 UIScrollViews 的触摸