I\'m new in swift programming. I need to create pure swift framework and import it in my existing pure swift project. When I try to import swift framework, I\'m getting this
On Swift:
Create Framework:-
Start Xcode -> Create a new Xcode Project -> iOS -> Framework & Library -> Cocoa Touch Framework -> Name the framework(ex. sampleCocoaFramework) -> Create.
Set Taget -> General -> Deployment info -> Deployment Target.
Add a public class: File -> New File -> iOS -> Swift File -> Name it (ex. openCocoaClass) -> Create.
Now add some code to the openCocoaClass.swift.
import Foundation
public class openCocoaClass {
public init() {
}
public var samplePublicVariable = "samplePublicVariable @ openCocoaClass"
public func samplePublicFunction()
{
print("samplePublicFunction @ openCocoaClass")
}
}
Clean the project : Product -> Clean
Configure the scheme settings : Product -> Scheme -> Edit Scheme -> Run -> Build Configuration -> Release.
Build the framework : Product -> Build.
Adding Framework to Project:-
Start a Xcode project and name it (ex. CocoaFrameworkTest).
Drag and drop the sampleCocoaFramework.framework to the CocoaFrameworkTest's project folder.
Target -> General -> Embed Binaries -> Add Other -> Select Framework -> Copy Items if needed -> Done.
Accessing Framework on ViewController:-
import UIKit
import sampleCocoaFramework
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let frameworkObject = openCocoaClass.init()
frameworkObject.samplePublicFunction()
print(frameworkObject.samplePublicVariable)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
In Xcode 7.2 using Swift 2.1 I managed to get past the mentioned error by making sure that the build settings of your pure Swift framework called Foo are as follows:
1) Build Settings->Packaging->Defines Module = Yes
2) Build Settings->Swift Compiler - Code Generation->Install Objective-C Compatibility Header = Yes (if you do not need to import your Swift framework into Objective C set this setting to No)
3) Erase the string value of Build Settings->Swift Compiler - Code Generation->Objective-C Bridging Header
4)Build Settings->Swift Compiler - Code Generation->Objective-C Generated Interface Header Name = Foo-Swift.h (if you do not need to import your Swift framework into Objective C erase this setting as you did in step 3))
5) Make sure that Build Settings->Packaging->Product Name = Foo
6) Add to the public umbrella header of your framework (Foo.h), which you can use to import your Swift code to Objective C, the following line:
#import "Foo-Swift.h"
(but if you do not need to import your Swift code into objective C skip this step)
6) Add the following line to the Swift file where you want to use the module Foo:
import Foo
Important notes:
1) Ensure that your umbrella header Foo.h is set to public in the File Inspector, otherwise this won't work.
2) If you are using your pure Swift framework in Objective C files, ensure that:
the Foo-Swift.h header has generated the correct interface by going to Foo.h and then clicking on the top left corner menu of the Xcode code editor and then choosing Includes->Foo-Swift.h (I had to rebuild the Foo framework a few times until Xcode caught-up with the change and then generated the correct interface in Objective C, so you may need to do that as well)
your swift classes inherit from NSObject or they won't be available to Objective C code
Additionally for @jvarela's answer, in Xcode 9 and swift 4,
To import Swift framework in an Objective C project, you must add
@objc
mark to all of your classes, methods and variables which you want to export into Objective C project .
For example,
@objc public class Foo: NSObject {
@objc public var fooDescription: String
@objc public func foo(){
}
}
I've done with the following steps.
Cocoa Touch Framework
to be selected)public func foo()
, to it.Single View Application
)Project Navigator
.Target Dependencies
in the app's Build Phases
setting.Embedded Binaries
in the app's General
setting.Now you can build like this code in the use-side app.
import FooKit
func bar() {
foo()
}