App-only accessible folder in ios - ios

I have an ipad app in which i show PDF documents in a UIWebView from my server.I want users to be able to download these documents so that they can read them offline. However i want them to read it only in the app.Basically my question is: Is it possible to have a folder that is only accessible from the app but not by users.

you can save it in the app bundle.
NSDate *date = [NSDate new];
NSString *docName = [NSString stringWithFormat:#"%lld.pdf", (long long)[date timeIntervalSince1970]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
dataPath = [documentsDirectory stringByAppendingPathComponent:docName];
EDIT: acording your comments question i think the solution would be something like this:
NSString *str = #"http://myserver.com/myfile.ext";
NSURL *url = [NSURL URLWithString:str];
NSData *content = [NSData dataWithContentsOfURL:url];
if (content) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"myfile.ext"];
[content writeToFile:dataPath atomically:YES];
}

Steps:
Create password to PDF file and Open PDF programatically with password.
You can Make Own DRM for PDF files.
Refer Following Link:
iPhone Documents Folder Library/Caches Security issue

Related

Cannot locate PDF file in iPhone

In one of my activity, I displayed a PDF using WebView on screen and tried to save this PDF using this code:
_pageSize = CGSizeMake(width, height);
NSString *newPDFName = [NSString stringWithFormat:#"%#", name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
NSLog(#"path=%#",pdfPath);
When I run this code in iOS simulator (using Xcode) and show the path and I opened this PDF file successfully in documents folder, but when I run this code in an iPhone, I got this path:
/var/mobile/Applications/0AF98361-C8DF-4C35-9E9F-EE48555185BC/Library/354746396.pdf
So where are PDF files stored in iPhone?
"..InDomains(NSLibraryDirectory, NSUserDomainMask, YES);"
"../Library/354746396.pdf"
Your paths is looking in the NSLibraryDirectory, it should be looking in the NSDocumentDirectory like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
If still fail, try this code:
NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *savePath = [documents stringByAppendingPathComponent:#"filename.pdf"];
NSLog(#"savePath: %#", savePath);
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
webView.scalesPageToFit = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}
my coding is right but i will not find the pdf file.i will solve my problem.i will find my pdf file in device.
there are two ways to find the pdf file.
1)install the iexplorer filemanager software.than open document folder.
2)connect your device in to the mac.select window menu and click the device in xcode.than after select your device name.select your app.
select show container.than after you can see the document folder....

How to write data to my App document after take a photo from Camera?

It's possible to save the shooting pictures into the photo library through apply for writeImageDataToSavedPhotosAlbum: after took pictures by camera.
But I don't want to save the picture into the photo library and wanna to save them into my owned APP catalog. Currently, I can get the NSdata and Metadata.
Is there anybody could advise me what I should do? Really appreciated!!
To save the NSdata to your own app documents folder, use the below code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirPath = [paths objectAtIndex:0];
NSString *filePath = [documentDirPath stringByAppendingPathComponent:#"ImageName.png"];
[yourNSdata writeToFile:filePath atomically:YES];
You can read it after using the below code,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:filePath];
NSData *myImageData = [NSData dataWithContentsOfFile:imagePath];
UIImage *savedImage = [UIImage imageWithData:myImageData];

How To Play Video from HTML5 in iOS?

i want to play video from html 5 file by zipping and unzip file programmatically of this link
http://enhancelearning.co.in/downloads/HTML5.zip
this file contain 4 html file
zip and unzip successfully created but video not playing in web view
here is th my code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [paths objectAtIndex:0];
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
// NSString *cachePath = [paths objectAtIndex:0];
NSString *zipFile = [docspath stringByAppendingPathComponent:#"001.html"];
NSURL *url = [NSURL fileURLWithPath:zipFile];
[webVew loadRequest:[NSURLRequest requestWithURL:url]];
Try first to put this html file somewhere in the net and see if the video playing when you go to this page from safari.
It's probably HTML-Safari-ios problem, and this way it easier to debug.

Writing data into file

I'm trying to make a txt log file with the actions that happens in my app. I want to save some text whenever the app is connecting to the server o displaying new info.
Well, how do I write to a file? I've tried using the method writeToFile: but it's not working because fileExistsAtPath: is returning NO.
My code is:
NSString *file_path = #"mylog.txt";
NSString *log = #"Hello World!!\n";
[log writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil];
Thanks!
PS: Oh, would it be readable through Organizer with the iPhone plugged in?
You should use the < App_Home >/Documents folder for storing Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file_Path = [documentsDirectory stringByAppendingPathComponent:#"log.txt"];
[log writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil];
But normally if you want to see and dump things while running the app,
you could just use NSLog() which outputs it in the console.
Try this method I wrote:
+(NSString*)createPath:(NSString*)fileName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localizedPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",fileName]];
//NSLog(#"%#",localizedPath);
return localizedPath;
}
It will return you a path for your file. You only need to give it a name.

Read a .txt file inside documents directory

How can I read a .txt file inside documents directory? Can you show me code?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
You can also use NSFileHandle for writing data in file and save to document directory:
You can follow: NSFileHandle

Resources