How to encrypt files in iPhone (PDF files) - iphone

i am developing a ebook reader and sending the ipa files to various people. The ipa files contains some PDF books. Is there any way in which i can encrypt the PDf files so that the user can see them only on device and not on PC... thanku

That's a general security question and the answer is: No. If one device (the iPhone) can decrypt the files without further data like a password or secret device key, another device (a desktop computer) can do this as well.
All you can do is obfuscate the files. That would keep people from simply unzipping the ipa and opening the PDFs. But any measure you take makes it only a little more difficult to access the files. There's no way to make it impossible for a skilled person to get at the data.

You could re-save the bundle pdf's using data protection, its not bulletproof but it makes is difficult to read the data (especially if the passcode is unknown), however it only works if the devices are pass-coded.
//There is probobly a quicker way to do this..ie..iterating the bundle programatically for pdf's
NSMutableArray * a = [[NSMutableArray alloc] init];
[a addObject:[NSString stringWithFormat:#"pdf1.pdf"]];
[a addObject:[NSString stringWithFormat:#"pdf2.pdf"]];
[a addObject:[NSString stringWithFormat:#"pdf3.pdf"]];
[self resaveFilesWithProtection:a];
[a release];
-(void)resaveFilesWithProtection:(NSArray*)fileNameArray
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * DocPath = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSString* s in fileNameArray) {
NSString * fullFilepath = [DocPath stringByAppendingPathComponent:s];//getting path to file
NSData *myData = [NSData dataWithContentsOfFile:filePath];//getting data out of old file
[fileManager removeItemAtPath:fullFilepath error:NULL];//deleting old file
NSError*er=nil;
[myData writeToFile:fullFilepath options:NSDataWritingFileProtectionComplete error:&er]; //saving back to disk with protection
}
NSLog(#"DONE");
}
You could also store the NSData in an SQLite database, or obscure the file extensions by re-saving them as .anything.
Edit:
If you don't want the user to be able to unzip the ipa and you think that extension obscuring isn't enough then your going to have to not put the pdf's in the bundle and pull them down from the network.

Related

iphone writeToFile: problem

Is there any way to save my .xml file to another directory other than "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..."
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"accUserNames.xml"];
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
NSLog(#"Error writing file !");
}
i wish to writeToFile: my .xml file to the desktop , any idea on how?
May the below code help,
NSString *documentsDirectory = #"/Users/student/Desktop/";
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"accUserNames.xml"];
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
NSLog(#"Error writing file !");
}
From within the iPhone Simulator, you should be able to successfully use #"/Users/student/Desktop/accUserNames.xml" as the path to write to. However, you can't do this on an iOS device (you'll be restricted to the application's sandbox directory — it's recommended you write to the Documents folder or other folders in there, depending what type of data you're storing).
Edit: I think I understand your problem. This part of your code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
effectively finds the path to "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..." (this is the normal place you want to save things). But if you just want to save to the desktop temporarily, you should just use appFile = #"/Users/student/Desktop/accUserNames.xml".
Note: I don't advocate this as a long-term solution, but if you just want to see the output of your program temporarily, it works fine.
If you want to upload your file to a server ("www.blahblah.com" in your example), then using the write to file methods is not the correct approach. This is only for writing data to the local file system (or on a network share, but that doesn't apply to iPhones).
If you want to transfer data to a webserver, you will need to have something on the server that will listen for a connection request, then it will need to accept the data which is transferred from your app. You cannot just write a file to "www.blahblah.com" as you would to a file system
Probably not with code, but you could try this: Open "Automator" (in the Utilities folder) and chose "Folder Action". As Input folder, you specify the directory of the documents (/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/...) and then you select, from "Files & Folders", "Duplicate Finder Items" and "Move Finder Items" and select the Desktop. Hit "Save", give it a name, and all files in the documents folder should be copied to the desktop.

Problem when trying to play (with MPMoviePlayerViewController) a video/song that I downloaded and stored in a file

Here is my problem.
I have a MPMoviePlayerViewController that play some videos wich are on the web. That part works.
But in order to play them later, without internet connection, I store them on the phone with that piece of code
NSData * data = [NSData dataWithContentsOfURL:[self dataURL]];
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * baseDocumentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
[data writeToFile:[baseDocumentPath stringByAppendingPathComponent:_itemId]
atomically:YES];
That part is ok, I can play the files on my iMac if i take them from the phone.
But after that when i do
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * baseDocumentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[baseDocumentPath stringByAppendingPathComponent:file.itemId]]];
There is just a gray Window in the modal viewController. And i get no notifications from the player.
Any ideas?
Thanks.
I came across this very same problem today.
It seems that iOS won't load any media files that have the wrong file extension. IMHO this is pretty stupid behavior, as I'm storing my media files with random names (UUIDs).
A quick workaround was to use the following code to create a symlink to the original file and give it the correct extension. Now iOS will happily load the file.
// Create a symlink for iOS as it won't load any files with the wrong extension
NSString *fixedFileName = [fileName stringByAppendingString:#".mp4"];
[[NSFileManager defaultManager] createSymbolicLinkAtPath:fixedFileName
withDestinationPath:fileName error:NULL];
Hope that helps. We simply ignore the fact that an error occurred, in case the symlink already exists.
Someone found what causes the problem.
The file name has no extension (like .mp4) so the MPMovieController doesn't try to read it (that sounds crazy to me -_- ). If I manually had .mp4 to my video file. the app can read it... I'm gonna append the extension of each file to its name.
Thanks anyway :)

Locally saved data files are persistant in the emulator but not in the real iPad device... I'm lost

I'm a (kind of) newbie XCode programmer (well, I would say not so newbie nowadays...) and "my pet issue" is: "I'm having trouble saving local files onto my real iPad, compared to saving them with the simulator".
Well to be honest, I have no problem whatsoever SAVING local files, but retrieving them. Why? Because on the simulator my local files seem to persist between compilation sessions, but on the real device, every time the application gets launched (not only after being uploaded from Xcode, but normally launched), data inside the "Documents" directory seems to disappear... So the final user would not be able to store needed historical data between sessions.
Is it a perception of mine? Is it normal behaviour?
The code I use to save this "persistent" data is this one:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDir = [paths objectAtIndex:0];
NSString *finalPath=[NSString stringWithFormat:#"%#/%#", documentsDir, path];
NSLog(#"Course.m: updatePersistentObject (to disk): final file = %#",finalPath);
[NSKeyedArchiver archiveRootObject:newObject toFile:finalPath];
'path' variable being #".HistoricalTestResults";
The code I use to retrieve data (wheather at boot time, or at runtime) is this one:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#" historical data: Documents paths = %#", paths);
NSString * docsDir = [paths objectAtIndex:0];
NSLog(#"Course.m: loadHistoricalResultsData: docsDir vale [%#]", docsDir);
NSString *tmpPath=[NSString stringWithFormat:#"%#/.HistoricalTestResults", (NSString *)docsDir];
NSLog(#"Course.m: loadHistoricalResultsData: tmpPath vale [%#]", tmpPath);
NSFileManager *localFileManager = [[NSFileManager alloc] init];
// create directory if it doesn't exist, don't do anything if it exists... (?)
[localFileManager createDirectoryAtPath:tmpPath withIntermediateDirectories:YES attributes:nil error:nil];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:tmpPath];
NSString *file;
while (file = [ dirEnum nextObject])
{
NSLog(#"Historical Data Folder: %#", file);
if ( [[file pathExtension] compare:#"dat"] == NSOrderedSame )
{
NSString *filePath = [NSString stringWithFormat:#"%#/%#", tmpPath, file];
NSLog(#"Course.m: loadHistoricalResultsData: filePath vale [%#]", filePath);
mHistoricalTestList=[[NSKeyedUnarchiver unarchiveObjectWithFile:filePath] retain];
}
}
[localFileManager release];
My exact problem is that while on the simulator, AT BOOT TIME, if I put a trace on the "while" code line, I can see how the enumerator gets some value, and I can iterate among the found files.
On the other hand, when using my iPad, the same breakpoint yields a "nil" pointer when obtaining the enumerator.
As I said, at the beginning of a clean program session, this is normal, so then I need to generate some storable results inside my program memory to store them onto disk.
I do it, and then I write them (both inside the simulator and the iPad). Then I can even re-retrieve this data (from disk) and it seems to still exist inside the Documents folder (both onto the iPad and the simulator).
But then, if I close/kill the program, this data seems to be lost onto the real iPad, and to persist in the simulator.
With this behaviour, my only deduction is "Real iPad programs cannot store persistent data onto their Documents directory". Am I right? (Of course not, because I've seen it work on some other programs).
So I have the feeling I'm doing something wrong, and after wasting TONS of time trying to find it, I'm now asking for advice on stackoverflow...
Every piece of help/insight/hint will be more than welcome.
Something I can think of is that your app is not being installed on the same app sandbox everytime. That means that while in the simulator your path is the same and the documents dir contains the same data, your ipad creates a new directory path for the installation, therefore the data you persisted in the last session won't be accessible.
Another thing that I experienced, was that the simulator was case insensitive when loading up resources, in contrast with the device that is actually case sensitive. In that time I had a strings file with extension .Strings and the iphone was looking for .strings. The simulator would work but the phone showed the keys when LocalizedString() macro was called.
So, tl;dr: verify the paths are the same in the different sessions, and your file names match.
I hope it helps. good luck!
PS: Gaudí rlz.
I'm happy! I was able to solve my own mess (!!?).
I've discovered that, when I originally copied my 2 code snippets, specially the "read part", I didn't copy it literally as it appears on my actual code, I just adapted some variable names and extra stuff that wasn't important here. (Typical situation)
I even did some more things than "removing some unrelated code pieces", (and here comes the important part), as I reordered some of them. And one of the parts I reordered was this one (which didn't work):
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString *tmpPath=[NSString stringWithFormat:#"%#/.HistoricalTestResults", (NSString *)docsDir];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:tmpPath];
// create directory if it doesn't exist, don't do anything if it exists... (?)
[localFileManager createDirectoryAtPath:tmpPath withIntermediateDirectories:YES attributes:nil error:nil];
which I changed (when posting here), onto this other part (which works):
NSString *tmpPath=[NSString stringWithFormat:#"%#/.HistoricalTestResults", (NSString *)docsDir];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
// create directory if it doesn't exist, don't do anything if it exists... (?)
[localFileManager createDirectoryAtPath:tmpPath withIntermediateDirectories:YES attributes:nil error:nil];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:tmpPath];
There's proably some logic behind this, as "why the first part does work on the emulator, but doesn't on the real device". It seems that "createDirectoryAtPath" is somewhat "resetting" some internal stuff, or maybe asking for an enumerator without having created a directory doesn't make sense...
Whatever the case is, I think my experience is worth enough to keep it here posted for someone who might be in my situation in the future!
Greetings again!

Simplest way on iPhone to unzip downloaded file?

Goal: download a zipped file, unzip it, and save it in the iPhone app's Documents directory.
The following code makes use of the initWithGzippedData method that was added to NSData in the Molecule app found here:
http://www.sunsetlakesoftware.com/molecules
As adapted to my app:
NSString *sFolder = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *sFileName = [sFolder stringByAppendingPathComponent:#"MyFile.db"];
NSURL *oURL = [NSURL URLWithString: #"http://www.isystant.com/Files/MyFile.zip"];
NSData *oZipData = [NSData dataWithContentsOfURL: oURL];
NSData *oData = [[NSData alloc] initWithGzippedData:oZipData];
[oZipData release];
b = [oData writeToFile:sFileName atomically:NO];
NSLog(#"Unzip %i", b);
Result: A zip file is successfully downloaded. From it a new, supposedly unzipped file is created in the Documents directory with the desired name (MyFile.db) but it has zero bytes.
Anybody see the problem? Or else is there a simpler way to unzip a downloaded file than the one used in the Molecules app?
I think that your problem may be that you are attempting to gzip-deflate a Zip file. Those are two different compression algorithms.
I based the gzip-deflating code in Molecules on this NSData category (the code of which I've copied into this answer) provided by the contributors to the CocoaDev wiki. What you'll want to do is use their -zlibDeflate implementation, which should properly unzip a Zip file.
Unrelated to your problem, instead of using NSHomeDirectory() and appending a path component, the recommended approach for finding the documents directory is the following:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
You should make sure your file is never too big, as you are loading it fully into memory before the unzip starts.

How do I find the size of my Core Data persistent store and the free space on the file system?

I am working on a database application using the Core Data framework. In this application I need to display how much data the application currently is using on the iPhone. Is there any way to do this?
I found this answer on the Apple Dev Forums to be useful for finding disk space available on the apps' home directory partition (note there are currently two partitions on each device).
Use NSPersistentStoreCoordinator to get your store collection.
Use NSFileManager to get each stores' size in bytes (unsigned long long)
NSArray *allStores = [self.persistentStoreCoordinator persistentStores];
unsigned long long totalBytes = 0;
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSPersistentStore *store in allStores) {
if (![store.URL isFileURL]) continue; // only file URLs are compatible with NSFileManager
NSString *path = [[store URL] path];
DebugLog(#"persistent store path: %#",path);
// NSDictionary has a category to assist with NSFileManager attributes
totalBytes += [[fileManager attributesOfItemAtPath:path error:NULL] fileSize];
}
Note that the code above is in a method of my app delegate, and it has a property persistentStoreCoordinator.
Your persistent store in Core Data is just a file on the file system. You access and possibly create this file when you create your Core Data stack. The following code will print the size of a persistent store and the free space of the file system, in bytes:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *persistentStorePath = [documentsDirectory stringByAppendingPathComponent:#"persistentstore.sqlite"];
NSError *error = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:persistentStorePath error:&error];
NSLog(#"Persistent store size: %# bytes", [fileAttributes objectForKey:NSFileSize]);
NSDictionary *fileSystemAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:persistentStorePath error:&error];
NSLog(#"Free space on file system: %# bytes", [fileSystemAttributes objectForKey:NSFileSystemFreeSize]);
This assumes that your persistent store is named persistentstore.sqlite and is stored in the documents directory for your application. If you are uncertain as to the name of your persistent store, look for where you alloc and init your NSPersistentStoreCoordinator. The name of the store should be specified somewhere in the code around there.
Note that the values you get back from the file and file system attributes dictionaries are NSNumbers, so you'll need to convert them to scalar types if you want to work with the file sizes in that manner. One thing to be careful of is that these values are in bytes, so for multi-gigabyte filesystems you might run into number size limitations with 32-bit integer data types.
Not sure where I saw it, but I believe that removing entries from the database will not necessarily shrink the database file. SQLite reclaims the storage internally and re-uses it. (This is typical of RDBMSs.) I believe there's a command-line utility somewhere that will compact it, but that's not going to help you if your app wants shrink the file to the dataset (to reclaim space for the OS, for example).
So while the file size method will give you a sense of the high-water-mark size of the database, it's not necessarily going to tell you the amount of storage used by your dataset.