Transfer file to physical iOS device - ios

I have set up a provisioning profile to debug my app (through Xcode) on a connected physical device.
The problem is this app requires certain supporting files. With the simulators on Mac, I simply navigate to the Documents directory for the app under the simulator directory and place the files there.
Is there a way to get these same files on to the physical device?

Place the files within your project files structure.
So they are copied to your App Bundle and will be available through the documents directory.
To add files to your iOS Project correctly:
Right click on the project icon at the top of the file list on the left.
Select Add files to <YourProjectName>
Select the folder/file you want to include and click Add.
Don't forget to select the correct target by selecting it from the given list. Refer to the screenshot please.
If your resource is not a single file but directory structure and you want all the directory tree copied, then remember to select the Added folders: Create groups
The files adding pop up window would seem like the following in XCode 6.x:
When the target is built, open up the bundle and your directory structure will exist inside fully intact. Not only that, these files may be accessible through iOS SDK as following.
So you may have to copy them to documents/library directory within the App as you may want to access them within the app.
Use the following code to do copy them.
// Check if the file has already been saved to the users phone, if not then copy it over
BOOL success;
NSString *fileName = #"test.jpg";
NSString *LIBRARY_DIR_PATH = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [LIBRARY_DIR_PATH stringByAppendingPathComponent:fileName];
NSLog(#"%#",filePath);
// Create a FileManager object, we will use this to check the status
// of the file and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the file has already been created in the users filesystem
success = [fileManager fileExistsAtPath:filePath];
// If the file already exists then return without doing anything
if(success) return;
// Else,
NSLog(#"FILE WASN'T THERE! SO GONNA COPY IT!");
// then proceed to copy the file from the application to the users filesystem
// Get the path to the files in the application package
NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
// Copy the file from the package to the users filesystem
[fileManager copyItemAtPath:filePathFromApp toPath:filePath error:nil];
Hope the above code sample is clear for you.
So whenever you want to access that file within your App, you can get reference to that file by getting it's path as follows:
NSString *sqliteDB = [LIBRARY_DIR_PATH stringByAppendingPathComponent:fileName];
Note: In any case if you require the files to be copied to the Documents directory within users app installed location, Replace the LIBRARY_DIR_PATH with the following:
NSString *DOCUMENTS_DIR_PATH = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Hope this answer is helpful to you!
Cheers!

Related

iOS: Is this the proper way to setup a database location under the application support folder?

I have a sql file that needs to be stored in my App. A user on this site recommend me to store it underneath the Application Support folder. This is the path directory that I am using with my fmDatabase library.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
self.dataBasePath = [applicationSupportDirectory stringByAppendingString:#"Email.sqlite"];
self.fmDataBase = [FMDatabase databaseWithPath:self.dataBasePath];
Everything seems to be working fine. However, I am a little nervous that I may be doing something wrong. I am using the Application Support directory because I don't want the OS or an update to interfere with the Database. I would use the document directory ,but my App supports file share so the user can mess it up.
(application_home)/Library path is a place where you can create custom subdirectories for files you want backed up but not exposed to the user.
create a directory such as (application_home)/Library/Database and save files there.
for mac os x with sqlite The convention is to store it in the Application Support folder .
i.e. in ~/Library/Application Support/YourAppNam*/database.db
I think the data will be saved on the Application Support Folder in Mac platform, for iOS, the default is xxx/Library.
I don't see any dangerous element on this, because you can save to different folder as long as you can load it from right path.

create simple txt file and read from it objective c

I want to create mock implementation of server. I have several json-files.
I want to add these files to the xcode project, and then read from them in the application.
In android we have assets folder. And we can place there files, and they will be installed with the application.
Is there any asset folder in ios to store files, and how to read files from there?
In iOS App Bundle is the main directory. If you put your file there you can read as such:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"txt"];
NSString *testString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(#"%#",testString);
This will read file myFile.txt

Replace Data Container of an iOS .ipa File

I have an app that downloads a whole bunch of data from over 100+ APIs upon successful login. I successfully download the data, and then use iExplorer to extract the data container folders (Documents, Library and Tmp) from the fully loaded application.
I would like to take a blank version of the original app, in .ipa format, and insert those data container folders into that fully loaded .ipa file. Then I will be able to take this new fully loaded .ipa, and use a deployment software to deploy it to a bunch of local user's devices. So everyone will have this fully loaded app.
Please, has anyone done this? Please provide some feedback, and don't argue with my methodology, because this has be done this way due to requirements. Maybe there is a step I'm missing? I'm not sure.
With the source code in hand, you can run the app in the simulator (no need for iExplorer), wait for it to download all the files and browse to the folder on your computer where the app was installed.
From there you can put aside any files you want along with their respective folders. If you're using Coredata there should be a SQLITE database file there somewhere (typically in your Application Support folder) and this might be all you need but it is hard to tell without looking at your implementation details.
Once you have the files you need set aside, add them to the app bundle via Xcode and create code to check whether files already exist (in which case you don't want to replace them), and if not copy all files needed from the bundle into their respective folders.
Here's some semi pseudo-code for you:
NSDictionary *userPrefs = [[NSUserDefaults standardUserDefaults] objectForKey:self.email];
if (![userPrefs[kInitialSetupCompleted] boolValue])
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *destinationFilePath = ...
NSURL *seedFilePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:kCoreDataStoreName ofType:nil]];
NSError* err = nil;
if (![fileManager copyItemAtURL:seedPath toURL:destinationFilePath error:&err]) {
NSLog(#"Could not copy seed data. error: %#", err);
// Deal with error
} else {
// Set user defaults kInitialSetupCompleted to YES
}
}

IOS creates new Application ID everytime I open the app, or run it from Xcode

With the upgrade to IOS8 and Xcode 6.0.1, I have noticed that adding images, writing them to file, and displaying them is broken in my app that has been working for over 2 years. It works fine at the time of taking the photo and saving it - it displays it fine. I store the FilePath in Core Data. But as soon as I close the app and reopen, or run the simulator again, it disappears.
I have tracked the problem that every time it runs on both the device or the simulator, the documents directory string changes, with the APPLICATION ID changing, so the image is not found in the path as it can't seem to access the path of the previous APPLICATION ID that it had.
I looked for this and couldn't find any answers. Has anyone else seen this and have any suggestions for how I can get around it. Thanks so much!
Kat
Edit - I have added this code example. Every time I run my app, this is the path that changes.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"documents directory %#", documentsDirectory);
Store your relative path, here by calling the temp directory, but not the full absolute path. The App container is changed at every start, due to sandboxing safety.
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:#"MyPic"] URLByAppendingPathExtension:#"jpg"];
NSLog(#"fileURL: %#", [fileURL path]);
In the end I added the files into Core data, as they were referenced from Core Data objects.
This is a new change in iOS8 for safety enhancement, so you never should keep absolute path.
You save a file name instead of file URL. Each time when you use file URL, you make file URL from file name by your computed property.

How to get files into library directory for iphone device testing

I have tested my app on the ios simulator and using the following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dictdir = [documentsDirectory stringByAppendingPathComponent:#"dict.plist"];
NSLog(dict);
I found the path in which to save my file, dict.plist so that it can be used later by the app.
I am now testing on my device and the place where I am intending to store my .plist file is
var/mobile/Applications/......./Documents/freq.plist
First of all, I know that non-user data is meant to be saved in the library folder, so how do I change my code so that the path is leading to the ..../library/ directory?
And how do I actually put my file in this directory? Right now using the 'organizer', I managed to get the file into the app sandbox, but not in a particular folder.
The same code will get the correct path on the device and simulator, that's what it is meant for. However, if you want to copy it, you have to include it in your bundle, or download it from the internet. You can't just copy it with the finder. For copying from the bundle, use the the function – copyItemAtURL:toURL:error:. Downloading is a bit more complicated and is probably better suited for a separate question.
Just change NSDocumentDirectory to NSLibraryDirectory

Resources