koen
koen

Reputation: 5729

Two Xcode projects, same target

I am working on version 2 of an iOS app. Since I am rewriting it in Swift, I started a new Xcode project. Even though the target names are the same, I get two different apps on my device or simulator, so they don't share data and preferences, etc.

Is it possible to have two different projects share the same target? Or should I brute-force replace everything (after a triple backup :) in the original project folder?

Upvotes: 0

Views: 1500

Answers (2)

koen
koen

Reputation: 5729

I spend a couple of days and many tries to get this to work, and am outlining below the steps I took. Hopefully this can be useful to others as well.

Background: To learn Swift and also modernize my app, I decided to completely re-write it, using all the goodies of Swift and the newer iOS APIs. Instead of slowly converting one class from Objective C to Swift, as it is described in many tutorials, I just started from scratch. So I made a new project, with the same name of the target, added some basic functionality, and ran it on the simulator. To my surprise, or maybe not, a new app was created, so I had two apps, one written in Objective C, and the new one in Swift. Since my app uses Core Data, they need to be the same, otherwise the user will lose all his/her data when upgrading. So with a lot of trial and error I was finally able to get this to work, here are the steps I used:

Disclaimer: use at your own risk - make sure to have backups so you can always revert.

  1. Make sure both projects have git enabled.
  2. Make backups for both projects. If you are syncing with Dropbox or something similar, turn that off.
  3. In the ObjC project repository, create a 'Swift' branch.
  4. In Xcode, import all source files form the Swift project to the ObjC project (on the Swift branch). For easier housekeeping, I recommend to keep everything in separate groups inside Xcode.
  5. Under 'Build Phases' in the ObjC project (Swift branch), remove all ObjC related source files and linked frameworks.
  6. Build and run.
  7. You may have some project related errors and will need to fix that. For instance in my case Xcode couldn't find the info.plist file and I had to remove a build script. YMMV.
  8. Commit the changes on git (still on the Swift branch).
  9. Switch between the Swift and ObjC branch and make sure that there is indeed only one build on the simulator and that they share data (or whatever is specific for your project).

Optional if you want to have two different projects:

  1. Switch to the Swift branch and quit Xcode.
  2. Make a copy of the whole project folder and name this eg MyApp_Swift, but don't rename the target and pbxproj file.
  3. Open the original project, and switch back to the ObjC branch, run and test.
  4. Open the copied project, and switch to the Swift branch, run and test.
  5. Delete (to trash) all ObjC related files from the new Swift project, run and test.
  6. Quit Xcode, and move the .git folder from the original Swift project folder to the new Swift project folder. I did this in the Terminal.
  7. Open the new Swift project, run and test. If everything is ok, commit the changes.
  8. Switch back to the ObjC branch of the original project, run and test. If everything is ok, you can delete the Swift branch in git.
  9. You should now have two different projects, both use the same app on the simulator.

Upvotes: 0

Nik
Nik

Reputation: 9431

No, it's not possible. Better update original project.

Upvotes: 1

Related Questions