我正在为 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”文件夹加载文档。
提前致谢。