Skip to main content

How to Install the Swift SDK

Installing SDK

There are several ways for you to install our SDK and the most convenient one is to use a package manager.

Installing with Package Manager

You can use CocoaPods or Swift Package Manager to manage your project's dependencies。

CocoaPods

Add the following pods into the target of Podfile::

pod 'LeanCloud'

or use the Subspecs way:

pod 'LeanCloud/Foundation' # (required) LeanStorage, SMS, LeanEngine etc.
pod 'LeanCloud/RTM' # (optional) LeanMessage, LiveQuery
# LeanCloud/RTM depends on LeanCloud/Foundation, so their versions must be consistent.
注意

LeanCloud/RTM 依赖了 LeanCloud/Foundation,所以不同模块的版本号必需保持一致。

Then install the SDK with the following command:

pod update

Or

pod install --repo-update

See official documentation for the differences between them.

After installing the SDK, you can open your project with <PROJECT_NAME>.xcworkspace under the root directory of your project.

Installing Manually

SDK

Initializing Your Project

To begin with, get your App ID and App Key from your app's Dashboard > Settings > App Keys.

Add the following line to the top of AppDelegate.swift:

import LeanCloud

Then add the code below into the function `application(_:didFinishLaunchingWithOptions:):

do {
try LCApplication.default.set(
id: your-app-id,
key: your-app-key,
serverURL: "https://your_server_url")
} catch {
print(error)
}

Enabling Debug Logs

You can easily trace the problems in your project by turning debug logs on during the development phase. Once enabled, details of every request made by the SDK along with errors will be output to IDE, browser console, or your app's Dashboard > LeanEngine > App logs.

// Place the line before LCApplication.default.set; only needs to be called once
LCApplication.logLevel = .all

Make sure debug logs are turned off before your app is published. Failure to do so may lead to the exposure of sensitive data.

Verifying

First of all, make sure you are able to connect to LeanCloud server. You can test it by running the following command:

curl "https://{{host}}/1.1/date"

If everything goes well, it will return the current date:

{ "__type": "Date", "iso": "2020-10-12T06:46:56.000Z" }

Then add the following code into your project:

do {
let testObject = LCObject(className: "TestObject")
try testObject.set("words", value: "Hello world!")
let result = testObject.save()
if let error = result.error {
print(error)
}
} catch {
print(error)
}

Save and run your program.

Then go to Dashboard > Data Storage > Data > TestObject. If you see a row with its words column being Hello world!, it means that you have correctly installed the SDK.

See Debugging if you’re not seeing the content.

Debugging

This guide is written for the latest version of our SDK. If you encounter any error, please first make sure you have the latest version installed.

401 Unauthorized

If you get a 401 error or see the following content in network logs:

{
"code": 401,
"error": "Unauthorized."
}

It means that the App ID or App Key might be incorrect or don't match. If you have multiple apps, you might have used the App ID of one app with the App Key of another one, which will lead to such error.

Client Cannot Access the Internet

Make sure you have granted enough permissions for your mobile app.