Getting a User Token inside iOS App

Hello dear readers,

I have been struggling for days with a problem involving the TwitchAPI and SwiftUI.

Short Version of my problem:
I am unable to obtain a user token in iOS because Twitch only accepts the https protocol and not the Apple “myApp://” scheme, which means I can’t redirect back to my app.

Long Version of my Problem:
I am developing an app that requires data from Twitch, particularly information about followers. However, since there is a rate limit for API requests, I souldnt use my app token and need to obtain a user token instead. This can be easily generated using the following code:

let authURL = "\(CLIENT_ID)&redirect_uri=\(REDIRECT_URI)&response_type=code&scope=\(scopes.joined(separator: " "))".

Once the user logs in and accepts, I should be redirected to the app to fetch the token and continue working with it. The problem is that to redirect to my app, Xcode needs to use a URL scheme like “myApp://auth,” which TwitchAPI console does not accept for redirecting purposes. I have tried various solutions, including the OAuthSwift library on GitHub and setting up a webserver for redirecting the URL, but I have not been successful.

If anyone has any suggestions for how to solve this problem, I would greatly appreciate your input. Thank you in advance!

Redirect to a website.
Then have the website redirect to your custom handler.

This is what I currently use in a released mobile app on iOS and android.

Under iOS the module/plugin I use utilises ASWebAuthenticationSession under the hood

I send the user to https://mywebsite/ which does all the oauth
Then when it has a token my website will redirect to customhandker://success?token=foo then I parse out the token.

1 Like

Dear Berry,
thanks for your answer. I managed it this way:
On my server I hosted a php file with this script:

$code = $_GET['code'];
if (isset($code)) {
  header("Location: myApp://callback?code=" . $code);
} else {
  echo "Authorization failed.";

In my Code I fetched the Information with

.onOpenURL{ url in
                guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
                let scheme = components.scheme,
                let host =,
                let queryItems = components.queryItems else { return }
                if scheme == "myApp", host == "callback" {

Thanks for your help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.