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 !!
Related
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.
So, my app queries an Amazon Dynamo DB database and retrieves a few kilobytes worth of data. What I want the app to do is download everything the first time, and then every time after, just download a timestamp to see if it has the most recent version of the data. So that I only have to download the data every once in a while, I'm trying to use NSKeyedArchiver to archive the array that I'm downloading. I have tried this three different ways, and none of them work on an iPhone, although two of them work on the simulator.
[NSKeyedArchiver archiveRootObject:self.dataArray toFile:#"dataArray.archive"];
This does not work on the simulator nor the actual iphone. The result of this method is NO.
The next thing I used was the full path:
[NSKeyedArchiver archiveRootObject:self.dataArray toFile:#"Users/Corey/Desktop/.../dataArray.archive"];
And this worked on the simulator, but not on the iPhone. My guess was that when compiled, the filesystem looks different (and obviously doesn't have the same path). So next I tried:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"dataArray" ofType:#".archive"];
[NSKeyedArchiver archiveRootObject:self.dataArray toFile:filePath];
Once again, this works on the simulator but fails on the iphone. I have confirmed that all of the data is in self.dataArray before writing to the archive, and confirmed that the array is nil after writing back to the archive (in the iphone version). Any ideas what's going on? Is there a better way to do the filepath?
This is what I tracked down:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent: #"dataArray.archive"];
[NSKeyedArchiver archiveRootObject:your_object toFile:filePath];
and it worked perfectly on both the simulator and the iPhone!
[NSKeyedArchiver archiveRootObject:self.dataArray toFile:#"dataArray.archive"];
You have to provide a full path.
[NSKeyedArchiver archiveRootObject:self.dataArray toFile:#"Users/Corey/Desktop/.../dataArray.archive"];
That is not a full path. A full path begins with / and does not have /../ anywhere.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"dataArray" ofType:#".archive"];
You do not have permission to write inside the mainBundle, it is read only.
Also, in general you shouldn't use file paths, you should use URLs. Some APIs (including this one) requires a path but URLs are the recommended approach these days.
Here's the proper way to write the file to disk:
NSURL *applicationSupportUrl = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask][0];
applicationSupportUrl = [applicationSupportUrl URLByAppendingPathComponent:#"My App"]; // replace with your app name
if (![applicationSupportUrl checkResourceIsReachableAndReturnError:NULL]) {
[[NSFileManager defaultManager] createDirectoryAtURL:applicationSupportUrl withIntermediateDirectories:YES attributes:#{} error:NULL];
}
NSURL *archiveUrl = [applicationSupportUrl URLByAppendingPathComponent:#"foo.archive"];
[NSKeyedArchiver archiveRootObject:self.dataArray toFile:archiveUrl.path];
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...
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
}
}
I created some files which store some logging information in my iPhone App. They are stored in the App's document folder. I would like to download them onto my Mac. I'm able to see them in the xCode organizer but I'm not able to access these files. How can I achieve this?
First, set Application supports iTunes file sharing on on your project's settings plist. It could also be named UIFileSharingEnabled.
Then, for the code, save the file in NSDocumentDirectory like so:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Id its a local file in the app use its name. If not, alter for your needs.
NSString *fileName = #"someFile.png";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
if (![fileManager fileExistsAtPath:documentDBFolderPath]) {
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
}
[self.window makeKeyAndVisible];
return YES;
}
All taken from iOS Dev Tips - File Sharing from App to iTunes is too easy
Using the Download button in the Organizer allows you to save a .xcappdata package to your Mac (Xcode 4.2, previous versions just save a folder). You can right-click and choose "Show Package Contents", a Finder window will open and you can find a copy of the Documents directory therein.