本文档主要介绍 iOS 如何集成 V4 版 Tap 登录 iOS 原生 SDK。使用的是 Swift 语言,Pod 方式导包。
⚠️ 注意:
iOS SDK 要求 Xcode 版本最低为 15.3
集成文档演示的是 V4 版本的 TapSDK 版本为 4.3.10
一、 创建项目
二、导入依赖 打开终端使用 pod 方式远程依赖;
1、进入项目目录下
2、生成 Profile 文件
3、在 Profile 文件中添加需要引入的依赖:
pod 'TapTapLoginSDK ', '~> 4.3 .10 ' pod 'TapTapCoreSDK ', '~> 4.3 .10 '
添加后文件内容如下:
4、 保存文件后执行安装的命令:
5 安装完成后,双击项目目录中的 **项目名.xcworkspace **文件,打开项目。
三、项目配置 1、 配置编译选项
在 Build Setting 中的 Other Link Flag 中添加 -ObjC 和 -Wl -ld_classic。
在 Build Setting 中的 Always Embed Swift Standard Libraries 设置为 YES,即始终引入 Swift 标准库,避免 App 启动时报错「无法找到 Swift 标准库之类」。如果项目中找不到,可以建立一个空 Swift 文件,Xcode 会自动建立桥接关系。
在 Build Setting 中的 Swift Compiler - Language/Swift Language Version 选择 Swift 5。
将工程 Pods 目录下 TapTapLoginSDK/Frameworks/TapTapLoginResource.bundle 资源文件导入工程中
点击 + 号将 Pods 文件夹中的 TapTapLoginResource.bundle 文件夹添加即可。
2、工程配置 1.打开 info.plist,添加如下配置(请替换 clientID 为你在控制台获取的 Client ID):
<key>CFBundleURLTypes </key> <array> <dict> <key>CFBundleTypeRole</key > <string>Editor </string> <key>CFBundleURLName</key > <string>taptap</string> <key>CFBundleURLSchemes</key > <array> <!-- 这里注意下,中括号需要去掉,最终是 Client ID 前拼接上 `tt` 的形式,例如:ttFwFdCxxxxxxxQDQwQN --> <string>tt[clientID]</string> </array > </dict> </array ><key>LSApplicationQueriesSchemes </key> <array> <string>tapiosdk</string > <string>tapsdk</string> <string>taptap</string ></array>
配置 openUrl
如果项目中有 SceneDelegate.swift,请先删除,然后请添加如下代码到 AppDelegate.swift 文件:
import TapTapLogin SDK func application(_ app: UIApplication , open url: URL ) -> Bool { return TapTapLogin .open(url: url) } func application(_ app: UIApplication , open url: URL , options: [UIApplication .OpenURLOptionsKey : Any ] = [: ]) -> Bool { return TapTapLogin .open(url: url) } func application(_ application: UIApplication , open url: URL , sourceApplication: String ?, annotation: Any ) -> Bool { return TapTapLogin .open(url: url) }
func application(_ application: UIApplication , configurationForConnecting connectingSceneSession: UISceneSession , options: UIScene .ConnectionOptions ) -> UISceneConfiguration { } func application(_ application: UIApplication , didDiscardSceneSessions sceneSessions: Set <UISceneSession >) { }
在 AppDelegate.swift 中添加 UIWindow
四、功能接口 示例代码见 ViewController.swift 文件中内容:
import UIKitimport TapTapCoreSDKimport TapTapLoginSDKclass ViewController : UIViewController { var scopes: [Scope ] = [Scope .publicProfile] let button1 = UIButton (type: .system) let button2 = UIButton (type: .system) override func viewDidLoad () { super .viewDidLoad() setupButtons() } func setupButtons () { button1.setTitle("初始化" , for: .normal) button1.frame = CGRect (x: 50 , y: 100 , width: 200 , height: 50 ) button1.addTarget(self , action: #selector (button1Clicked), for: .touchUpInside) view.addSubview(button1) button2.setTitle("登录" , for: .normal) button2.frame = CGRect (x: 50 , y: 200 , width: 200 , height: 50 ) button2.addTarget(self , action: #selector (button2Clicked), for: .touchUpInside) view.addSubview(button2) } @objc func button1Clicked () { let options = TapTapSdkOptions () options.clientId = "y2a********vh47m" options.clientToken = "XknZNP*********NzsiPgN0tp******PDHM" options.region = .CN options.enableLog = true options.preferredLanguage = TapLanguageType .auto TapTapSDK .initWith(options) } @objc func button2Clicked () { TapTapLogin .login(with: scopes) { [weak self ] account, error, isCancel in guard let self = self else { return } if isCancel { self .handleLoginCancelled() return } if let error = error { self .handleLoginError(error) return } if let account = account { self .handleSuccessfulLogin(account: account) } else { self .handleUnknownError() } } } func handleSuccessfulLogin (account : TapTapLoginSDK .TapTapAccount ) { print ("登录成功" ) } func handleLoginError (_ error : Error ) { print ("登录失败" ) } func handleLoginCancelled () { print ("用户取消了登录" ) } func handleUnknownError () { print ("登录过程中发生未知错误" ) } }
五、编译打包
六、常见问题: 1、如果编译中报错:Multiple commands produce '/Users/ggghhh/Library/Developer/Xcode/DerivedData/TestV4-cslrpkjkfditukenwptbwubpssmc/Build/Products/Debug-iphoneos/TestV4.app/TapTapLoginResource.bundle'
A: 表示 TapTapLoginResource.bundle 重复引入了,可以将如下图位置中的 TapTapLoginResource.bundle 删除;
2、如果报错:
Sandbox: rsync.samba(41256) deny(1) file-write-create
A: 可以将 User Script Sandboxing 设置为 NO
3、如果 Pod 安装 依赖库报错:git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository
A: 可以先使用该命令检查是否有将本地的 key 配置到 Github:ssh -T git@github.com
如果命令返回不是 success 表示未配置,请参考该Github配置ssh key的步骤 文档进行配置.然后重新执行 pod install 引入依赖库。