Hi friends, in this blog we will discuss about how multiple projects are handled in iOS.
🎯 Challenges which we face with single project.
- Consider a situation that you have one application that will be published in various countries with some different features but also it have some same features. Instead of keeping duplicate work, you can work similar feature as framework and you can use it in your app.
- One more use case is :- If you’re working in a big team / your app is very big, it’s better to keep separate project for each work. It helps for CI/CD integration to alert particular team about issues. I’m explain more about this in my next blog.
How can you create a multiple project in one Xcode project?
first we will look what you mean by workspace and xcodeproj
- An Xcode workspace always exists in your project. It may be external to an .xcodeproj, or embedded within one.
- A workspace is just a list of contained projects. there is no other metadata in it.
- Xcode workspaces are directories with the .xcworkspace extension that is present as packages
Workspaceis responsible for dependencies between projects
- Each workspace contains bellow 3 files. The important file is
- like workspace, an Xcode project is a bundle containing one or more files.
- The most important and only required file is the
Projectis responsible for the source code.
Steps involved in adding multiple project:-
- Create Simple xcode project as follows.
- Create xcode project by Select File -> New -> project -> app -> give name for the project(e.g Test) -> select destination to save project
- Now Your app folder look like this
2. To add framework follow bellow steps.
- Open your xcode project where you need to add -> Select File -> New -> project -> Framework-> give name for the framework(e.g NextScreen) -> select your project where you need to add project under
- Now your project look like this which has one
Testxcodeproject and a
3. Create plugin file which acts as interface between the app and framework
swift filewhich acts as plugin between the framework and project :- Select File -> New -> Swift File -> name the file(e.g NextScreenPlugin) and save under framework folder.
iosNote: class and method used in plugin file which need to be accessed from different module should have open/public access specifier. To know about it click here
4. Add your framework as dependency to your app
- Select your project -> Select Build Phase -> Click on
+button under Dependencies. -> add your framework there(I’m using NextScreen as framework)
- Expanded project folder looks like below
4. Create a view controller to test
- In this Example i’ve a viewcontroller with button in Test app. Tapping on that button will take user to next screen which is present in my framework
- Add code for pushing view controller in NextScreenPlugin as
- Import framework into the app and call required method
- Now run your code. your app look as bellow
How to use xcworkspace for adding multiple project:-
- Create a folder where you want to store your project.
- Select File -> New -> Workspace.
3. Add name and store the workspace. It creates empty workspace for you. Now your app folder look like this
4. Select File -> New -> this time instead of selecting workspace select project -> select app -> give project name (e.g WorkSpaceTest)-> select workspace where you need to add project under
5. Now your project is added to the workspace and you check the location of the project in
contents.xcworkspacedata . Now your
contents.xcworkspacedata and project folder look like this after adding.
6. follow same steps above for adding multiple projects into xcodeproject as explained above.
Enjoy your coding. I hope you learnt something from this blog. Please hit the clap button below 👏 to help others find it!. follow me on Medium.