Below, you will create a swift project and then introduce 3 libraries:
- Snappy simplifying the library of AutoLayout code
- Alamofire HTTP network library, written by afnetworking authors
- Sdwebimage picture loading, OBJC project, via header file bridge mode for Swift use
Among them, the first two are Swift's library, SDWebImage
is OBJC's library.
Source code See: Https://github.com/MarshalW/SwiftWith3rdLibDemo
The project runs like this:
Before you do the following steps, you need to make your project a git project. Using the Snappy Library
Snappy Library, which simplifies the code for AutoLayout layouts.
Unlike the OBJC project, the SWIFT project does not have a cocoapods-like library dependency tool, but instead uses Git's submodule to lead the repository. It is now more convenient to use. 1. Create Submodule
Execute in the current project directory:
1
git submodule add https://github.com/Masonry/Snappy.git
You will see an extra snappy directory when you finish executing.
Such items, if Git is shared with other people, they need to be in addition to the clone
project:
1
git submodule update--init--recursive
2. Add Snappy.xcodeproj to the Xcode project
As shown, drag the file directly to the past:
3. Set the Snappy.framework to the project
Xcode Open the Project General
, click +
Then, choose Snappy.framework
, plus it.
4. Writing code tests
If normal import
, Xcode does not error, it should be no problem:
1
Import Snappy
Writing a simple code to run is no problem:
Override func Viewdidappear (Animated:bool) {
println ("Super View: \ (Imageview.superview)")
Let padding = uiedgeinsetsmake (100, 100, 0, 0)
imageview.snp_makeconstraints {Make in//autolayout
Make.edges.equalTo (self.imageview.superview!). With.insets (padding)
Return
}
}
Using the Sdwebimage Library
The Sdwebimage library was written by OBJC. The approach introduced is different from Swift (starting with the 3rd step). 1. Create Submodule
This step is still not saved:
1
git submodule add https://github.com/rs/SDWebImage.git
2. Add Sdwebimage.xcodeproj to the Xcode project
3. Set the target Dependencies
Over here:
Join:
It looks like this:
4. Link Binaray with Libraries
Find here:
Join Imageio.framework (iOS comes with), and Libsdwebimage.a
5. Set other Linker Flags
Here to set -ObjC
The setup is done:
6. Create a bridge header file
is to create an ordinary header file, which is generally named 项目名-Bridge-Header.h
7. Set up the bridge header file
Find here:
Double-click the arrow labeled Place, will pop up the text box, the header file can be added.
I started by referring to the StackOverflow answer, the header file directly dragged in, found to use the absolute path. Can be modified after dragging:
Frequently asked questions about introducing third-party libraries
The main problem now is that each swift third-party library Deployment Target
version is inconsistent. For example Alamofire
, 8.0, and Snappy
7.0, need to make their versions consistent.
There will be an error like this:
1
Ld:embedded Dylibs/frameworks is only supported on IOS 8.0 and later ...
How swift projects introduce third-party libraries