我是 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/