iOS Swift:如何将含Storyboards的App1作为模块嵌入App2并支持修改
Perfect question — embedding a storyboard-heavy app as a modifiable module is a common need, and there's a clean, scalable way to pull this off. This approach lets the App2 team tweak both code and design while keeping your App1's core organized.
Step 1: Refactor App1 into a Framework
First, we'll turn App1 into a reusable framework that bundles its code and storyboards:
- Open your App1 Xcode project, then create a new Cocoa Touch Framework target (File > New > Target > Framework & Library > Cocoa Touch Framework). Name it something like
App1Core. - Drag all the code, view controllers, models, and utilities you want to reuse from App1 into this new framework target. Make sure to check "Copy items if needed" and select the
App1Coretarget when prompted. - Handle Storyboards:
- Add App1's storyboards to the
App1Coretarget's Copy Bundle Resources build phase (select the framework target > Build Phases > Copy Bundle Resources > + > pick your storyboards). - In your framework code, load storyboards using the framework's bundle (not the main app bundle) to avoid conflicts. Here's a Swift example:
// Load App1's main storyboard from the framework bundle let frameworkBundle = Bundle(for: App1MainViewController.self) let app1Storyboard = UIStoryboard(name: "App1Main", bundle: frameworkBundle) let rootVC = app1Storyboard.instantiateInitialViewController() as! App1MainViewController
- Add App1's storyboards to the
- Expose Public APIs: For classes/methods App2 needs to access, mark them as
open(Swift) or add their headers to the framework's Public Headers section (Objective-C). Keep internal logicinternalorprivateto avoid exposing unnecessary code.
Step 2: Integrate the Framework into App2 via Git Submodule
Using Git submodules lets the App2 team modify App1's code while keeping version control separate:
- Navigate to App2's project root in Terminal, then run:
This clones App1 into agit submodule add <Your App1 Git Repository URL> Modules/App1CoreModules/App1Corefolder in App2's repo. - Drag the
App1Core.xcodeprojfile from theModules/App1Corefolder into App2's Xcode project (check "Copy items if needed" is unchecked so changes stay linked to the submodule). - In App2's main target, go to General > Frameworks, Libraries, and Embedded Content, click the + button, select
App1Core.framework, and set its embedding option to Embed & Sign.
Step 3: Enable Edits for App2's Team
Since we're using a Git submodule, the App2 team can modify App1's code and storyboards directly:
- They can open the
App1Core.xcodeprojwithin App2's project, edit storyboards, tweak code, and test changes in real-time alongside App2. - If they want to push changes back to App1's repo, they can commit directly in the
Modules/App1Corefolder and push to App1's remote. If not, local changes stay isolated to their App2 copy. - Pro tip: To make storyboard editing smoother, ensure App2's Xcode project has the framework's storyboards included in its workspace (they should be automatically if you dragged the .xcodeproj correctly).
Alternative: Direct Subproject Integration
If Git submodules feel overkill for your team, you can simply drag App1's entire .xcodeproj into App2's project and link the App1Core framework target. This is simpler but lacks the version control separation of submodules — great for small teams or temporary integrations.
Key Gotchas to Avoid
- Resource Conflicts: Prefix App1's resources (storyboard names, image assets, strings) with a unique identifier (e.g.,
App1_) to prevent clashes with App2's resources. - API Breaking Changes: If the App2 team modifies public APIs in App1, they'll need to update App2's code that calls those APIs to avoid crashes.
- Testing: Always test loading App1's storyboards and functionality in App2 across different iOS versions and device sizes to catch bundle loading issues early.
内容的提问来源于stack exchange,提问作者Mr Abbasi

