How can I use an .a static library in swift?

↘锁芯ラ 提交于 2019-11-26 20:14:07

Have you fixed this problem you asked? I meet this problem on today too, and I've fixed it just a moment. If you don't have fixed this problem, you can try the below steps:

p.s.: the 2 projects are in the same workspace (the static lib project and the App project), the static lib project is build before the app project. The workspace structure as the pic shows:

  1. In the static lib project, all the .h files are need add to "Build Phases/Copy Files":

  1. Drag the static lib product file (the .a file) to the app project, see the pic:

(path: "app project/Build Phases/Link Binary With Libraries")

*if you care about the red color mark for the .a file, you just need choose the "generic iOS device" as the build device to re-build the static lib project, and re-drag the .a to the app project(the red one can be delete)

  1. Set the "Library Search Path" in you app project:

this is the .a file path which the project built in: $(USER_LIBRARY_DIR)/Developer/Xcode/DerivedData/StockApp-fkjqyvsniiauxogkivalcduqeotj/Build/Products/Debug-iphoneos

  1. Create Bridging-Header file for your app project, and import the static lib in it, in my case I include "StaticLib/StaticLib.h" and "CommonFoundation/CommonFoundation.h":

  1. Add the Bridging-Header file path to "Objective-C Bridging Header" of you app project:

Then all done, you can use the functions of the static libraries now.

Using Objective-C Classes in Swift

If you are going to import code within an App Target (Mixing Swift and Objective-C in one project) you should use bridging header file to expose Objective-C code to Swift code. Read more here

In this post I will describe how to import Objective-C static library to Swift code

Swift consumer -> Objective-C static library

Xcode version 10.2.1

Create an Objective-C static library or Create Objective-C static library Target

Create a library project

File -> New -> Project... -> Cocoa Touch Static Library -> Enter necessary information and choose Language -> Objective-C

Create module.modulemap file

module module_name {
    umbrella header "module_name-umbrella.h"
    export *
}

Add .h files to module_name-umbrella.h that will be open for consumer

#import "header_1.h"
#import "header_2.h"

Add Copy Files Build Phase

Project editor -> select a target -> Build Phases -> Copy Files -> add `module.modulemap`, `module_name-umbrella.h` 

Add Headers

Project editor -> select a target -> Build Phases -> Headers(If it doesn't exist -> + at the top -> New Headers Phase) -> add all `.h` files from `module_name-umbrella.h`(`header_1.h`, `header_2.h`)

Build library

Note: Be sure that you build library for the same process architecture as the client code.

Find generated output

Products group -> lib<module_name>.a -> Show in Finder

Note: By default it will be located in subfolder of DerivedData folder (it can be changed if you delete DerivedData during development process and rebuild it again).

The directory includes

  • lib<module_name>.a – built static library
  • module.modulemap file
  • module_name-umbrella.h file
  • usr/local/include folder that includes .h files from module_name-umbrella.h - public interfaces/definitions

Using Objective-C static library

Link Binary With Libraries

Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib<module_name>.a` file

Add Library Search paths

Project editor -> select a target -> Build Settings -> Search Paths -> Library Search paths -> add path to the parent of `lib<module_name>.a` file

Add Header Search Paths

Project editor -> select a target -> Build Settings -> Search Paths -> Header Search Paths -> add path to generated `include` folder (or a path to the parent of generated `module_name` folder with `.h` files)
  • When you set a path you can define a path to one of parent folder and set recursive(/** will be added to the end of the path). For example you can defile a path to Build directory with recursive parameter.
  • If the patch contains spaces - , you should escape them using \ or enclose the path in double quotes ""

Import module to the Swift client code

import module_name

About Library

Swift consumer -> Swift static library

Objective-C consumer -> Swift static library

Objective-C consumer -> Objective-C static library

Yes, you can use static libraries in Swift. Go to your Build Phases and under "Link Binary With Libraries" and add them there.

Alternatively, you can go under Build Settings and in "Search Paths" append the "Library Search Paths" value to include the path to the folder that your .a file is in.

You can add headers for your library the same way under the "Header Search Paths"

Also keep in mind that if this library is written in Objective-C, you will need a Bridging Header to use it in Swift.

With Xcode 9 using static libraries supported with Swift. You can use like using in Objective-C. Xcode Release Notes

Xcode supports static library targets which contain Swift code. Debugging applications that use Swift static libraries may require a complete set of build artifacts that are in their original location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!