2014-05-06 44 views
1

我使用webviewer組件的iOS5應用程序的HTML5。如何從pdftron的webviewer中的tmp文件夾加載文檔?

我已將「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」文件夾加載文檔。

在此先感謝。

回答

2

問題似乎是示例代碼被寫入從應用程序包中讀取.xod文件,但您正在將xod文件下載到臨時目錄。所以,你需要改變

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

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