How to retrieve PDF in iPhone - ios

I am trying to retrieve PDF file using NSSearchPathForDirectoriesInDomains.i am getting path for PDF file but how to convert that pdf file into nsdata or nsmutabledata(bytes).Here is my code to get path of PDF.
NSArray *arrayPaths1 =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path1 = [arrayPaths1 objectAtIndex:0];
path1 = [path1 stringByAppendingPathComponent:#"mypdffile.pdf"];
in path1 i am getting the path and to get byte data I am trying
[decodedData writeToFile:path1 atomically:YES];
decodedData is NSMutableData.decodedData is nil.Please help me.How can I solve this problem?

Try below code..
NSData *dataPdf = [NSData dataWithContentsOfURL:pdfOnline.url];
//Get path directory
NSArray *arrayPaths1 =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//Create PDF_Documents directory
NSString * documentsDirectory = [arrayPaths1 objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:#"mypdffile.pdf"];
[[NSFileManager defaultManager] documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"PDFRefYouPreferred"];
[dataPdf writeToFile:filePath atomically:YES];
Hope it will fix the issue..!

You can use [NSData dataWithContentsOfFile:<#path of your file#>] for that purpose.
In your case it should be:
decodedData = [NSData dataWithContentsOfFile: path1];

Related

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]];
}
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

Imagepath Replace in Document Directory

For using NSFileManager, if exists the Image path or folder,how to replace new image path (or) Folder in document directory .
Is there the replace feature in NSFileManager?
(how to replace image path position with new image path in document directory)
You want to owerwrite an image, just write it to file.
If an image with the same name exists at the same path, it will be overwritten:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
UIImage * imageToSave = [UIImage imageNamed:#"Icon.png"];
NSData * binaryImageData = UIImagePNGRepresentation(imageToSave);
[binaryImageData writeToFile:[basePath stringByAppendingPathComponent:#"myfile.png"] atomically:YES];
You can not replace a path using NSFileManager because, you've already saved ImagePath. Now you can check if that path is exists then you can remove that filePath using this:
if ([fileManager fileExistsAtPath:Path] == YES) {
[fileManager removeItemAtPath:Path error:&error];
}
Now save your new path.
If you want to replace content then simple remove first then write again using
writeToFile:filePath
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* foofile = [documentsPath stringByAppendingPathComponent:filename];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
NSLog(fileExists ? #"Yes" : #"No");
if (fileExists == YES)
{
NSString *filePath = [NSString stringWithFormat:#"%#/%#", aDocumentsDirectory,filename];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:filePath atomically:YES];
}

ios - cannot decrypt after encryption

I'm having troubles decrypting pdf files and displaying them with presentViewController after they are encrypted.
When I download the pdf files, they are being encrypted like this:
NSData *pdfData = [[NSFileManager defaultManager] contentsAtPath:filePathDocumetFolder];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *docFolder = [NSString stringWithFormat:#"/Documents/%#", documentFilePath];
NSString *filePath = [documents stringByAppendingPathComponent:docFolder];
NSString *pdfName = [NSString stringWithString:filename ];
NSError *error;
NSData *encryptedPdf = [RNEncryptor encryptData:pdfData withSettings:kRNCryptorAES256Settings password:#"A_SECRET_PASSWORD" error:&error];
if(error){
NSLog(#"error: %#", error);
}
NSLog(#"where?? FileTra%#", filePath);
[encryptedPdf writeToFile:[filePath stringByAppendingPathComponent:pdfName] atomically:YES];
I know that this encryption above works, as when I browse the filesystem with iExplorer, I cannot open the files because they are protected.
In my DocumentHandle controller I'm trying to decrypt them so that they can be viewed:
NSDictionary* dict = [command.arguments objectAtIndex:0];
NSString* urlStr = dict[#"url"];
NSURL* url = [NSURL URLWithString:urlStr];
NSString* fileName = [url path];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent: fileName];
NSData *dataEn = [[NSFileManager defaultManager] contentsAtPath:[path stringByAppendingPathComponent:fileName]];
NSLog(#"this to decrypt%#", [path stringByAppendingPathComponent:fileName]);
NSData *decryp = [RNDecryptor decryptData:dataEn withSettings:kRNCryptorAES256Settings password:#"A_SECRET_PASSWORD" error:nil];
[decryp writeToURL:[[NSURL alloc] initFileURLWithPath:path] atomically:YES];
if(decryp){
NSLog(#"decrypted");
}else{
NSLog(#" not decrypted");
}
weakSelf.fileUrl = [[NSURL alloc] initFileURLWithPath:path];
For some reason, the pdf files are not being decrypted and I'm being presented with a blank file even thought I'm receiving NSLog as decrypted :(
Can anyone help me please? thank you

How to store JSON data in a file?

I am building an article reading iOS app.
I have a .json file in match folder named as Data.json
I am not able to write data into it.
Here is my code:
- (void)writeJsonToFile {
//applications Documents dirctory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//live json data url
NSString *stringURL = ysURL;
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
//file to write to
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"Data.json"];
//attempt to download live data
if (urlData) {
[urlData writeToFile:filePath atomically:YES];
}
//copy data from initial package into the applications Documents folder
else {
//file to write to
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"Data.json"];
//file to copy from
NSString *json = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"json" inDirectory:#"/match/Data.json"];
NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil];
//write file to device
[jsonData writeToFile:filePath atomically:YES];
}
}
Try using NSStrings method to write the file, and check your error code:
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource: #"Data" withExtension:#"json"];
NSError * error;
[json writeToURL:fileUrl atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#",error.localizedDescription);
can you tell us more about the problem you have ?
1) You don't have to redefine the filePath in the else because you already did before.
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"Data.json"];
And you should use stringByAppendingPathComponent which automaticaly add the / you will need or not :
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Data.json]
2) Are you sure your Data.json is include in your project ? do you get a nil ?
NSString *json = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"json" inDirectory:#"/match/Data.json"];
3) For the problem of not load : </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.pl​atform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)
Cannot find executable for CFBundle CertUIFramework.axbundle

How to view the downloaded files in Device ipad/iphone

Hi in my application i have option of downloading a pdf file i have searched for that i got the code i have used its working fine in my mac i can see the downloaded pdf file. But the problem is if i download the same pdf in device where i can find the downloaded pdf file in device.
This is my code which i have used to download the pdf file.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://jesusredeems.com/mag/pdf/JRE-2014-03.pdf"]]; //Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"11011.pdf"]; [pdfData writeToFile:filePath atomically:YES]; NSLog(#"%#",filePath);
If i print the filepath I'm getting like this.
/var/mobile/Applications/353C27AD-1E64-41CA-A429-16C2A20C8606/Documents/11011.pdf
In my mac i can see the downloaded file in that path but in my Ipad where i can see the downloaded pdf file please anyone tell where i have to find it.
Thanks.
Most likely your saving part is correct but try this anyways.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://jesusredeems.com/mag/pdf/JRE-2014-03.pdf"]];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSString *destPath = [documentsPath stringByAppendingPathComponent:#"11011.pdf"];
[pdfData writeToFile:destPath atomically:YES];
And do this to retrieve it.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSString* dataFile = [documentsPath stringByAppendingPathComponent:#"11011.pdf"];
NSData *pdfData3 = [NSData dataWithContentsOfFile:dataFile];

Resources