Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

ios - How to integrate Cocoapods with a Swift project?

As Apple introduced Swift, their new programming language, I wonder how you can integrate it with existing Objective-C libraries that are available via CocoaPods?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Cocoapods 0.36 and above introduces the use_frameworks! instruction which implies that the bridging header is not required for importing Objective-C pods in Swift.

Please find below a full example using MBProgressHUD and Alamofire:

1. Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.3'
use_frameworks!

pod 'Alamofire', '>= 1.2.2' # Swift pod
pod 'MBProgressHUD', '>= 0.9.1' # Objective-C pod

2. Deletion

Remove the #imports from your bridging header or even delete the bridging header file if you do not need it. If you choose the latter possibility, do not forget to delete the path (to this deleted bridging header file) in your Xcode project configuration.

3. Adding imports

Add import MBProgressHUD and/or import Alamofire at the top of every Swift files that need these class(es).

4. Fix the enums if necessary

You're now using bona fide frameworks, so your enums have moved in flight! You might have a line of Swift that was fine with the bridging header like this:

progressHUD.mode = MBProgressHUDModeIndeterminate

That now has to become this:

progressHUD.mode = MBProgressHUDMode.Indeterminate

Not to big a deal, but the pile of errors might lead you astray that you have a bigger problem than you do if you are using a lot of Objective-C enums.

(Source of this answer)

For your information: I guess (you will have to test by yourself to be sure) that the use_frameworks! instruction in your Podfile is only compatible with Xcode projects targeting iOS >= 8.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...