Is it possible to search pdf file in whole iphone? - iphone

I want to search each and every pdf files on whole iphone.Is it possible in iphone sdk?
I have tried following code but it is giving our own application's files.
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
NSArray *onlyPDFs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self ENDSWITH '.pdf'"]];

Each application (and its associated files) are sandboxed under iOS, hence you won't be able to access files other than those produced by your own application.
i.e.: This isn't possible.

Related

How to change or replace database sqlite inside Iphone?

I have a problem with the sqlite database. In my app i read an external db that I copy inside the iphone. It works!! Now I modified the database and I Have to replace it inside the iphone. I deleted the database from project and i put the new one (same name), then I start the simulator (or the iphone) and the app use again the old database.
This is the code i use for copy the database:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appDBPath = [documentsDirectory stringByAppendingPathComponent:#"On4h.db"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"On4h.db"];
success = [fileManager fileExistsAtPath:appDBPath];
if (success) {
return;
}
success = [fileManager copyItemAtPath:defaultDBPath toPath:appDBPath error:&error];
if (!success) {
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
How it's possible to replace the database with another with the same name? How its possible that now the iphone read again the old one?
Thanks!!
To change the database you need to do the below..
Open Preferences
Go to location Tab and click on the arrow indicating.
Now go through the following :
Application Support -> iPhone Simulator -> 7.1 (your simulator version) -> Applications -> Find out your project -> Library -> Caches
on the above location you will found your old database. Now replace it with new one.
NOTE: Above process is for the iPhone/iPad simulator only. For iPhone/ iPad devices you need to delete and reinstall the app.

how to create plist file in xcode 4.3.2

I am trying to create my own plist file to be used in my application, I am using apple documents as refrence for doing this.
However, I have read that if you create your own plist you need to place it in the resource folder in your applications build. The issue with that is that I have no resources folder in my build... so I am woundering what should I do?.. I have read this guys answere here, he says its fine to just place the plist file in the supporting Files folder.. is this okay to do in regards to allowing the plist to be read and written too?
To read and write to plist the best practice is to copy it to document root if you want to access it through bundle you don't have write permission. I have provided a snap shot of the code here and how you can accomplish this.
NSError *err;
NSFileManager *fileManager = [NSFileManager defaultManager];
//getting the path to document directory for the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [paths objectAtIndex:0];
NSString *path = [documentDir stringByAppendingPathComponent:#"YourFile.plist"];
//checking to see of the file already exist
if(![fileManager fileExistsAtPath:path])
{
//if doesnt exist get the the file path from bindle
NSString *correctPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"YourFile.plist"];
//copy the file from bundle to document dir
[fileManager copyItemAtPath:correctPath toPath:path error:&err];
}
You can put that plist file anywhere you want.
The important thing will be copying it into the bundle. So to be sure for that check
project settings>build phases>copy bundle resources
You can open project settings by left-clicking on your project in the project navigator.

xcode/ios: trying to copy files to caches folder on install

I'm trying to copy a bunch of files to my app's caches folder when the app is installed on the device. I tried to use the Copy Files phase to achieve this, but i don't really know which path to use for its destination.
is this the right approach ... ?
or is this not possible at all?
I am not sure if it is impossible using this approach. However you can always write a method which preloads your resources on launch to Caches dir. Here's sample code:
NSString* path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
path=[path stringByAppendingString:#"/plpart1.txt"];
NSError* error;
NSString* source=[[NSBundle mainBundle] pathForResource:#"pl_part1" ofType:#"txt"];
[[NSFileManager defaultManager] copyItemAtPath:source toPath:path error:&error];

iPhone:Accessing documents directory file path in Safari

I have placed a file programmatically in documents folder on iPhone like below.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSError *error;
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"myfilename.xxxxx"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"myfilename.xxxxx"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, #”Failed to create writable database file with message ‘%#’.”, [error localizedDescription]);
}
I want to access this file in Safari by giving the path of local file through the below code.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:localFileURL] ];
Is it possible? I saw somewhere, we can use the URL starting with "file:///" . Is it true? If yes, how can i access my documents directory file path in Safari passing the URL via the above one line code?
Please advise.
Thank you!
I would expect that that is not possible because every app is sandboxed, which means your app's data is not accessible to any other app, and vice versa.
To add to that, file:// is not one of the recognized or supported iOS schemes, as far as I know.
See run locally created file on iPhone safari
If what you mean is to run the html in a browser inside the app that LOOKS LIKE Safari, you can use a UIWebView and call
loadHTMLString:baseURL
You can create your file URL like this:
NSURL *myFileURL = [NSURL fileURLWithPath: pathToMyFile];
But like everyone else says, you can't access an app's file from any other app (without jailbreaking).

How do I get an NSArray of filenames of all files in a given directory in my app?

What I want to do seems simple enough: Get an array of filenames in a given "directory" on my app. But the more I play around with NSFileManager and NSBundle I find myself getting more lost...I just want to get the filenames of the files organized in a specific directory in my iPhone Xcode project...For example: The directory in question is called "Images" (created using "Add > New Group" under the project icon in Xcode) and contains 10 .png images. It seems simple but I am getting nowhere. I'd appreciate some help in finding the answer.
Q: How do I get an NSArray of filenames of all files in a given directory in my app?
If you want the folder to be part of the App bundle, you need to create a folder reference inside the Resources group, instead of a group (it will show up in Xcode as a blue folder as opposed to the yellow one).
To do this, you drag the folder from Finder and select folder reference when prompted.
Once you have that, you should be able to get the contents of the folder with something like:
NSError *error = nil;
NSString *yourFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"YourFolder"];
NSArray *yourFolderContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:yourFolderPath error:&error];
The accepted answer's been deprecated for years now. The following should be used instead.
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
Update: #macserv updated the accepted answer after I posted this.
Nowadays, this is how you do it:
From the Finder, Drag & Drop your folder in your project's navigator and the next popup will show up:
As you can see in that screenshot, you have to select Create folder references.
Do what the solution for this post is tells you to do. I mean:
NSError *error = nil;
NSString *yourFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"YourFolder"];
NSArray *yourFolderContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:yourFolderPath error:&error];