如何从pdftron的webviewer中的tmp文件夹加载文件?

时间:2014-05-06 04:46:27

标签: ios objective-c uiwebview pdftron

我在iOS应用程序中使用了webviewer组件的HTML5。

我添加了" 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;
}

但我的文件保存在&#34; tmp&#34;应用程序的文件夹,因为它是从服务器下载的。我无法从tmp文件夹加载文档。请帮忙。

我无法从&#34; tmp&#34;移动文件。文件夹到&#34; xod&#34;因为我无法在运行时更改Bundle,文档也在其他地方使用。

当我在&#34; xod&#34;中添加文档时捆绑包中的文件夹加载罚款。但是当我尝试从&#34; tmp&#34;加载doucment时应用程序中的文件夹,它不加载。我尝试在&#34; stringWithQuery&#34;中附加文档的路径,但没有运气。

主要问题是&#34; stringWithQuery&#34;。请帮忙。

我想从&#34; tmp&#34;加载文档文件夹中。

提前致谢。

1 个答案:

答案 0 :(得分:2)

问题似乎是编写示例代码以从应用程序包中读取.xod文件,但是您正在将xod文件下载到临时目录。所以你需要改变

NSString* fullPath = [[NSBundle mainBundle] pathForResource:docPath ofType:@"xod"];

NSString* fullPath = [[NSTemporaryDirectory() stringByAppendingPathComponent:docPath] stringByAppendingPathExtension:@"xod"];