Tuesday 19 March 2019

Add framework to main Xcode project with Podfile managing dependencies

1. create a main project


2. go to the root folder of MainProject, run 'pod init' 'pod install',  leave the Podfile for now.


3. Xcode -> File -> New -> Project, choose Cocoa Touch Framework and name it, I will call it NetworkingFramework



The framework can be save in any location, in this example I put it in Frameworks folder under the root folder.



4. link MainProject with NetworkingFramework

4.1 create a new group in MainProject
4.2 Xcode->File->Add files to 'MainProject', choose to add the NetworkingFramework.xcodeproj
4.3 go to the MainProject General tab, add NetworkFramwork.framework into Embedded Binaries
 
5. now try add any public class and public functions in NetworkingFramework


6. try import NetworkingFramework in MainProject and invoke any public function in NetworkFramework. 



Build and run. If you followed the above step, it should succeed. If anything is broken: try clean -> completely quit Xcode -> open Xcode and rebuild


7.  edit the Podfile and only add Alamofire in NetworkFramework because we don't want to expose and library dependencies to MainProject, the MainProject doesn't need to know the implementation and only need to talk the wrap layer- NetworkService in NetworkingFramework.



8. pod install.  Now it will build when add import Alamofire in NetworkService file. However, the app will crash at runtime.

One solution to solve this: grab the Alamofire.framework to NetworkingFrame folder and add it to Copy Files in Build Settings tab.





No comments:

Post a Comment