How to download PDF and store it locally on iPhone?

前端 未结 7 1324
时光说笑
时光说笑 2020-11-30 19:51

I am able to successfully view a PDF from a website. I want to be able to download that PDF to the device, then access that file locally.

When the app is opened, it

7条回答
  •  醉话见心
    2020-11-30 20:42

    I have found one method which I tried myself:

    // Get the PDF Data from the url in a NSData Object
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
        NSURL URLWithString:@"http://www.example.com/info.pdf"]];
    
    // Store the Data locally as PDF File
    NSString *resourceDocPath = [[NSString alloc] initWithString:[
        [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
            stringByAppendingPathComponent:@"Documents"
    ]];
    
    NSString *filePath = [resourceDocPath 
        stringByAppendingPathComponent:@"myPDF.pdf"];
    [pdfData writeToFile:filePath atomically:YES];
    
    
    // Now create Request for the file that was saved in your documents folder
    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    
    [webView setUserInteractionEnabled:YES];
    [webView setDelegate:self];
    [webView loadRequest:requestObj];
    

    This will store your PDF locally and load it into your UIWebView.

提交回复
热议问题