Xcode, changing applications subfolder? - iphone

Hi have noticed today whilst writing a simple iPhone app that Xcode sometimes starts a new folder in applications, whilst your still working on the same app.
/Users/Fuzzygoat/Library/Application Support/iPhone Simulator/User/Applications/4E5EF4F0-F410-46A6-888C-0D23BB97D2DC
Does anyone know what causes Xcode to swap to a new app folder (i.e. the one named "4E5EF4F0-F410-46A6-888C-0D23BB97D2DC")
EDIT_001: One thing I have noticed is that I have been doing a lot of quitting an application and restarting to check a set of archive methods, and that does tend to confuse it sometimes. A couple of times it has not found saved data, I guess this is just a side effect of constantly running the simulator over and over via Xcode. Things seem to go just fine if I test on the Simulator without Xcode, for a start the folder keeps the same name.
NB: I am using NSSearchPathForDirectoriesInDomains to get the documents folder each time.
gary

For security reasons, iPhone OS restricts an application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application’s “sandbox.” The sandbox is a set of fine-grained controls limiting an application’s access to files, preferences, network resources, hardware, and so on. In iPhone OS, an application and its data reside in a secure location that no other application can access. When an application is installed, the system computes a unique opaque identifier for the application. Using a root application directory and this identifier, the system constructs a path to the application’s home directory. Thus an application’s home directory could be depicted as having the following structure:
/ApplicationRoot/ApplicationID/
During the installation process, the system creates the application’s home directory and several key subdirectories, configures the application sandbox, and copies the application bundle to the home directory. The use of a unique location for each application and its data simplifies backup-and-restore operations, application updates, and uninstallation. For more information about the application-specific directories created for each application and about application updates and backup-and-restore operations.
see The Application Sandbox

I know it happens when you restart the simulator. At least, that is when I have definitely observed it.
The developer docs tell you that the path can change without warning and to never depend on it. This is part of the iPhone's security system which prevents malicious apps from using hardcoded paths to find and exploit system resources.

Related

Can my app be distributed through the MacOS App Store (struggling with Sandbox)?

I'm new to MacOS app development, and just getting around to hopefully releasing an app. I have two options - the Mac App store or do it through my website.
I would have preferred to do via the App Store but realized it has a "sandbox" requirement, and I'm flummoxed on what I should do.
My app requires heavy file/folder access. Some of the basic requirements are
ability to open and save files
ability to create folders (without prompting, as part of a process run)
ability for a backend process to access those files and run on it (e.g., if I added a markdown file to the app, and then run pandoc on it)
I can live with a user giving access to a base folder (and its subfolders) as kind of the app folder and I can work inside it. But being able to invoke a command-line tool installed on the mac, to run on those files, is paramount.
Can I do this at all? Or do I need to go outside the app store? I do not need access to system files etc and my app does absolutely nothing with any file except the ones within a project folder chosen by the user.
Any pointers to more recent documentation greatly appreciated. Kind of lost.

Core Data DB Location Debug v Release Build

I have been developing my first SWIFT MacOS App and it uses Core Data. I've just done my first release build and found that it still points at the same SQL Lite DB as my debug build. As I didn't not specify it's location I assumed (incorrectly) it would be in a different location for release.
How do I specify the location of the Database for Core Data ? Is there a way to automatically set a different location for Debug and Release ? I want to be able to run release and debug on the same machine.
What I usually do is have separate debug and release versions of the app. Then I can have both installed on the same device at the same time. The persistent store (and other files) use the same file names, but since they're separate apps the data is separate.
You can do this by duplicating the app target in Xcode, and making a few changes. In the list of targets, right-click your app and select "duplicate". Give the new one a similar but slightly different name. For example, maybe add "-dev" to your app's name to indicate it's the development target.
You'll also need to change the bundle ID and display name of the app. The bundle ID tells iOS it's a different app, and the display name makes it obvious which one you're tapping. You can also add a different app icon if you like.
Now you can build two versions of the app and install both on the same device. The dev version can have whatever code you're working on that's maybe still kind of broken, and the release version can be whatever's ready for release or a version downloaded from the App Store.
In iOS, your CoreData database file is usually located in Library/Application Support within the sandboxed App. Pre iOS 10 it used to be in the Documents folder.
On MacOS with sandboxing enabled it's usually located at ~/Library/Containers/<app_bundle_id>/Data/Library/Application Support/<app_target_name>
Depending on your needs, you can choose different locations for each of your persistent stores when you setup the CoreData stack.
However, I don't see any reason to choose different store locations for different Build Configurations. Thats not the purpose for Build Configurations anyway. Your database will end up being exactly the same - it cannot be affected by build settings.
I would also not recommend to use different store locations for any purpose - except perhaps when you perform Unit Tests.
What you can do however, is to set certain environment flags that affect CoreData's behaviour, for example that enables Thread Violation Assertions. You do this in Xcode Schemes - not in the build settings. Note, that a scheme uses a certain Build Configuration, say Release or Debug and that the name for the scheme can be the same which is often confusing.
You should ensure that these helpful assertions that you can set in a Scheme are not enabled when you build a release. ;)

