I’m having trouble with OAuth authentication for Twitch using the OAuthSwift library. When users complete the authorization process on Twitch, they don’t get redirected back to my iOS app.
Instead, Safari shows this warning message: You are about to leave Twitch. Twitch has no control over the content or security of http://localhost.
I tried setting up URL handling in my AppDelegate but it’s not working:
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if url.host == "localhost" {
OAuthSwift.handle(url: url)
}
return true
}
Here’s my authentication setup code:
@IBAction func loginButtonTapped(_ sender: Any) {
let oauth = OAuth2Swift(
consumerKey: "my_client_id",
consumerSecret: "my_client_secret",
authorizeUrl: "https://id.twitch.tv/oauth2/authorize",
accessTokenUrl: "https://id.twitch.tv/oauth2/token",
responseType: "code",
contentType: "application/json"
)
oauth.authorizeURLHandler = OAuthSwiftOpenURLExternally.sharedInstance
self.oauth = oauth
oauth.authorize(withCallbackURL: URL(string: "http://localhost")!, scope: "user:read:email", state: "TWITCH") { result in
switch result {
case .success(let (credentials, response, params)):
print(credentials.oauthToken)
TokenStorage.shared.token = credentials.oauthToken
case .failure(let error):
print(error.localizedDescription)
}
}
}
Any suggestions on how to fix this redirect issue would be helpful!