2015-01-07 2 views
0

Я разрабатываю приложение для прошивки 8.1 с помощью Xcode 6.1.1магазина Внутренне PDF из Интернета и отображения в WebView

И я хочу, чтобы скачать PDF из Интернета, хранить в свой IPad, а затем загрузить его в веб-просмотр; это тест PDF:

pdf screenshot

это результат я получаю на IPad после попытки вывести его на моем WebView:

iPad screenshot

это мой код до сих пор ...

// Get the PDF Data from the url in a NSData Object 
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]]; 

    // Store the Data locally as PDF File 
    NSArray *searchPaths   = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentFolderPath = searchPaths[0]; 
    NSString *archivePath   = [documentFolderPath stringByAppendingPathComponent:@"larg-file.pdf"]; 

    [pdfData writeToFile:archivePath atomically:YES]; 

    // Now create Request for the file that was saved in your documents folder 
    NSURL *url = [NSURL fileURLWithPath:archivePath]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [_webView setUserInteractionEnabled:YES]; 
    [_webView setDelegate:self]; 
    [_webView loadRequest:requestObj]; 

Как это сделать?

EDIT: это ответ

NSArray *searchPaths   = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentFolderPath = searchPaths[0]; 
    NSString *archivePath   = [documentFolderPath stringByAppendingPathComponent:@"larg-file.pdf"]; 

    if (![Functions isAlreadyStoredinDeviceFileWithName:@"larg-file.pdf"]) { 

     NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]]; 
     [pdfData writeToFile:archivePath atomically:YES]; 
    } 


    // Now create Request for the file that was saved in your documents folder 
    NSURL *url     = [NSURL fileURLWithPath:archivePath]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [_webView setUserInteractionEnabled:YES]; 
    [_webView setDelegate:self]; 
    [_webView loadRequest:requestObj]; 
+0

Как вы загрузили это в Webview? –

+1

Зачем использовать веб-представление? Почему бы не использовать 'QLPreviewController'? – rmaddy

+0

FYI - ваша логика для создания пути «Документы» неверна и не будет работать под iOS 8. – rmaddy

ответ

1

Этот код:

NSString *resourceDocPath = [[NSString alloc] initWithString:[ 
    [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] 
    stringByAppendingPathComponent:@"Documents"]]; 

Попытки сформулировать каталог в пачке приложения. У вас нет доступа для записи к набору приложений. Поэтому ваш файл не сохраняется.

Откажитесь от -[NSFileManager URLsForDirectory:inDomains:] или NSSearchPathForDirectoriesInDomains, как получить путь к папке документов пользователя.

+0

наконец-то помог – Jesus

Смежные вопросы