TikTok iOS SDK Swift— Videos, Photos Sharing in TikTok from your iOS app

Step 1: Configure TikTok App Settings for iOS

Go to TikTok Developer App Registration Page to create your app. After approval, you will get the Client Key and Client Secret. Which is must to implement this SDK in iOS.

Step 2: Install the SDK

Add the pod to your Podfile:

pod 'TikTokOpenSDK', '~> 5.0.0'
pod install

Step 3: Configure Xcode Project

  1. Update Info.plist just like below
  2. LSApplicationQueriesSchemes: Use to Open TikTop App
  3. TikTokAppID: Use to config TikTok OpenSDK
  4. CFBundleURLTypes : Use TikTok App callback your App
<key>LSApplicationQueriesSchemes</key>
<array>
<string>tiktokopensdk</string>
<string>tiktoksharesdk</string>
<string>snssdk1180</string>
<string>snssdk1233</string>
</array>
<key>TikTokAppID</key>
<string>$TikTokAppID</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>$TikTokAppID</string>
</array>
</dict>
</array>

Step 4: Update AppDelegate and SceneDelegate

Update AppDelegate.h like below.

import TikTokOpenSDK
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
TikTokOpenSDKApplicationDelegate.sharedInstance().logDelegate = self
TikTokOpenSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)return true}func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

guard let sourceApplication = options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
let annotation = options[UIApplication.OpenURLOptionsKey.annotation] else {
return false
}

if TikTokOpenSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: sourceApplication, annotation: annotation) {
return true
}
return false
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
if TikTokOpenSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {
return true
}
return false
}

func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
if TikTokOpenSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: nil, annotation: "") {
return true
}
return false
}
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
_ = appDelegate.application(UIApplication.shared, open: url, options: [:])
}
}
}

Step 5: TikTokOpenSDKLogDelegate

ERROR or Warning will callback in this method.

func onLog(_ logInfo: String) {print(logInfo)}

Step 6: Implement Video Kit Functionality

Video Kit allows users to share videos and photos from your app to TikTok. Make sure your app has access to Photo Library.

private class func shareToTikTok(isVideo: Bool, asset: PHAsset) {let isInstalled = TikTokOpenSDKApplicationDelegate.sharedInstance().isAppInstalled()if (isInstalled) {let request = TikTokOpenSDKShareRequest()request.mediaType = isVideo ? TikTokOpenSDKShareMediaType.video : TikTokOpenSDKShareMediaType.imagevar mediaLocalIdentifiers: [String] = []mediaLocalIdentifiers.append(asset.localIdentifier)request.localIdentifiers = mediaLocalIdentifiersrequest.hashtag = "ERSocialShareManager"request.send(completionBlock: { resp -> Void inprint(resp)})}}

Conclusion

And finally we are done. Thank you very much for reading. Happy coding :)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mohammad Mahmudul Hasan

Mohammad Mahmudul Hasan

37 Followers

A learner, constantly striving to learn new technologies and look the ways to be better in this rapidly changing industry.