Developing iOS Application Features as Modules in Xcode


Segregating the features in our application is very fruitful for maintaining the application as our application keeps on growing. It helps us in rapid development of new features.

Note: We are going to be using cocoapods for creating modules.

  1. Create a new project or open your existing project in Xcode.
  2. Open Terminal in root directory of your project.
  3. Type “pod init” in terminal and hit Enter.
  4. Now let’s add Login Module in our application. Create a directory named “Modules” and execute “cd Modules” in terminal.
  5. Now being in Modules directory execute “pod lib create Login” in terminal. We’ll be asked with number of questions to proceed.

6. Xcode will automatically open the Login Project. Don’t close the project. We are going to do some changes to this project.

  • Delete Tests Group from Project Navigator and Login_Tests Target.
Delete Tests Group
Delete Login_Tests Target
  • Change Swift Language Version. Login_Example Target > Build Settings > Swift Language Version.
Update Swift Language Version
  • Build the Project. We’ll get an error in AppDelegate.swift. Fix the error.
Fixing error in AppDelegate.swift

7. Go to Login Project from Finder. Make the hidden files visible if not. The shortcut for this is Command + Shift + .

8. Delete these directory and files.

  • _Pods.xcodeproj
  • .git
  • .gitignore
  • .travis.yml

The folder structure after looks like below:

8. Go inside Example directory and delete others except:

9. Now go back and open Podfile of your main project. Edit the Podfile to add Login Module.

10. Perform pod install then open ModularProject.xcworkspace. Open your project navigator it should look like this. Build the project.

Here we can see that we’ve new Login-Example scheme that we can run. We can now individually run these schemes when working on specific module/feature. We won’t have to compile the whole main project.

11. Now if you check on the project navigator inside Pods > Development Pods you’ll see something like:

There is a ReplaceMe.swift file there. You can now create a LoginViewController.swift file right clicking on Login Group or rename the ReplaceMe.swift.

12. Now from our main project in ViewController.swift we are going to open LoginViewController.swift that we created in Login Module. I’ve created a Login Button and linked its outlet in ViewController.swift which we’ll use to open LoginViewController.swift. The ViewController.swift class will look like below:

  • We’ve imported Login Module and added code to push LoginViewController on login button tap.
  • The LoginViewController.swift inside the Login Module should be public class otherwise we won’t be able to access the class outside the module.

13. We can also create a Core Module that every other module uses where we can add common classes, views, extensions, assets etc. For that we have to add that Core module name in .podspec file that every module has.

14. That’s it. We’ve successfully added features as modules in our project.

Feel free to show your support. Here is the github link for the demo project to checkout the implementation.

iOS Developer.