iOS How to open pdf, not from NSBundle - ios

I have an app where I need to annotate a pdf. I have been using code from how to edit a PDF in objective-c? and it's great, except the file location part. I was getting the pdf from using
NSString *newFilePath = [[NSBundle mainBundle] pathForResource:#"Untitled_4" ofType:#"pdf"];
This was working in the Simulator, but will not work on my testing iPad.
I was getting this error:
CGDataConsumerCreateWithFilename: failed to open 'long boring file path' for writing: Operation not permitted.
I also played around with
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newFilePath = [documentsDirectory stringByAppendingPathComponent:#"Untitled_4.pdf"];
I now understand that "You are not allowed to write to the application bundle." Thanks The operation couldn’t be completed. Operation not permitted
When I later try to open the new annotated pdf it does not open correctly.
Any help would be greatly appreciated. You guys rock!
Hopefully this will help everyone else who needs help with pdf annotations in iOS.

Answering my own question.
I ended up using
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newFilePath = [documentsDirectory stringByAppendingPathComponent:#"Untitled_4.pdf"];
then drawing everything.
The problem was not there, it was getting the finished pdf later to show it. I needed to use that same code later.
Thanks for your help.
Hope anyone confused who finds this later will have their problem solved as well.

Related

iMessage App: Sticker from ImageFile from disk can be peeled of, but not placed

My standalone Message App saves images like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[imageName stringByAppendingString:#".png"]];
The PNG files are than loaded into MSStickers and correctly displayed in a MSStickerBrowserView. When I try to place the sticker in a conversation, the sticker disappears after a second. It works fine with my test image inside the Resources folder:
[[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
Does anyone know what kind of problem this is? There is no error message either.
Very simple answer: The saved image files where larger than allowed. After shrinking it works

XCode - file not found in document directory

I am having random error while reading saved photos from document directory in iPhone. I save photos taken from my app to document directory and then read it from there next time when user come back. However, after XCode 6 & base SDK change to 8.1, this document directory path keeps changing. So sometime I found photos and sometime not.
I read few posts online thats says that not Apple differentiate App from Data and that's why this issue coming up. Anyone has any thoughts on this? Any solution?
This is how I save file to document Directory
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path firstObject];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"key"];
[image writeToFile:path atomically:YES];
And this is how I read it:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path firstObject];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"key"];
UIImage *cellImage = [[UIImage alloc] initWithContentsOfFile:filePath];
Go to Product - Scheme - Edit Scheme - Options - Working Directory and specify working directory

This used to work: Displaying image using imageWithContentsOfFile

This was working for me yesterday morning and now it doesn't. So, I suspect something else changed to cause it...but I can't find the change. I've spent hours reverting my code back almost a week and still it's not working (and I know it was working yesterday morning). So, I'm hoping that in posting this specific issue (a symptom?) some ideas will surface that I can evaluate. Thanks.
I download images as they're needed:
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *targetFile = [NSString stringWithFormat:#"%#/%#.%#", documentDirectory, imageName, imageType];
// only download those where an image exists
if(![imageType isEqualToString:#""])
{
// only download the file if there is not already a local copy.
if([filemgr fileExistsAtPath:targetFile] == NO)
{
NSMutableData *imageData = [[NSMutableData alloc] initWithLength:0];
[imageData appendData:data];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *thumbNailFilename = [NSString stringWithFormat:#"%#.%#", imageName, imageType];
NSString *thumbNailAppFile = [documentsDirectory stringByAppendingPathComponent:thumbNailFilename];
}
}
Then display them:
NSString *imageFullName = [NSString stringWithFormat:#"%#%#", [greetingObject valueForKey:#"gid"], [greetingObject valueForKey:#"itp"]];
NSString *fullImagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:imageFullName];
UIImage *greetingImage = [UIImage imageWithContentsOfFile:fullImagePath];
self.greetingImage.image = greetingImage;
The variables "imageFullName" and "fullImagePath" are populated with the correct data and the image files are present on the simulator in the specified directory. Yet, "greetingImage" equals nil.
Here's what I get for "fullImagePath": /Users/Steve2/Library/Application Support/iPhone Simulator/7.1/Applications/8C9F8417-F6E2-4B38-92B3-82A88477CB7F/Documents/165.jpg
Here are the image files:
I have also tried variations using initWithContentsOfFile and dataWithContentsOfFile and get the same result. The greetingImage variable is nil.
I appreciate your ideas. I've even reinstalled Xcode in hopes that something got corrupted. No dice.
Added: One thing I just thought of... I did add the SystemConfiguration.framework to the project yesterday for an unrelated feature. It's currently at the top of the Linked Frameworks and Libraries list. I have no experience working with this. Could it be causing the problem?
Thanks.
Your code looks correct.
I would check that the images themselves are still okay. Looking at the screenshot you posted Finder isn't showing previews of the images which it should do with a valid JPEG. You say that the images are being downloaded so I suspect that they are being corrupted somehow on the way down.
EDIT:
Didn't notice that you were using initWithContentsOfFile. Since you are saving the files as NSData objects you will need to load them into memory as NSData objects and then init a UIImage with the data object, like so:
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
[UIImage imageWithData:imageData];

Finding the path of my documents folder in iOS

file://localhost/var/mobile/Application/EAC0BC34-6A82-41CB-AE44-9CD8E7D880C8/Documents
I get an alert this is my path, but i am unable to find where this location is all about. I am using iOS 5.0 to deploy my Application in ma device.
Thanks
Try this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
Or as per your tag, maybe you're using Titanium? If so maybe this helps:
https://wiki.appcelerator.org/display/guides/Filesystem+Access+and+Storage - Ti.Filesystem.applicationDataDirectory

FMDB open database

I'm trying to open a database that I have in my project inside Resources.
The problem is that it seems that its impossible to find the database file!
I tried with the complete path, and it works, but this is not a good solution.
I would like to now how to open it!
I'm using this code:
db = [FMDatabase databaseWithPath:#"bbdd.sql"];
I don't know how to find the other part of the "actual" path.
Do you have a solution for me?
Thanks!!!!
You need to find the full path of the database in the your resource bundle, something like this :
NSString *databasePath = [[NSBundle mainBundle] pathForResource:#"mySQLiteDatabaseFile" ofType:#"sqlite3"];
There's a complete example in this thread Copying data to the Application Data folder on the iPhone
Get Document directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingPathComponent:#"Test.db"];
Next
DB = [FMDatabase databaseWithPath:dbPath];

Resources