# SDK教程

# 准备工作

下载SDK

# 安装

使用CocoaPods安装

#需要打开 use_frameworks!
use_frameworks!

pod 'TTCSDK'

注意: 如果使用的Object-c语言接入的SDK,请指定SDK引用库的swift版本号为4.1

# 添加APP的URL Scheme

添加规则为:"TTC" + "-" + "APP的Bundle identifier"。 例如TTC-com.tataufo.TTC-SDK-iOS-Demo

# SDK初始化

在程序启动时调用

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)
        window!.rootViewController = ViewController()
        window!.makeKeyAndVisible()

        TTCSDK.register(appId: "AppId", secretKey: "SecretKey", scheme: "anyScheme") { (result, error) in
            if result {
                print("register success")
            } else {
                print("register failure:\(error!)")
            }
        }
        return true
    }

# 处理钱包的绑定

系统提供了三种方法,需要注意的是如果都实现只会走最新的。

// after iOS 9
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
    let result = TTCSDK.handleApplication(openURL: url)
    return result
}

// iOS 4.2 - iOS 9
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    let result = TTCSDK.handleApplication(openURL: url)
    return result
}

// iOS 2 - iOS 9
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
    let result = TTCSDK.handleApplication(openURL: url)
    return result
}

# 用户登录

Dapp必须某时刻调用该方法,否则后续用户行为皆不可用。此处登录只需要userID 不需要用户其他的属性,返回的用户信息包含用户在TTC主链对应的地址

let user = TTCUserInfo(userId: userId)

TTCSDK.login(userInfo: user) { (success, error, _) -> Void in

    if success {
        print("login success")
    } else {
        print(error!)
    }
}

# 更新用户信息

调用更新用户信息接口,userID必须与当前登录用户一致。

func updataUser(user: TTCUserInfo) {

    TTCSDK.update(userInfo: user) { (success, error, _) -> Void in

        //返回的user 会增加ttc主链地址
        if success {
            print("Update user information success")
            TWToast.showToast(text: "Update user information success")
            self.dismiss(animated: true, completion: nil)
        } else {
            print("Update information failed:\(error ?? "未知错误")")
            TWToast.showToast(text: "Update information failed:\(error ?? "")")
        }
    }
}

# 退出登录

退出登录后其他用户相关的操作无法进行

TTCSDK.logout()

# 查询未转入钱包的余额

Dapp已绑定钱包,当有未转入钱包的TTC,可使用该方法查询

TTCSDK.queryAccountBalance { (success, error, balance) in

    if success {
        print("balance: \(balance)")
    } else {
        print("error: \(error!)")
    }
}

# 查询钱包的余额

Dapp已绑定钱包,查询钱包余额

TTCSDK.queryWalletBalance { (success, error, balance) in
    if success {
        print("balance: \(balance)")
    } else {
        print("error: \(error!)")
    }
}

# 解除绑定钱包

Dapp已绑定钱包,可使用该方法解除绑定

TTCSDK.unBindWallet { (success, error) in
    if success {
        print("Untied success")
    } else {
        print("error: \(error!)")
    }
}

# 用户行为上传

记录用户行为,actionType: 行为类型必须大于100。

extra: 行为其它字段,必须是json字符串

let timestamp = Int64(Date().timeIntervalSince1970 * 1000)
let extra = "{'actionType':\(actionType), 'userID':\(TTCUser.shared.userId ?? ""), 'content':'发了一个帖子哈哈哈哈哈哈','timestamp':\(timestamp)}"

TTCUploadAction.uploadAction(actionType: 1, extra: extra) { (success, error) in
    if success {
        print("Record success")
    } else {
        print("Record failure:\(error!)")
    }
}

# 标记SDK是否可用状态

默认SDK都是可用状态,若想使SDK不可用设为false

TTCSDK.sdk(isEnabled: false)

# 错误类型

每个可以返回错误信息的方法都会使用TTCSDKError,包含错误码和错误描述。

@objc public class TTCSDKError: NSObject {
    /// Error number
    @objc public var code: String = ""
    /// Error number
    @objc public var errorDescription: String = ""
}

# 错误码

SDK错误码文档,请点击这里

# TTC广告

# 初始化广告

在加载广告之前,应用应调用 TTCAdMob 中的 configure: 类方法并向其传递 AdMob 应用 ID,以便初始化TTC广告 。此操作仅需执行一次,最好是在应用启动时执行。

以下示例展示了如何在 AppDelegate 中调用 configure: 方法:

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
TTCAdMob.configure(appID: "YOUR_ADMOB_APP_ID")
return true
}
}

# 横幅广告

横幅广告是在应用布局中占据一处位置的矩形图片或文字广告。用户与应用互动时,这类广告会停留在屏幕上,并且可在一段时间后自动刷新。

创建 TTCAdBanner

var banner: TTCAdBanner!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

banner = TTCAdBanner(adSize: .LargeBanner)
banner.rootViewController = self
banner.delegate = self
banner.adUnitID = "ca-app-pub-3940256099942544/2934735716"
view.addSubview(banner.bannerView)
banner.bannerView.snp.makeConstraints { (make) in
make.centerX.equalTo(view)
make.bottom.equalTo(-34)
}
}

加载广告

let request = TTCAdRequest()
//        request.testDevices = [TTCkAdSimulatorID, "a6f4cc131cbe0effa815572262d24262"]
banner.loadRequest(requset: request)

广告事件

TTCAdBannerDelegate 中的每个方法都是可选方法,因此您只需实现所需的方法即可。

