iOS 接入指南

环境要求

iOS 10.0 及以上版本 XCode 4.5 及以上版本

获取 ClientKey

请到开发者应用登记页面进行申请,申请后将获得 ClientKey (下文中将 ClientKey 称为 AppID),审核通过的应用可使用 ClientKey 和 抖音短视频 通信。

通过 CocoaPods 集成 (推荐)

第一步:在工程的 Podfile 里面添加以下代码:

pod 'DouyinOpenSDK', '~> 4.1.0'

第二步:运行 pod install 命令安装 SDK

pod install --repo-update

运行 pod install 不会自动更新 repo,如果下载的不是最新版,可使用 pod install —repo-update 更新或先运行 pod repo update。

手动集成 (不推荐)

下载 SDK 包

点击下载 DouyinOpenSDK SDK 包。

集成 Framwork

  1. 将解压后的 DouyinOpenSDK.framework文件copy拖拽到工程文件夹中,左侧目录选中工程名,在 TARGETS->Build Phases-> Link Binary With Libaries 中点击“+”按钮,在弹出的窗口中点击“Add Other”按钮,选择工程目录下的 DouyinOpenSDK.framework 文件添加到工程中。

2.正确配置如下图所示

3.引入系统库说明:

  • WebKit.framework- 是为了SDK内部走web授权时所使用
  • Security.framework- 是为了SDK传递信息安全的加解密库
  • 引入系统库的操作如下: 左侧目录中选中工程名,在TARGETS->Build Phases->Link Binary With Libaries中点击“+”按钮,在弹出的窗口中查找并选择所需的库(见下表),单击“Add”按钮,将库文件添加到工程中。

4.在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC,字母 O 和 C 大写。

配置工程

配置 URLScheme

无论是通过 Pod 方式还是手动将SDK引入工程之后,需要配置你的 App 与抖音短视频通讯的 URLScheme

1.为了保证可以正常唤起抖音短视频,在 info 标签栏的Custom iOS Target Properties中找到 LSApplicationQueriesSchemes 如果没有点击“+”添加一个并设置 Key 为LSApplicationQueriesSchemes, Value 类型为数组,将如下配置粘贴到数组中:

<key>LSApplicationQueriesSchemes</key>
<array>
<string>douyinopensdk</string>
<string>douyinsharesdk</string>
<string>snssdk1128</string>
</array>

2.为了保证抖音短视频可以正常唤起你的 App,在 Xcode 中,选择你的 TARGETS,在 info 标签栏的最下面的 URL Types,在 URL Schemes 里面填入你申请得到的 ClientKey 。在抖音短视频完成相关请求之后通过这个URL Schemes和你的 App 通信。

3.在 info 标签栏的Custom iOS Target Properties中点击+新加一项设置 Key 为DouyinAppID,value 为 String 类型,值为你申请的 AppID。在 App 启动时会自动将配置同步到 SDK 中

<key>DouyinAppID</key>
<string>$ClientKey</string>

注: 你如果不想通过 Info.plist 的方式向 SDK 注册你的 Key。也可以通过如下方式通过代码方式进行注册

 [[DouyinOpenSDKApplicationDelegate sharedInstance] registerAppId:DouYinNewClientKey];

如果你同时使用了两种设置方式将按照 - [DouyinOpenSDKApplicationDelegate application: didFinishLaunchingWithOptions:]- [DouyinOpenSDKApplicationDelegate registerAppId:] 调用顺序进行覆盖,后者覆盖前者

配置相册使用权限

分享的图片通过相册进行跨进程共享手段,如需使用分享功能,还需要填相册访问权限,在 info 标签栏中添加 Privacy - Photo Library Usage Description。

正确的完成上述配置如下图所示

注意:请务必保证正确填写你获得 ClientKey ,并保证和在网页上申请是所填写的 BundleID 和 工程配置中的 BundleID 一致

SDK 使用

初始化以及 UIApplicationDelegate 部分

在 AppDelegate 中引入DouyinOpenSDKApplicationDelegate.h头文件 并在 App 启动、收到 Open URL 打开 App 时调用 SDK 进行处理

#import<DouyinOpenSDK/DouyinOpenSDKApplicationDelegate.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[DouyinOpenSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
 return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

 if ([[DouyinOpenSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]
        ) {
 return YES;
    }

 return NO;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{

 if ([[DouyinOpenSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]) {
 return YES;
    }

 return NO;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
 if ([[DouyinOpenSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:nil annotation:nil]) {
 return YES;
    }
 return NO;
}
@end

获得日志信息

通过实现向 DouyinOpenSDKApplicationDelegate 注册 Log 的代理 DouyinOpenSDKLogDelegate 便可以通过 -id onLog: 可以收到 SDK 内部的一些错误的或者警告信息。

e.g.

#import <DouyinOpenSDK/DouyinOpenSDKApplicationDelegate.h>

@interface AppDelegate () <DouyinOpenSDKLogDelegate>

#pragma mark

- (void)registerLogDelegate
  {
  [DouyinOpenSDKApplicationDelegate sharedInstance].logDelegate =self;
  }

#pragma mark - DouyinOpenSDKLogDelegate Delegate

- (void)onLog:(NSString \*)logInfo
  {
  //Process log
  }

@end
诚邀您对本文档易读易用性进行评价
好用
不好用