5

I am implementing social login with TikTok in my app, From official documentation I implemented Basic setup and connected with my AppDelegate https://developers.tiktok.com/doc/getting-started-ios-quickstart-swift. Implemented loginkit with there sample code but request.send completionBlock is not getting any response or do not enter into completion block after we authorised from TikTok app. Please help if any one has implemented tiktok login kit in iOS.

/* STEP 1 */
let scopes = "user.info.basic,video.list" // list your scopes
let scopesSet = NSOrderedSet(array:scopes)
let request = TikTokOpenSDKAuthRequest()
request.permissions = scopesSet

/* STEP 2 */
request.send(self, completion: { resp -> Void in
    /* STEP 3 */
    if resp.errCode == 0 {
        /* STEP 3.a */
        let clientKey = ... // you will receive this once you register in the Developer Portal
        let responseCode = resp.code

        // replace this baseURLstring with your own wrapper API
        let baseURlString = "https://open-api.tiktok.com/demoapp/callback/?code=\(responseCode)&client_key=\(clientKey)"
        let url = NSURL(string: baseURlstring)

        /* STEP 3.b */
        let session = URLSession(configuration: .default)
        let urlRequest = NSMutableURLRequest(url: url! as URL)
        let task = session.dataTask(with: urlRequest as URLRequest) { (data, response, error) -> Void in
             /* STEP 3.c */
        }
        task.resume()
    } else {
        // handle error
    }
}
  • Still need help? – Leri Gogsadze Aug 06 '21 at 14:04
  • Same here, it never calls the completion block regardless if you explicitly retain request and data task. Redirects back to the app tho, but without any info it's looking for as per official documentation – bitemybyte Aug 07 '21 at 19:52
  • @LeriGogsadze thanks I have solved it by making changes in AppDelegate functions and sceneDelegate function which they mention in official documentations. – Hammad Hassan Aug 09 '21 at 06:21
  • 1
    sceneDelegate function they mention in openURLContexts to call "appDelegate.application(UIApplication.shared, open: url, options: [:])", I replaced it with "appDelegate.application(UIApplication.shared, open: url, sourceApplication: nil, annotation: "")". and its start working. @bitemybyte – Hammad Hassan Aug 09 '21 at 06:26
  • the code in the question doesn't work, doesn't even compile, can you share a working sample code that works well with TikTok's Login api ? – JAHelia Sep 03 '22 at 16:53

1 Answers1

4

Thanks to author's comment I figured that out too. In my case, there was no SceneDelegate in the project, so I had 3 url-related methods implemented in AppDelegate as per TikTok's documentation:

1:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool

2:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any)

3:

func application(_ application: UIApplication, handleOpen url: URL) -> Bool

The docs also suggested that 1st method should use a default value of [:] for options, which is plainly wrong so I removed it.

I also had Firebase dynamic links implemented in the 1st method:

if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
      self.handleDynamicLink(dynamicLink)
      return true
}

Turns out, if you remove the 1st method completely and move Firebase DL handling to method #2 everything starts working! Dynamic links are handled and TT's completion block finally gets called

bitemybyte
  • 971
  • 1
  • 10
  • 24
  • when TikTok app isn't installed, you will be redirected to an auth web page to finish the login flow, but when this webpage is dismissed the completion block will return an error (code 10006) which means: Invalid Redirect URL – JAHelia Sep 03 '22 at 21:14