How to Integrate Cocoapods with a Swift Project

How to integrate Cocoapods with a Swift project?

It seems that the process is similar to the one described in Mix and Match section of Using Swift with Cocoa and Objective-C documentation.

  1. Create your Podfile and run pod install.
  2. Create a new Objective-C header file, Example-Bridging-Header.h, and add it to the project.
  3. Add import statement to the bridge header.
  4. Set Objective-C Bridging Header for your target:

enter image description here

Now you can use your library, in that case, MKUnits, in your Swift file:

let kilograms = NSNumber.mass_kilogram(2)()
let pounds = NSNumber.mass_pound(10)()
let result = kilograms.add(pounds)
println(result)

More here: Integrating Cocoapods with a Swift project

Integarte/Install cocoapods to existing xcode project, objective-c or swift

Install CocoaPods on System

Step.1 Open Terminal and enter the following command:

sudo gem install cocoapods


Create Podfile for Project

Step.2 now you need to close Xcode.

Open Terminal at project's root folder

Step.3 Next, enter below command to create podfile:

pod init


Edit podfile

Note: Make sure we will edit podfile with Xcode not TextEdit etc.

Step.4 Type this command to open the Podfile using Xcode for editing:

open -a Xcode Podfile

Step.5 update pode file as shown below screenshot, save and close.

enter image description here

Install lib/framework

Now we have added our required lib/framework pod command

pod 'SwiftForms'

let's go for install

Step.5 Enter the following command in Terminal and hit Enter

pod install

Result screen

enter image description here

Thats it!! we have done.

Open Project with pods

Now go to the project folder,we can see that CocoaPods created a new project_name.xcworkspace file and a Pods folder.

open project_name.xcworkspace with xcode

your project structure should look like

enter image description here

Using a pod from inside a swift package

It's possible but it's not useful because Swift Packages don't have weak linking to frameworks as XCode project does with -weak_framework <framework_name> so you need to implement non trivial steps.

In swift package you can specify a framework to link with linkedFramework:

.target(
name: "MyPackage",
dependencies: [],
linkerSettings: [
.linkedFramework("Localize_Swift")
.unsafeFlags(["-F/Users/user/.../Localize-Swift"])
]
),

Where unsafeFlags specifies the full path to dir with the framework. But unexpectedly then you can not use your package with your app because of next issue:

The package product 'MyPackage' cannot be used as a dependency of this
target because it uses unsafe build flags.

To compile Swift Package with your framework you should copy Localize_Swift.framework to target build dir of your package and then you can omit unsafe build flags of the package because the compiler sees all dependencies on the dir's root level.

.target(
name: "MyPackage",
dependencies: [],
linkerSettings: [
.linkedFramework("Localize_Swift")
]
),

The same you can do with your app after adding your package. If you copy Localize_Swift.framework to the app's target build dir then your linked swift package can be compiled because it looks for linkedFramework in the current build dir.

By default pods are generated to separate folders in target build dir e.g.: $TARGET_BUILD_DIR/Localize-Swift/Localize_Swift.framework so you can change CONFIGURATION_BUILD_DIR for Localise-Swift target in Pods project to generate the framework to the root of the target build dir or make a script to copy etc. But there is a problem that swift package dependencies are started compiling at the early begin of compilation process when you don't have any compiled pod frameworks. So at the first stage you should compile your Localize_Swift.framework before (better to make a fat framework) and then add a Pre-actions Run Script under Build in your target scheme that copies the framework from your destination to target build dir.

cp -r $PROJECT_DIR/Localize_Swift.framework $TARGET_BUILD_DIR/Localize_Swift.framework

Now both your app and your swift package can be compiled with Localize_Swift framework.

As I say in the begin it's not useful because you need to compile Localize_Swift.framework manually (or with an additional script) before the general compilation process that neutralizes convenience of cocoa pods at all.

Consider using next preferred options:

  1. Pod is avaliable as a swift package. You can make a dependency in your package and in this case both your package and the dependency will be also available in your app. By the way Localize_Swift supports swift packages.

  1. Pod has swift sources You can make your own swift package with the sources files and then link it to your package.

  2. Pod has binaries From Swift 5.3 swift package can embed xcframework so you can build this xcframework from pod's binaries and then make binary target dependency in your swift package: .binaryTarget(name: "MyLib", path: "MyLib.xcframework") (see How can I add a local library as a dependency in Swift Package Manager)

How to use Objective-C CocoaPods in a Swift Project

Basic answer to your question is Yes, you can use objective-c code built with CocoaPods.

More important question is "How to use such libs?"

Answer on this question depends on use_frameworks! flag in your Podfile:

Let's imagine that you want use Objective-C pod with name CoolObjectiveCLib.

If your pod file uses use_frameworks! flag:

// Podfile
use_frameworks!
pod 'CoolObjectiveCLib'

Then you don't need add any bridge header files.

Everything that you need is import framework in Swift source file:

// MyClass.swift
import CoolObjectiveCLib

Now you can use all classes that are presented in lib.

If your pod file doesn't use use_frameworks! flag:

// Podfile
pod 'CoolObjectiveCLib'

Then you need create bridging header file and import there all necessary Objective-C headers:

// MyApp-Bridging-Header
#import "CoolObjectiveCLib.h"

Now you can use all classes that are defined in imported headers.

Adding minimal GRDB to an Xcode project using Swift Package Manager, Cocoa Pods, or other

Cocoa Pods

As indicated by the author, Swift Package Manager is only one option and is "not known to integrate with existing Xcode projects", so probably not a good choice. So to address part one of the question, the technique for integrating GRDB using Cocoa Pods is shown below.

Cocoa Pods Install

Based on this tutorial, you should be able to install CocoaPods without downloading anything first if your OS is OS X 10.7 or newer.

  • Applications > Utilities > Terminal
    • sudo gem install cocoapods
    • pod setup --verbose

My install seemed to work fine, but generated more logging than the tutorial showed.

Integration with Existing Project

  • Created a new project Applications > Xcode (9.4 beta) > Create a new Xcode project > Single View App > "FirstDb"
  • This created /Users/owner/documents/xcodeprojects/FirstDb/FirstDb.xcodeproj
  • Put import GRDB in the view controller (obviously couldn't compile yet)
  • Close Xcode! (we will be accessing it later, through xcworkspace, not xcodeproj)
  • Applications > Utilities > Terminal
    • cd /Users/owner/documents/xcodeprojects/firstdb
    • pod init
    • open -a Xcode PodFile

Make your file specify GRDB:

platform :ios, '9.0'
target 'FirstDb' do
use_frameworks!
pod 'GRDB.swift'
end

That doesn't take advantage of the versioning function available, so it will be up to you to make sure there's no breaking changes by introduced by GRDB as it is enhanced.

Use Cocoa Pods to get GRDB

Now is when you download GRDB:

  • Applications > Utilities > Terminal
    • cd /Users/owner/documents/xcodeprojects/firstdb
    • pod install

You should see something like this:

Analyzing dependencies
Downloading dependencies
Installing GRDB.swift (2.10.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `FirstDb.xcworkspace` for this project from now on.
....

Build the Project with GRDB import

Now open the workspace file FirstDb.xcworkspace (not the proj file). In the left project outline, you should see your starter project and also Pods:

FirstDb
Pods

Build the workspace (Product > Build), and you should see that your import GRDB line in the ViewController is compiling without errors.



Related Topics



Leave a reply



Submit