How Do I Create a Development Framework In iOS Including Swift?
My goal in this was to create an iOS framework that incorporates both Swift and Objective-C that I could use in my development projects. The nature of this framework is that the framework itself is undergoing development. So, it was important that each time I build a project using this framework (I’ll call projects using the framework “using” projects for lack of a better term), I wanted to make sure the framework itself was re-built. I wanted this to be a framework because I have a few using apps across which I want to use the same framework code. I have struggled with this for a good hunk of today, and wasted a lot of time on something that should have been, in my thoughts at least, easier. So, I’ll share my process.
- Link Binary with libraries VS Embed Frameworks
- What exactly is a Private API, and why will Apple reject an iOS App if one is used?
- iOS : Could not build module <framework-name>
- dyld: Symbol not found: _NSURLAuthenticationMethodClientCertificate when trying to run iOS app
- Create and import swift framework
- Download and execute iOS code dynamically at runtime
Solutions Collect From Internet About “How Do I Create a Development Framework In iOS Including Swift?”
The first thing to observe (which was certainly not my first observation!) is that you cannot do this using a static library under iOS. Xcode will not let you use Swift in a static framework Try it. Xcode will deny your wishes!
Here’s the process I ended up with. The two main issues I had to deal with were: (i) making Xcode link to the framework in the using project without errors, and (ii) getting access to the headers of the framework in the using project. In Apple’s enlightened view these two issues are separate. Note the sarcasm. ;).
1) Create a
Cocoa Touch Framework using Xcode. I believe this works with Xcode6 and Xcode7. Use:
File > New > Project > iOS > Framework & Library > Cocoa Touch
I happen to be using Xcode7. (Do not make a
Cocoa Touch Static Library— like I said above, Xcode will not let you incorporate Swift into static libraries).
2) With your Swift classes, make sure the members and functions are public. I’ve not experimented with this, but it seems that the public attribute is necessary for the members and functions to be visible to users of the framework.
3) Add what ever Swift classes (and Objective-C) you want to your framework.
4) Close that framework project. (The same project can’t be open twice in Xcode, and you need to incorporate the framework into your using project next).
5) Open your using project in Xcode. For me this was an existing universal app project. You may be creating a new using project. In any event, drag the .xcodeproj file of your framework project, in the Finder, into your using project.
6) Inside of your using project, open your framework project. And drag the framework file into
Embed Frameworks in Build Phases (the
Embed Frameworks section wasn’t present in Build Phases when I first started my experiments and I don’t know yet what magic caused it to appear!).
These steps so far should enable you to build and link without actually yet integrating the usage of your library code.
(I was using https://github.com/RadiusNetworks/swift-framework-example for some of my testing).
7) Now for the coup de grace: Under
Build Settings, search for
Framework Search Paths. And add in:
(It seems you do not have to have this marked as recursive).
8) In your Swift code files using the framework, you need to add an import at the top of each file:
You should now be able build and link using your new library!
9) One more gotcha: Make sure your
Deployment Target for your framework matches your destination project. E.g., if your using project builds for iOS7, make sure your framework builds for iOS7 or earlier.
10) Second gotcha (10/23/15): I just learned that it is necessary for my framework to have “App-Swift.h” (the name I use for this) as the Objective-C Generated Interface Header name in Build Settings. When I took this (Objective-C Generated Interface Header) out (trying to fix another issue), I get serveral interesting issues coming up in App-Swift.h. These issues look something like:
"Cannot find interface declaration for NSObject"?
11) Third gotcha (10/29/15): When I tried to upload my first app to iTunes Connect that makes use of this Framework, I got an uploading error. The error read:
ERROR ITMS-90206: “Invalid Bundle. The bundle at
‘Your.app/Frameworks/YourFramework.framework’ contains disallowed file
Various SO and other posts have run into this kind of error, and the trick for me was, for the Framework target, in Build Settings, to set “Embedded Content Contains Swift Code” to NO. (My app Build Settings had this flag set to NO already).
An example project with most of these steps completed is on https://github.com/crspybits/CocoaTouchFramework.git
- swift – UILabel text not renderer when using renderInContext asynchronously
- Swift iOS google Map, path to coordinate
- Your iOS Development certificate has been revoked
- “iOS 6.0 Simulator” not visible/installable in Xcode 5.0.2
- How to achieve a playback rate of 4.0 with AVPlayer in iOS?
- Upgraded to iOS 7 beta / Xcode 5 beta and receiving warning “CODE_SIGN_ENTITLEMENTS specified without a valid Developer Signing Identity for iOS”
- mp3 audio playback not working with Cordova 3.5 on iOS
- Adding an accessory view button to the MKMapView call out annotation?
- How to add playable(such as wav,wmv) header with PCM data/buffer in iOS?
- Union of two array of two different Objects
- Existing iOS form framework
- How to pop from one view controller to another view controller
- Swift: binary operator '==' cannot be applied to operands of type “protocol”
- What does @NSManaged do?
- How to add small red dot in UITabBarItem