I am new to iOS and just started learning. I have created an App for downloading files into Documents folder of the directory. When I download the files on simulator, I can find it through the path : /Users/......./Simulator/iOS7.0/Applications/Documents.
But I am not able to find the files when I run the App on the device and download the files. I have given the path using the following code:
filePath = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: #"Documents/%#", self.downloadedFilename]];
Can anyone please tell me the way to find the files on the device using iTunes?
How about just downloading all the files from the sandbox to your Mac? Open Xcode, Window->Organizer.
Click on the Applications entry for the connected device, and click on your Application in the list of apps. Now you can see the data files on the device for that app, and you can download the whole bundle from the device to your Mac.
Use below sample code for create and write data with in created folder document
NSData *data = [[NSData alloc] initWithBytes:saves length:4]; //replace with your file content
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *appFile = [dataPath stringByAppendingPathComponent:#"MyFile"];
[data writeToFile:appFile atomically:YES];
Update: Did you check this bool in your info.plist before connect it with itunes.? Check this bool in your info.plist to share files with itunes. Refer UIFileSharingEnabled and see this picture below.
Related
In my project i saved the PDF file from NSData & displayed in WebView it working fine, i checked with Simulator & find in app folder the file is successfully downloaded without any corruption.
Here my problem is how can i view the file in real device(iPhone & iPad too) & share the files. like android file manager..
there is no option to view & share(Mail , Whatsapp etc..) the files?
my code below
NSError *errr;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/Folder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&errr]; //Create folder
[data writeToFile:[NSString stringWithFormat:#"%#/%#", dataPath, #"Report.pdf"] atomically:YES];
}
[self.webView loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
Help me..
Actually, instead of WebView you could also use the QuickLook
Framework, which is designed for this specific purpose. You just pass
the location of the file and conform to the protocols. It will present
a view controller with the document inside and also give you native options to share and open the document in other apps.
Find the answer here.
Hi I'm planning to add a static SQLite3 from my projects resource folder but I dont know if I'm doing it correctly.
so far here's my progress...
Created SQLite3 Database from SQLite Database Browser
Copied Sqlite3 Database to my project folder
Added network.db to my project resource folder
and now I don't know where to go next.. I hope someone can help me with my problem.
If you are modifying your database at run time (i.e. you are inserting, deleting or updating records), you need to create a copy from bundle to documents directory. iOS sendboxing mechanism will not allow you to modify bundle resource at run time. So, you need to copy it to documents directory. Below code can help you out:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath = [documentsDir stringByAppendingPathComponent:#"network.db"];
BOOL success = [fileManager fileExistsAtPath:filePath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] pathForResource:#"network" ofType:#"db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:filePath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable resource file with message '%#'.", [error localizedDescription]);
}
If you are referring your db for lookup purpose only (only using it for select queries), there is no need to copy from bundle to documents directory.
You need to add that project file into the documents directory,
You can do this for that,
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:#"networks.db"];
NSError *error;
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:documentsDirectory error:&error];
And then if you want to do anything with the SQLite3 file, you can use any SQLite Managers, For example SQLite Manager
check out my answer on this link .
this will copy your database into document directory file from your bundle. then you can use this database and insert , update , delete your data.
Hey guys a quick question!
I am making an iPad app which uses the dropbox sync api. Now I am wondering how to play movie files within my dropbox filesystem in the MPMovieplayer. Since the dropbox filesystem is not the same as the local sandbox and I don't know how to retrieve a nsurl of the file I can't play the video.
This is something I tried (my last hope) but it doesn't work:
+(NSString*)loadMovie : (DBPath*)path
{
DBFile *file = [self openFileAtPath:path];
NSData *data = [[NSData alloc] initWithData:[file readData:nil]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *vpath = [documentsDirectory stringByAppendingPathComponent:file.info.path.name];
[[NSFileManager defaultManager] createFileAtPath:vpath contents:data attributes:nil];
return vpath;
}
You'll need to write the file out to local storage first. See https://forums.dropbox.com/topic.php?id=110085 on the Dropbox forum.
I can do the reverse with the following: But cannot copy to the app bundle.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// Destination path
NSString *fileInDocumentsPath = [documentsPath stringByAppendingPathComponent:#"Passwords File.txt"];
// Origin path - used when file is in bundle
NSString *fileInBundlePath = [[NSBundle mainBundle] pathForResource:#"Passwords File" ofType:#"txt"];
// File manager for copying File in Bundle to Sandbox
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:fileInBundlePath toPath:fileInDocumentsPath error:&error];
The application bundle is read only. It cannot be modified after you deploy the app.
Files in your app's documents folder can be accessed via iTunes if the app supports File Sharing. Check out How to enable File Sharing for my app.
I have an app where I have a certain amount of .jpg pictures (roughly 300). They serve as something to start with, because they're actually located in the internet, but obviously it's more convenient for user not to download all of them at the first start of the App, but to have them pre-packed.
I am required to rewrite these images everytime I get new information from server. Obviously, I can't touch the app bundle, so I see my steps like this:
Unpack the images from bundle to the Documents Directory at the first start of an App.
Access them only from Documents Directory, but not from bundle.
If it's necessary, I should rewrite them.
And thus my code will be unified, cause I will always use the same path to get the image.
The problem is that I know very little about the whole file system thing in iOS, so I don't know how to unpack the particular bundle contents to Documents Directory and also I don't know how to write to Documents Directory either.
Could you please help me with some code and also confirm that my solution scheme is right?
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *destPath = [documentsDirectory stringByAppendingPathComponent:#"images"]; //optionally create a subdirectory
//"source" is a physical folder in your app bundle. Once that has a blue color folder (not the yellow group folder)
// To create a physical folder in your app bundle: drag a folder from Mac's Finder to the Xcode project, when prompts
// for "Choose options for adding these files" make certain that "Create folder references for …" is selected.
// Store all your 300 or so images into this physical folder.
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"source"];
NSError *error;
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destPath error:&error];
if (error)
NSLog(#"copying error: %#", error);
Edited per additional comment from the OP:
To rewrite with the same file name to the same directory, you can use a combination of fileExistsAtPath and removeItemAtPath to detect and remove the existing file before writing.
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
// now proceed to write-rewrite
Try this code
-(void)demoImages
{
//-- Main bundle directory
NSString *mainBundle = [[NSBundle mainBundle] resourcePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = [[NSError alloc] init];
NSArray *mainBundleDirectory = [fm contentsOfDirectoryAtPath:mainBundle error:&error];
NSMutableArray *images = [[NSMutableArray alloc]init];
for (NSString *pngFiles in mainBundleDirectory)
{
if ([pngFiles hasSuffix:#".png"])
{
[images addObject:pngFiles];
}
}
NSLog(#"\n\n Doc images %#",images);
//-- Document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
//-- Copy files form main bundle to document directory
for (int i=0; i<[images count]; i++)
{
NSString *toPath = [NSString stringWithFormat:#"%#/%#",documentDirectory,[images objectAtIndex:i]];
[fileManager copyItemAtPath:[NSString stringWithFormat:#"%#/%#",mainBundle,[images objectAtIndex:i]] toPath:toPath error:NULL];
NSLog(#"\n Saved %#",fileManager);
}
}