Swift 项目引入 LeanCloud

发表于 2018-09-08   |   分类于 Swift 课程

安装

  • 创建一个 Single View Application
  • 在项目根目录创建一个 Podfile
  • 在其中加入如下代码,这里的 ins-test 是项目名,然后执行 pod install
use_frameworks! 
target "ins-test" do
  pod "LeanCloud", "~> 11.0.1"
end
  • 打开项目名.xcworkspace文件
  • 在项目的 AppDelegate.swift 中加入 LeanCloud 引入的代码引入代码

测试

Appdelegate.swift 文件中加入如下代码

测试代码

然后运行应用即可在 LeanCloud 控制台看到创建的数据

测试数据

测试代码

Podfile

use_frameworks! # LeanCloud Swift SDK can only be integrated as framework.

target "ins-test" do
  pod "LeanCloud", "~> 11.0.1"
end

AppDelegate.swift

//
//  AppDelegate.swift
//  ins-test
//
//  Created by HuanCheng Bai on 2018/9/8.
//  Copyright © 2018年 HuanCheng Bai. All rights reserved.
//

import UIKit
import LeanCloud

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        LeanCloud.initialize(applicationID: "3B5SllOK7TgK5xGQTpxXARPH-gzGzoHsz", applicationKey: "9DB0NF42z5uNUlzb6WbeovDE")
        let post = LCObject(className: "TestObject")
        post.set("words", value: "Hello World!")
        post.save { result in
            switch result {
            case LCBooleanResult.success: print("Success")
            case LCBooleanResult.failure: print("Failure")
            }
        }
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

参考链接

  1. https://leancloud.cn/docs/start.html
  2. https://cocoapods.org
  3. https://leancloud.cn/docs/leanstorage_guide-swift.html

发表新评论

© 2019 Powered by Typecho & Theme 豫ICP备15027577号