iosobjective-ciphonedocumentsdirectory

Binary to pdf convert without store in documentsDirectory in obj c


Have a Binary code convert it to nsdata using dataFromBase64String. And load it web view successfully. But Want the pdf file do not store in documentsDirectory.Need to add one button click user click the button, then Binary to pdf convert will happened and display in web view. here my code. How is possible help me. thanks advance.

  NSString *binaryString =@"Binary code to change";
  myData = [NSData dataFromBase64String: binaryString];
  [self savePDF:myData];


- (void)savePDF:(NSData *)pdfContent
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
    NSString *documentsDirectory = [paths objectAtIndex:0];

   finalPath = [documentsDirectory stringByAppendingPathComponent:@"myPdf.pdf"];
    NSLog(@"%@",finalPath);
    NSURL *url = [NSURL fileURLWithPath:finalPath];
    [pdfContent writeToURL:url atomically:YES];
     [aWebView loadRequest:[NSURLRequest requestWithURL:url]];
}

Solution

  • If you don't want to store pdf file to DocumentsDirectory then try following, hope it will work for you

    NSString *binaryString =@"Binary code to change";
      myData = [NSData dataFromBase64String: binaryString];
      [self savePDF:myData];
    
    
    - (void)savePDF:(NSData *)pdfContent
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
       finalPath = [documentsDirectory stringByAppendingPathComponent:@"myPdf.pdf"];
        NSLog(@"%@",finalPath);
        NSURL *url = [NSURL fileURLWithPath:finalPath];
        //[pdfContent writeToURL:url atomically:YES];
         [aWebView loadRequest:[NSURLRequest requestWithURL:url]];
    }
    

    Comment //[pdfContent writeToURL:url atomically:YES]; line and check