How to access and display documents from iPad3 programatically? - ipad

I am implementing an application that allows users to add and share different documents. I am done with adding documents by enabling "Application supports iTunes file sharing" in the plist. So user can add his/her documents directly to the application with the help of iTunes. Now my problem is I need access and display all documents under my application in a table view with their title. Based on the user selection I need to display it in a pdf or any other format.
How can I access all the documents under my application?
Also is there any other way to dump documents in to my application except using iTues? Please suggest a better option.

I found a solution for this. By using the following code snippet we can easily access all the files.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *s in fileList)
{
if ([s hasSuffix:#".pdf"])
{
//do stuff
}
else
{
// do stuff
}
}

Related

A Picker controller similar to UIImagePickerController to pick files ios 7

Just as we have UIImagePickerController to pick images into our iOS app. Is there a way to import the files in a similar manner as well? iOS 8 suggests a UIDocumentPickerViewController, but only for files in iCloud. Is there a way to import files in iOS 7?
Thanks!
Kaushil Ruparelia
iOS 8 allows you UIDocumentPickerViewController for picking files on iCloud. Other file inside your iDevice could not load or find with this controller.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *s in fileList){
NSLog(#"%#", s);
}
You can access files from your App Sandbox, and Some other type like, Images/Videos through UIImagePickerViewController.
In short, There is no way to get other files directly through any controller :(
HTH, Enjoy Coding !!

Loading XML config from URL on iOS - [Metaio SDK]

in must examples of Metaio SDK docs the xml's are loaded from Assets folder (more specifically, from NSBundle in iOS).
But in iOS, you can't write into a file in your app's bundle -- the entire bundle is read-only
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/index.html
In my project I need download all stuff to working with Metaio SDK, things like Videos, Sounds, 3D models and trackable images and your xml configs.
For example, from the Metaio docs, to work with a XML then are in Assets, are easy like:
NSString* MarkerTrackingFile = [[NSBundle mainBundle] pathForResource:#"tracking" ofType:#"xml" inDirectory:#"Assets/tracking"];
But how I said, I need to get this XML from internet, so, to do this I make an method to download.
(This is just a dirty code for study, not optimized without threads and anything special, for now)
/*********************************************/
-(void)downloadXML{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"markerless_tracking.xml"];
NSString *contents = [NSString stringWithContentsOfFile:filePath];
NSLog(#"%#", contents);
}
After this download, I try to use this XML in my project, I confirmed this consistence, but not work when I call him with this method, but i receive the message from error: No success loading the tracking configuration
- (void) loadTrackingConfigurationFiles
{
// Markerless tracking configuration file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* markerlessTrackingFile = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"markerless_tracking.xml"];
// We apply the correct one to the SDK
if(markerlessTrackingFile)
{
bool success = m_metaioSDK->setTrackingConfiguration([markerlessTrackingFile UTF8String]);
if( !success)
NSLog(#"No success loading the tracking configuration");
}
}
My main question is: Is possible loading a xml from outside NSBundle folder in METAIO SDK? In my project i don't have anything in local folders, everything is downloaded, tracking images, videos, sounds, xml etc, is viable the use Metaio SDK for this purpose?
I appreciate any help, examples or suggestions.

Testflight update build will remove datas

I have an application which downloads images/context from server.
Save images to /documents, context to database coredata.
Let say I've downloaded all the datas (app display all the necessary datas (images/context)) and for some reason I uploaded a new build of the app in testflight I then update the app on my device and when I run the app it now won't display the images that I've downloaded on the previous build but I still have the context. So my question is did the new build remove all the datas inside /documents? If so how come I still have my context datas (database coredata)?
Is your app utilizing an absolute path to the Documents folder? This is bad, as recorded here.
You would normally want to use something like:
- (NSString *)documentsDirectory
{
if (_documentsDirectory == nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [paths objectAtIndex:0];
}
return _documentsDirectory;
}
to get the path dynamically. You could then use the following to log it contents and see what's there:
NSArray *documentsDirectory = [fileManager contentsOfDirectoryAtPath:self.dataController.documentsDirectory error:NULL];
NSLog(#"documentsDirectory is: \n%#", [documentsDirectory description]);

How to open files in a UIWebView?

I just make a file browser for iOS, I just need to know how to open any kind of file in a UIWebview. I think the webview can open any kind of file, that's because I chose to use a UIWebView to view the files directly in the app. Now my question.
How to programmatically switch to a UIViewController in the same UIStoryBoard and display the file? The file URL is stored in a string called path.
This is how fare I am, I just need to add the file viewer.
Can someone help me?
-(void) displayFile:(NSString*)fileName
{
NSError *error = nil;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
TO get the filename
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath]) //Optionally check if folder already hasn't existed.
{
// do something
}
}
Hope this helps you...

iPhone App Update. "createSymbolicLinkAtPath" released?

I am using the symbolic link with iPhoneApp.
NSString* js_path = [[NSBundle mainBundle] pathForResource:#"js" ofType:nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if (js_path != nil) {
NSFileManager *fm = [NSFileManager defaultManager];
NSString* move_js_path = [NSString stringWithFormat:#"%#/js", [paths objectAtIndex:0]];
NSError *error;
if ([fm createSymbolicLinkAtPath:move_js_path withDestinationPath:js_path error:&error]) {
} else {}
}
I updated the application program. It becomes impossible to read the file afterwards.
However, the file was able to be read by reinstalling it after the application program had been deleted once.
Is there a problem in this code?
If anyones wondering about this, the reason is the application moves directory every time it's updated, the solution might be to use relative paths bundles are located at /var/mobile/Applications/[unique id that changes every update]/app.app whereas the documents directory is located at /var/mobile/Applications/[unique id that changes every update]/Documents so a symlink from Documents to the bundle will always work if it uses something like ../[bundlepath lastPathComponent]/Documents
ofc it's a good idea to build the relativeness from the directories you get at runtime rather than hard coding it, incase these relative paths change in the future.

Resources