Read and write files in development machine - ios - ios

Is there a way to write in my local (not simulator) filesystem from an ios app developed with xcode, when I am running the app with Xcode's simulator ?
Reason: I have an ios application and I would like to have some content that the user could fetch as JSON data from my server. However, I would also like this JSON data to be generated from my (development) version, locally (so that I am sure it is exactly in the right format). I would therefore like my application to read and write in something like ~/User/myself/my_website/my_data/ rather than in the simulator's folder. Obviously this code would only be active in the development version and not in the release version (it doesn't need to be app review compatible).

Well, turns out it was trivial, and much simpler than writing on the device. Just specify a path to write to in the form of an absolute path and do [stringData writeToFile:path ...].

Related

iOS - Differences over application main bundle path

It's really easy to retrieve main bundle's path for an app:
let path = NSBundle.mainBundle().bundlePath;
On a device running iOS 9, the result maybe something like:
/var/containers/Bundle/Application/52E76B99-BF75-4E99-82BB-51307695E694/AppName.app
The result shows that application installed via Xcode is stored under /var/containers/Bundle/Application on iOS 9.
But in some other devices, the main bundle's path is something like:
/private/var/mobile/Containers/Bundle/Application/0E6DD32F-4E6F-4E54-B47E-A91060097E16/myapp.app
That's weird. That two paths differ a lot. I wonder what may produce that differences?
You should never use the path if you're storing it for later use or comparison. As it will always change and generated run time by iOS but its iOS responsibility to get you the right path when you ask for the bundle path. So you should always use a path which is created run time.
Good to read.

Pass app to Xcode

I've been running a Xcode project on my iPhone but I haven't upload the binary yet. The thing is that I've lost the code on my Mac (that's what happens when you don't commit con GitHub) and the only thing I still have from the project code is the "app" that the iPhone saved when I run it for the first time. Is there any way to pass the code from the iPhone or see the code?
No. Your code was compiled, linked and packaged to produce the application, and it is the resulting compiled binary that has been installed on your phone.
You may be able to retrieve resources (storyboards, XIBs, images, plists...). For the code, there may be decompilers (not sure if there are any for Objective-C or Swift, though), but they will not reproduce your original code, just code that compiles to the same thing (i.e. without comments, with arbitrary names for local and instance variables, etc.).
Don't you have a Time Machine backup of your project?

Data is not getting from sqlite DB in device but it is working in iOS Simulator

In my app I want to display data from sqlite database on UIPicker View.It is working fine in ios simulator but data is not fetched when using iphone device.I m not getting the issue.How it can be solved???
Are you sure there is sqlite file available at path? sqlite3_open() will create the file if its not exists. Please check wheather you have created sqlite file in bundle, if yes you cannot do that. The file actually has to already exist and be copied into the bundle as a resource at build time, then used as a template to copy to a writeable area like the documents folder. The best way, if you intend to create a writeable database, would be to get the path to the Documents directory and use that. You can and should do that unconditionally, without regard to whether you are running on the Simulator or not. Indeed, I would suggest not creating any more differences between Simulator behavior and device behavior than you face anyway.

Is application bundle fully replaced on upgrade from App Store?

The following use case:
create a strings file (not localized) and deploy the application to your test device
localize the file (en.lproj or similar), make some changes to the new file and remove the original file
deploy the application on the device
What I noticed that very often (always?) the application will still use the old non-localized file. The reason is that apparently the file is still in somewhere the application bundle, even though it has been removed from the project. iOS's logic is that if a file of a specific type is looked up, it first checks in the root of the bundle for the non-localized version, and only if it doesn't find it does it go deeper into the localized folders. (Is it just me, or is this logic kind of backwards? I'm used to first looking for language specific file, and then falling back to some defaults, but it might be my Java background.)
Sometimes (always?) removing the app from the device completely doesn't help either. What does in this case is a CMD+SHIFT+K, which cleans the build folder, and after the app is built and deployed again, the correct file is used.
A worse problem would be if such a thing leads to an app crash, like some strange issues with Nibs or whatever - I have seen those as well...
The question is: what would happen if the first version of my app in the apple store had the non-localized file, and the second version localized it? When the user upgrades the app, would they get the same behavior as me during development (i.e. outdated, non-localized file used)?
I can't imagine because I think this would be a cause of a lot of grieving and bug requests, but I can't tell for sure as I haven't yet published any iOS app.
If the behavior is correct when upgrading from the store, why is it? What is so different? Are the files stored locally on my computer somewhere and used when I deploy?
Can anybody share their experiences?

iOS 6 local storage file appears twice

I have an app that uses local storage (using sencha 'Ext.data.proxy.LocalStorage') to view some content offline. I was rejected by itunes for violating the iOS Data Storage Guidelines.
I thought it might be related to the localStorage being inside the /Documents folder in iOS before iOS 5.1. To confirm that in newer versions the document folder is empty I checked, but I still found a .file__0.localstorage.
The strange think is that the same file (but without the leading dot) also exists in the library/caches folder, where it should be.
Both have the same content.
Does anyone know how this can happen? I already asked in the Sencha Support but it does not seem to be related to the Sencha framework.
Note: I am not using PhoneGap, only Sencha.
Making a very small Hello World Application with Sencha that writes to local storage I could reproduce this behavior. It seems like on application exit, it will write a copy of the localstorage file to the /Documents folder.
This seemed suspicious to me and it reminded me of the PhoneGap patch for iOS 5.1 that backs up the localStorage file also.
With iOS 5.1 Apple started to put the localStorage file in the Library/Caches folder which broke many applications because the data was no longer reliable stored. So PhoneGap and others started to implement Workarrounds that backup the localStorage file to the documents folder and automatically restore it if needed.
Trying to find proof that Sencha does something similar, I opened up the resulting .app package and found a stbuild_template file that contains the native wrapper code. It is of course compiled, but searching for the string "localStorage" results in some interesting results like:
restoreLocalStorage
preserveLocalStorage
Could not remove source file while backing up localstorage Could not copy localstorage backup . Caches WebKit/LocalStorage .file__0.localstorage file__0.localstorage
SNLocalStoreageFix
Also I found a lot of references to NimbleKit which seems to be used internally for the packaging.
This thread also hints at a solution for the iOS 5.1 local storage problem in Sencha Touch 2.1 which is what the OP is using: http://www.sencha.com/forum/showthread.php?194674-Localstorage-and-native-iOS-5.1-apps&s=04149e771f9c4eea15cb6f6d97069ff6
All this is evidence for me that Sencha implemented a workarround for iOS 5.1 which will put the localStorage in the /Documents folder. Since you only use the localStorage file for caching, Apple rejected you because you store non-user data in /Documents.
I think using PhoneGap there is an option to disable this backup. So you might want to consider using this or ask Sencha if they have a similar option to disable it.

Resources