startDownloadingUbiquitousItemAtURL local file path? - iphone

Where does startDownloadingUbiquitousItemAtURL:error method save downloaded file in local?
Is it the same as given URL?

Yes , its the same URL
The file in place before it downloads from iCloud to the device is a placeholder of sorts.
You can check the status of the URL with the key NSURLUbiquitousItemIsDownloadedKey
NSURL *foo = file://cloud/container/reference/tofile;
NSNumber *isDownloadedValue = NULL;
BOOL success = [foo getResourceValue:&isDownloadedValue forKey: NSURLUbiquitousItemIsDownloadedKey error:NULL];
if (success && ![isDownloadedValue boolValue]) {
[[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:foo error:NULL];
}
Theres no error handling in that snippet which you will want to do in production code.

Related

FileExistsAtPath returning NO on movie from UIImagePickerController

I saved a movie file's path from UIImagePickerController, and I know it exists because I can play it on the device. An NSLog on the string containing the movie file path returns this:
file://localhost/private/var/mobile/Applications/E694555D-3959-4CC5-A829-4260323C2C65/tmp//trim.6JemAI.MOV
When this string is used like this however, it returns NO:
NSLog(#"file exists: %i", [[NSFileManager defaultManager] fileExistsAtPath:media.movie]);
Any idea this this is failing? Could it be related to the value being stored as a path, or perhaps that the path includes // at one point? These are just some thoughts I've had.
You need to convert the URL to a file path.
NSURL *url = info[UIImagePickerControllerMediaURL];
NSString *path = [url path];
NSLog(#"file exists: %i", [[NSFileManager defaultManager] fileExistsAtPath:path]);
A path doesn't have the leading file://localhost.

How to List out .EPUB files in iPhone?

I am creating an epub reader. In that I want to list out .epub files from iphone. So I want to know is there any possible way to list out the .epub files from iphone (not just from the project directory path but also anywhere else in the phone)?
No, this is not possible.
Since there is no filesystem access, except the the directory with in apps sandbox.
All apps have to store the files they use with there sandbox, you tell iOS that you app can op .epub files. Which will allow the user to open the file from, example an email in your app.
As answered by #rckoenes, it is not possible to access the filesystem other than your app bundle.
You can access the files in your app bundle like this:
NSString *bundlePathName = [[NSBundle mainBundle] bundlePath];
NSError *error;
NSArray *bundleContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePathName error:&error];
for (NSString *currentItem in bundleContents) {
if ([currentItem rangeOfString:#"." options:NSBackwardsSearch].location != NSNotFound) {
int tempIndex = (int)([fileName rangeOfString:#"." options:NSBackwardsSearch].location);
tempIndex++;
NSString *aStrExtension = [[fileName substringWithRange:NSMakeRange(tempIndex, [fileName length]-tempIndex)] lowercaseString];
if ([aStrExtension isEqualToString:#"epub"]) {
//Add this file to an array, to make it available for choosing and view its details
}
}
}
If you mean you want to open ePub files saved not inside your application bundle, then you cant, you will have access only to the files inside your app sandbox
As per the #rckoenes: Any files out of App bundle is not accessible,
So I retrieved .epub files like this way.
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];
NSString *filename;
while ((filename = [direnum nextObject] )) {
if ([filename hasSuffix:#".epub"]) { //change the suffix to what you are looking for
[arrayListofEpub addObject:[filename stringByDeletingPathExtension]];
}
}

Check if file is already saved to disk?

I am downloading files from my server, saving them to device, and displaying them to the user in my app. I want to implement a check to see if the file already exists on the device so we can skip the download and just display, but I can't figure out the best way to do that.
I create a unique fileName for each file and then convert it to an NSURL like this:
NSString *fileString = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:fileName]];
self.fileURL = [[NSURL alloc] initFileURLWithPath:fileString];
Then I write to file and save the URL for use shortly after:
[data writeToFile:fileString atomically:YES];
self.fileURL = [[NSURL alloc] initFileURLWithPath:fileString];
How can I check if that File or URL already exists?
Thanks
NSFileManager has a method to check if a file exists at a path:
[[NSFileManager defaultManager] fileExistsAtPath: fileString]
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsfilemanager_Class/Reference/Reference.html#//apple_ref/doc/uid/20000305-CHDDDDJG

How do I get the PATH of a file that is in a known folder with a known extension, but with an unknown filename?

i'm working with an app for iOS, using an external framework for downloading an image. The file is saved in a known director inside documents directory, but the filename is unknown. Is an image, of a known extension. How can i get the filename or filepath? any idea?
thanks.
You can enumerate each file and check its extension
NSDirectoryEnumerator* myFolderDir = [manager enumeratorAtPath:myfolder];
for (id file in myFolderDir)
{
NSLog(#" file %#",file);
if ([[file pathExtension] isEqualToString:#"YOUREXTENSION"])
{
NSString *path = [myfolder stringByAppendingPathComponent:file];
[self.Files addObject:path];
}
}
Just iterate contents of your directory.
NSString *directoryPath = #"your directory path";
NSString *extension = #"your extension";
NSError *error = nil;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:&error];
if(!error) {
for(NSString *item in directoryContent) {
if([item rangeOfString:extension].location != NSNotFound) {
// item is filename at directory path with current extension
}
}
}

Question on retrieving information on file in application directory?

i have a program which i can download a video file from a server to the app's directory but how do i retrieve the information about this file (mp4). I would like know the size of that file and it there a way to know when the file is being created as in a date or time? Thanks in advance
NSError *error = nil;
NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath: filePath error: &error];
if ( fileInfo ) {
// examine fileInfo, see NSDictionary (NSFileAttributes) in NSFileManager.h
} else {
// handle the error
}