我在iOS应用中使用HTML5的webviewer组件。
我添加了"HTML5"文件夹,"为任何添加的文件夹创建文件夹引用","将项目复制到目标组的文件夹"未选中。
在iOS中给出的示例代码有一个名为"xod"的文件夹,其中包含一个默认文档,并且该文件夹被添加为"为任何添加的文件夹创建文件夹引用"。
上述场景的示例代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];
webView.delegate = self;
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:webView];
NSString* fullPath = [[NSBundle mainBundle] pathForResource:@"/html5/MobileReaderControl" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:fullPath];
NSString* absoluteString = [url absoluteString];
NSString* stringWithQuery = [absoluteString stringByAppendingString:@"#d=iosrange://xod/GettingStarted.xod"];
NSURL* webviewUrl = [NSURL URLWithString:stringWithQuery];
NSURLRequest* webviewerRequest = [[NSURLRequest alloc] initWithURL:webviewUrl];
[webView loadRequest:webviewerRequest];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"iosrange"]) {
NSString *requestString = [URL absoluteString];
// get rid of the protocol
NSString *withoutProtocol = [[requestString componentsSeparatedByString:@"//"] objectAtIndex:1];
NSArray *urlParts = [withoutProtocol componentsSeparatedByString:@"#"];
// document location is everything before the question mark
NSString *docPath = [[urlParts objectAtIndex:0] stringByDeletingPathExtension];
NSString *docLocation = [[NSBundle mainBundle] pathForResource:docPath ofType:@"xod"];
// query string has start and possibly stop values for the byte range
NSArray *queryString = [[urlParts objectAtIndex:1] componentsSeparatedByString:@"&"];
NSInteger rangeStart = [[queryString objectAtIndex:0] integerValue];
NSInteger originalRangeStart = rangeStart;
NSInteger rangeEnd = [[queryString objectAtIndex:1] integerValue];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:docLocation];
if (rangeStart < 0) {
// a negative range means it's from the end of the file
// we need to calculate what that offset is from the beginning of the file
NSInteger fileSize = [fileHandle seekToEndOfFile];
rangeStart = fileSize + rangeStart;
}
[fileHandle seekToFileOffset:rangeStart];
NSData *data;
if (rangeEnd == 0) {
data = [fileHandle readDataToEndOfFile];
} else {
data = [fileHandle readDataOfLength:(rangeEnd - rangeStart)];
}
// convert data to base64
NSString *stringBuffer = [Utils encodeBase64WithData:data];
// call JavaScript function with the data
// setTimeout is used to make sure that it returns asynchronously
NSString *jsFunction = [NSString stringWithFormat:@"setTimeout(function() {window.CoreControls.PartRetrievers.IOSPartRetriever.PartSuccess('%@', %d);}, 0)", stringBuffer, originalRangeStart];
[webView stringByEvaluatingJavaScriptFromString:jsFunction];
return NO;
}
return YES;
}
但是我的文档保存在应用程序的"tmp"文件夹中,因为它是从服务器下载的。我无法从tmp文件夹加载文件。请帮助。
我不能将文档从"tmp"文件夹移动到"xod"文件夹,因为我不能在运行时更改Bundle,并且文档也在其他地方使用。
当我在"xod"文件夹中添加文档时,它会加载罚款。但是当我尝试从应用程序中的"tmp"文件夹加载文档时,它不会加载。我尝试在"stringWithQuery"中附加文档的路径,但没有运气。
主要问题是"stringWithQuery"。请帮助。
我想从"tmp"文件夹中加载文档。
问题似乎是示例代码是为了从应用程序包中读取.xod文件而编写的,但是您正在将xod文件下载到临时目录中。所以你需要改变
NSString* fullPath = [[NSBundle mainBundle] pathForResource:docPath ofType:@"xod"];
NSString* fullPath = [[NSTemporaryDirectory() stringByAppendingPathComponent:docPath] stringByAppendingPathExtension:@"xod"];