func adViewDidReceiveAd(_ banner: TTCAdBanner) {
print("adViewDidReceiveAd")
}

func adViewDidFailToReceiveAd(banner: TTCAdBanner, error: Error) {
print("adViewDidFailToReceiveAd")
}

func adViewWillPresentScreen(banner: TTCAdBanner) {
print("adViewWillPresentScreen")
}

func adViewWillDismissScreen(banner: TTCAdBanner) {
print("adViewWillDismissScreen")
}

func adViewDidDismissScreen(banner: TTCAdBanner) {
print("adViewDidDismissScreen")
}

func adViewWillLeaveApplication(banner: TTCAdBanner) {
print("adViewWillLeaveApplication")
}

# 插页式广告

插页式广告是全屏广告,它会覆盖整个应用界面,直到用户将其关闭。这些广告通常会在应用流程的自然过渡点(例如,活动之间或游戏关卡之间的暂停时段)展示。当应用展示插页式广告时,用户可以选择点按广告,访问其目标网址,也可以将其关闭,返回应用。

创建插页式广告对象并加载广告

override func viewDidLoad() {
super.viewDidLoad()
interstitial = TTCAdInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
let request = TTCAdRequest()
//        request.testDevices = [TTCkAdSimulatorID, "a6f4cc131cbe0effa815572262d24262"]
interstitial.loadRequest(requset: request)
}

展示广告

要展示插页式广告,请检查 TTCAdInterstitial 上的 isReady 属性以验证加载已完成,然后再调用 present。

@IBAction func doSomething(_ sender: AnyObject) {
...
if interstitial.isReady {
interstitial.present(rootViewController: self)
} else {
print("Ad wasn't ready")
}
}

TTCAdInterstitial 是一次性对象,这意味着插页式广告展示后,hasBeenUsed 会返回 true,就不能再用该插页式广告对象加载另一个广告了。要请求另一个插页式广告,您需要创建一个新的 TTCAdInterstitial 对象。如果您尝试重复使用插页式广告对象,则会收到如下错误响应:“Request Error: Will not send request because interstitial object has been used”。

广告事件代理

func interstitialDidReceiveAd(ad: TTCAdInterstitial) {
print("interstitialDidReceiveAd")
}

func interstitialDidFailToReceiveAdWithError(ad: TTCAdInterstitial, error: Error) {
print("interstitialDidFailToReceiveAdWithError")
}

func interstitialWillPresentScreen(ad: TTCAdInterstitial) {
print("interstitialWillPresentScreen")
}

func interstitialDidFailToPresentScreen(ad: TTCAdInterstitial) {
print("interstitialDidFailToPresentScreen")
}

func interstitialWillDismissScreen(ad: TTCAdInterstitial) {
print("interstitialWillDismissScreen")
}

func interstitialDidDismissScreen(ad: TTCAdInterstitial) {
print("interstitialDidDismissScreen")
}

func interstitialWillLeaveApplication(ad: TTCAdInterstitial) {
print("interstitialWillLeaveApplication")
}

# 激励视频广告

激励视频广告是一种全屏视频广告,用户可选择使用全屏模式观看,以换取应用内奖励。

激励视频广告初始化并请求广告 TTCAdRewardBasedVideoAd 具有单实例设计

var rewardBasedVideo: TTCAdRewardBasedVideoAd!
override func viewDidLoad() {
super.viewDidLoad()

rewardBasedVideo = TTCAdRewardBasedVideoAd.sharedInstance
rewardBasedVideo.delegate = self
let request = TTCAdRequest()
//            request.testDevices = [TTCkAdSimulatorID, "a6f4cc131cbe0effa815572262d24262"]
rewardBasedVideo.loadRequest(request: request, adUnitID: "ca-app-pub-3940256099942544/1712485313")
}

展示激励视频广告

if rewardBasedVideo.isReady {
rewardBasedVideo.present(rootViewController: self)
}

广告事件代理

func rewardBasedVideoAd(rewardBasedVideoAd: TTCAdRewardBasedVideoAd, didRewardUserWithReward reward: TTCAdReward) {
print("receve: \(reward.amount?.description ?? "null") \(reward.rewardType?.description ?? "what?")")
}

func rewardBasedVideoAd(rewardBasedVideoAd: TTCAdRewardBasedVideoAd, didFailToLoadWithError error: Error)  {
adRequestInProgress = false
print("didFailTo" + error.localizedDescription)
}

func rewardBasedVideoAdDidReceiveAd(rewardBasedVideoAd: TTCAdRewardBasedVideoAd) {
adRequestInProgress = false
print("DidReceiveAd")
}

func rewardBasedVideoAdDidOpen(rewardBasedVideoAd: TTCAdRewardBasedVideoAd) {
print("rewardBasedVideoAdDidOpen")
}

func rewardBasedVideoAdDidStartPlaying(rewardBasedVideoAd: TTCAdRewardBasedVideoAd) {
print("rewardBasedVideoAdDidStartPlaying")
}

func rewardBasedVideoAdDidCompletePlaying(rewardBasedVideoAd: TTCAdRewardBasedVideoAd) {
print("rewardBasedVideoAdDidCompletePlaying")
}

func rewardBasedVideoAdDidClose(rewardBasedVideoAd: TTCAdRewardBasedVideoAd) {
print("rewardBasedVideoAdDidClose")
}

func rewardBasedVideoAdWillLeaveApplication(rewardBasedVideoAd: TTCAdRewardBasedVideoAd) {
print("rewardBasedVideoAdWillLeaveApplication")
}