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

2019-01-11 02:12发布

I want to use my webrtc .a static library in swift. Can you help please? I read you can´t use static libraries in swift, is that true?

3条回答
别忘想泡老子
2楼-- · 2019-01-11 02:23

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:

/Users/raniys/Desktop/Screen Shot 2016-03-14 at 5.27.20 PM.png

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

enter image description here

  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")

enter image description here

*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:

enter image description here

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":

enter image description here

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

enter image description here

Then all done, you can use the functions of the static libraries now. enter image description here

查看更多
欢心
3楼-- · 2019-01-11 02:23

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

查看更多
爷的心禁止访问
4楼-- · 2019-01-11 02:24

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

enter image description here

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.

enter image description here

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.

查看更多
登录 后发表回答