KMM: Use a Shared Binary Library in an iOS Project XCFramework Manually

March 6, 2023
 
Manually add an iOS XCFramework File Dependency to Your Local Native iOS Application

In my previous article, we used Kotlin Multiplatform Mobile (KMM) to build shared binary libraries: AAR and XCFramework for Android and iOS respectively. In this article, I will show you how to manually add it to your local native iOS application.

Download the Shared Library

You can build the XCFramework files yourself by following the previous article, or you can download from here:

https://bitbucket.org/CodeGorilla/cg-lib-kmm-shared/src/master/source/codeGorillaLibrary/build/XCFrameworks/

Create a New iOS Project

Open Xcode and create a new project using an Empty Activity.


Now run the Hello World template code to ensure that your project is initialized properly.

Manually Add XCFramework Dependency

Create Frameworks Folder/Group

For this example, we will manually copy the XCFramework files into the Frameworks folder of your project. The Frameworks folder is NOT created automatically with the Xcode template, so you'll have to create it yourself. To create the Frameworks folder, Create a New Group and call it Frameworks.

Right Click on the Project | New Group | Frameworks

Copy XCFramework into Frameworks Group

Drag and drop your XCFramework into the folder. You should be prompted to select some options and you should check the following boxes: “Copy items if needed” and “Create groups”


Embed & Sign

You will want to embed and sign your framework within your project, to set this up, click on your project, within the General tab, scroll to the Frameworks, Libraries, and Embedded Content section, you should see that your library is already listed there. Change the Embed value from Do Not Embed to Embed & Sign


Project | General | Frameworks, Libraries, and Embedded Content

Using the Dependency in the Code

At this point, you're all set to begin using your dependency.

To stay focused on our objective, we will be making minimal updates to the template code to simply confirm that we have access to the XCFramework shared library that was created from KMM.

Edit the ContentView file to import our library and use it as follows:

import SwiftUI
import codeGorillaLibrary

struct ContentView: View {
init() {
let celsius = TemperatureConverter().fahrenheitToCelsius(fahrenheit: 0.00)
let fahrenheit = TemperatureConverter().celsiusToFahrenheit(celsius: 0.00)
let _ = print("KMM, Celsius: \(celsius)")
let _ = print("KMM, Fahrenheit: \(fahrenheit)")
}
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.padding()
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Run your code and you should now see the Celsius and Fahrenheit results in the Debug Area.


Download the Code

You can download the code for this article here:

git clone https://CodeGorilla@bitbucket.org/CodeGorilla/cg-app-using-kmm-lib-ios.git

References



 
Return to articles