SMLoginItemSetEnabled is causing wrong copy of helper app to be launched

This is similar to this issue:
Error in Sandboxed App, When loading Helper (LoginItems), code signing issue
The error that I'm getting is the same, but installing the app in /Applications doesn't fix the problem because since SMLoginItemSetEnabled only allows you to set an app bundle id and not the actual app location, it always ends up trying to load some other copy of the helper app that is not in /Applications. Theoretically I could delete all copies of the App on my computer and that should fix the problem, but I cannot prevent users from keeping multiple copies of my app on their computer, which would break the launch at login feature.
Is there a way around this?
Bump the version number of the instance of the login item that you want to be launched. If it's the highest version number, the system should prefer it to existing lower versions of the login item.
If multiple applications (for example, several applications from the same company) contain a helper application with the same bundle identifier, only the one with the greatest bundle version number is launched. Any of the applications that contain a copy of the helper application can enable and disable it.
https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLoginItems.html#//apple_ref/doc/uid/10000172i-SW5-SW1

Jailbroken iPhone - root privileges

I would like to make an app that will manage some files on jailbroken devices like SMS, Notes .db, but I have problems with accessing such files. I've also made just to test my skills, similar application like your file manager and I could browse almost whole file system and access everything except some folders in /var/mobile/Library/. I get a list of contents of /var/mobile/Library but I get nothing in subdirectories like /var/mobile/Library/Notes/, /var/mobile/Library/SBSettings/, and btw what is most important in my case: /var/mobile/Library/SMS/ or /Notes/ displays nothing.
Of course when I browse my device by SSH using Fugu I got everything and have access to everything (like chmod i.e.)
I would be great if you please give me some advices or source code what should I do to have, I guess the root's privileges, not only to access those files, but also change their permissions and names.
I know how to make such operations using typically NSFileManager, but not on files from the directories above like /.../SMS/.
I've never developed an application that strictly need jailbroken device to perform the given tasks. I believe I will need some extra libraries? Is it true? Can I use Xcode or there is something else to develop such apps.
I would be very grateful!
Even when jailbroken, applications installed via Xcode or the App Store are still sandboxed. To get read-only access to the entire filesystem, an application has to be installed in /Applications/ instead of /var/mobile/Applications/. To get write access to the entire filesystem, the application would additionally have to be owned by root and be flagged with the setuid mode

Deploying Test Resources in the iPhone Simulator

I am working on an iPhone Application that stores images in the Applications 'Document' folder.
I am currently doing the majority of my testing using the iPhone Simulator. In order to aid development I want to have some test images pre-loaded into the Document folder of the application that my application can access and display.
I have discovered that when running the application in the iPhone simulator you can access the applications file system via
/Users//Library/Application Support/iPhone Simulator/User
The application is then represented by a UID and within that is the Document folder. Placing the images in this folder and they adding their path to my applications database allows me display test data.
The problem is that each time I build and redeploy the application the old application folder is deleted and a new one is created, this means I have to copy the images to the new application folder and update the database.
Is there a way to automatically add test resources to an application when building and deploying? Should I be putting the test data in the bundle instead, and if so is there a way to not include the test data when building the actual release?
Many thanks in advance
Xcode should be smart enough to migrate anything from your 'old' UUID-based folder hierarchy to the 'new' one each time you build. Are you not seeing this move automatically?
Note: you should definitely be using dynamically generated paths here, not hard-coded. Use [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; or [#"~/Documents" stringByExpandingTildeInPath] to get the proper (current) path to your documents folder.