How to Add Frameworks into the Swift Project

How to import framework into XCode project?

That's not a Swift module, so first, you need to create a bridging header, in which you import <BRLMPrinterKit/BRLMPrinterKit.h>, then you can use the BRLM classes in your Swift files.

Add Swift Package to a custom framework

Well, there are numerous isses you could have faced during importing framework itself. It also depends if you use framework as binary or source code. I assume you were using source code approach as you are the creator of framework. You can however check all approaches here: in this SO question . Lets look at all the steps you need to implement in order to successfully use framework with SPM dependencies in your swift project.

  1. create SPM properly and also link all additional SPM dependencies tutorial here. Make sure all your classes, structs etc. and their coresponding initializer has correct access level property. If you plan to use them outside of the package, use public initializers..
    2)Once you created you SPM package, link it to framework. For the sake of this answer I created testFramework and linked one of my custom SPM package called VodApiPackage . This package also contains dependency to another BaseTvApiServicePackage.

screenshot1

I also added TestPrinter file containing simple function for creating error declared in my SPM package. This function servers only for checking that everything is working properly and will be user later. It is also declared public.

import Foundation
import VodApiPackage

public struct TestPrinter {
public init () {}

public func makeTest() {
let x = VodApiError.customErr(msg: "testMsg")
print(x.localizedDescription)
}

}


  1. Open your project and make link to framework, you can also check this nice tutorial. The most important step from tutorial is step 5 and 6. Where you drag .xcproj into your project and link libraries and framework

  2. make sure your library and SPM dependencies are correctly linked in your project. Check sample project below.

screenshot1


  1. Build and test using your framework and its packages:

    import UIKit
    import testFramework

    class ViewController: UIViewController {

    override func viewDidLoad() {
    super.viewDidLoad()
    testmodel()

    TestPrinter().makeTest()
    }
    }

How can I add existing frameworks in Xcode 4?

As per Apple's documentation:

  1. In the project navigator, select
    your project.
  2. Select your target.
  3. Select the "Build Phases" tab.
  4. Open "Link Binaries With Libraries"
    expander.
  5. Click the + button.
  6. Select your framework.
  7. (optional) Drag and drop the added
    framework to the "Frameworks" group.

Create a framework from existing iOS project

When you want to make a framework, or module, from a project, the first thing you’ll need to do is, if you don’t have one already, make an Xcode project workspace (File > Save as Workspace) . The next step is to add a new framework “project” to your workspace (that could’ve been why you saw some resources telling you to make a new project) with File > New Project and choosing Cocoa Touch framework. When you add it, make sure you pick your workspace under both Add to and Group. Then you will need to migrate in the files that you wanted to be in this module — make sure to choose copy items if needed.
Here’s an article with more specific details on the process if you need it:
https://medium.com/kinandcartacreated/modular-ios-splitting-a-workspace-into-modules-331293f1090

Import Framework in Swift Project, Xcode

If I get you correctly you don't have a separate build target for your framework (you already built it with Xcode 5) and included the framework into your project's build target.

The part of the documentation you're referring to is about frameworks within different targets.
Since your framework is in the project's target this part of the documentation doesn't apply here.

In your case you can't do an import of the framework in your Swift file. That's why you get the error message "No such module myFramework".
myFramework is no module -- it is part of your project's module (which is by default determined by your product name). As such the classes in your framework should be accessible.

However your framework is written in Objective-C. So what you have to do is to import the Swift facing classes in your bridging-header as described here.

Please note that this has nothing to do with the Swift import of a module. The import directive in the bridging-header file just notifies the compiler to 'translate' Objective-C header files to Swift syntax and makes the public header visible to Swift.

So what should you do now?

  • First import the header files you're interested in in the bridging-header. You only need to import the headers you will interact with in Swift.

  • Try to compile your project in this stage. If Xcode can't find the header files of the framework your problem is probably not related to Swift or Xcode 6 but a problem with including frameworks in general.

  • After that try to instantiate a class you imported in the bridging-header, maybe in your AppDelegate.swift. Xcode auto-completion should offer you the type names.

Hope this helps.



Related Topics



Leave a reply



Submit