[GH-ISSUE #248] Can't seem to use SafariViewController #144

Closed
opened 2026-03-03 16:46:04 +03:00 by kerem · 7 comments
Owner

Originally created by @tunds on GitHub (Jul 7, 2016).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/248

Seems like i've set everything right but when trying to access the SafariViewController using the following

if #available(iOS 9.0, *) { oauthswift.authorize_url_handler = SafariURLHandler(viewController: self) } else { oauthswift.authorize_url_handler = AuthWebViewController() }

I get the following error in the console
2016-07-07 17:51:35.047 OAuthSwiftExample[402:76129] Warning: Attempt to present <SFSafariViewController: 0x14d49b10> on <OAuthSwiftExample.Auth: 0x14e39250> whose view is not in the window hierarchy!

Originally created by @tunds on GitHub (Jul 7, 2016). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/248 Seems like i've set everything right but when trying to access the SafariViewController using the following ` if #available(iOS 9.0, *) { oauthswift.authorize_url_handler = SafariURLHandler(viewController: self) } else { oauthswift.authorize_url_handler = AuthWebViewController() }` I get the following error in the console `2016-07-07 17:51:35.047 OAuthSwiftExample[402:76129] Warning: Attempt to present <SFSafariViewController: 0x14d49b10> on <OAuthSwiftExample.Auth: 0x14e39250> whose view is not in the window hierarchy!`
kerem 2026-03-03 16:46:04 +03:00
Author
Owner

@phimage commented on GitHub (Jul 7, 2016):

I think that this error indicate that we try to present the safari controller using self as parent (the one passed into SafariURLHandlerinit) but this selfcontroller is not displayed (or not into hierarchy) when you doing authentification

Maybe I have a temporary fix that could work for you (even if I think you use the wrong controller when doing init)
Try to edit SafariURLHandler class into OAuthSwiftURLHandlerType.swift
When controller is used, replace by UIViewController. topViewController(controller) or UIApplication.topViewController?

<!-- gh-comment-id:231143832 --> @phimage commented on GitHub (Jul 7, 2016): I think that this error indicate that we try to present the safari controller using `self` as parent (the one passed into `SafariURLHandler`init) but this `self`controller is not displayed (or not into hierarchy) when you doing authentification Maybe I have a temporary fix that could work for you (even if I think you use the wrong controller when doing init) Try to edit `SafariURLHandler` class into OAuthSwiftURLHandlerType.swift When `controller` is used, replace by `UIViewController. topViewController(controller)` or `UIApplication.topViewController?`
Author
Owner

@tunds commented on GitHub (Jul 7, 2016):

Hi there i'm not too sure i understand where to make the edits on which line in this class... Also the class that i'm calling this on is the rootviewcontroller as well reckon this could be the issue?

<!-- gh-comment-id:231148007 --> @tunds commented on GitHub (Jul 7, 2016): Hi there i'm not too sure i understand where to make the edits on which line in this class... Also the class that i'm calling this on is the rootviewcontroller as well reckon this could be the issue?
Author
Owner

@phimage commented on GitHub (Jul 7, 2016):

yes the controller passed must be the one currently displayed when launching authentification

<!-- gh-comment-id:231148404 --> @phimage commented on GitHub (Jul 7, 2016): yes the controller passed must be the one currently displayed when launching authentification
Author
Owner

@tunds commented on GitHub (Jul 7, 2016):

Ahh right okay so could you possible hint at which line to change since i'm a bit confused as to what i have to edit in that class

<!-- gh-comment-id:231148705 --> @tunds commented on GitHub (Jul 7, 2016): Ahh right okay so could you possible hint at which line to change since i'm a bit confused as to what i have to edit in that class
Author
Owner

@phimage commented on GitHub (Jul 7, 2016):

I talk first about a fix in oauthswift code and I think I say the file name and class
But to really fix your problem, it is in your code when you doing oauthswift.authorize_url_handler = SafariURLHandler(viewController: self) }
Do it into the view controller which launch the authentification, not appdeletage or something like that

<!-- gh-comment-id:231150899 --> @phimage commented on GitHub (Jul 7, 2016): I talk first about a fix in oauthswift code and I think I say the file name and class But to really fix your problem, it is in your code when you doing `oauthswift.authorize_url_handler = SafariURLHandler(viewController: self) }` Do it into the view controller which launch the authentification, not appdeletage or something like that
Author
Owner

@tunds commented on GitHub (Jul 7, 2016):

Hmmm see i've not done in the app delegate i've actually done it on the class which i want it do the app authentication on, as you can see below in my authentication class

` func doOAuthSoundCloud(consumerKey: String, consumerSecret: String) {

    let oauthswift = OAuth2Swift(
        consumerKey:    consumerKey,
        consumerSecret: consumerSecret,
        authorizeUrl:   authorizeUrl,
        accessTokenUrl: accessToken,
        responseType:   "code"
    )

    oauthswift.authorize_url_handler = SafariURLHandler(viewController: self)
    //oauthswift.authorize_url_handler = AuthWebViewController()

// if #available(iOS 9.0, *) {
//
// } else {
//
// }

    // Add safari and webview so it authenticated in app
    // Clean up code in model classes and root view controller

    let state: String = generateStateWithLength(20) as String
    oauthswift.authorizeWithCallbackURL( NSURL(string: callBackUrl)!, scope: "", state: state, success: {
        credential, response, parameters in
        // self.showTokenAlert(serviceParameters["name"], credential: credential)
        self.testSoundCloud(oauthswift,credential.oauth_token)
        }, failure: { error in
            print(error.localizedDescription)
    })
}`

and in my view controller when i want to login

`//
// ViewController.swift
// OAuthSwiftExample
//
// Created by Tunde Adegoroye on 07/07/2016.
// Copyright © 2016 Tunde Adegoroye. All rights reserved.
//

import UIKit

class ViewController: RootViewController {

@IBOutlet weak var loginBtn: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Do verification here

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func loginBtnDidTouch(sender: AnyObject) {
            authMethods.doOAuthSoundCloud("xxxxxxxx", consumerSecret: "xxxxxxxxxxx")
}

}

`

<!-- gh-comment-id:231152262 --> @tunds commented on GitHub (Jul 7, 2016): Hmmm see i've not done in the app delegate i've actually done it on the class which i want it do the app authentication on, as you can see below in my authentication class ` func doOAuthSoundCloud(consumerKey: String, consumerSecret: String) { ``` let oauthswift = OAuth2Swift( consumerKey: consumerKey, consumerSecret: consumerSecret, authorizeUrl: authorizeUrl, accessTokenUrl: accessToken, responseType: "code" ) oauthswift.authorize_url_handler = SafariURLHandler(viewController: self) //oauthswift.authorize_url_handler = AuthWebViewController() ``` // if #available(iOS 9.0, *) { // // } else { // // } ``` // Add safari and webview so it authenticated in app // Clean up code in model classes and root view controller let state: String = generateStateWithLength(20) as String oauthswift.authorizeWithCallbackURL( NSURL(string: callBackUrl)!, scope: "", state: state, success: { credential, response, parameters in // self.showTokenAlert(serviceParameters["name"], credential: credential) self.testSoundCloud(oauthswift,credential.oauth_token) }, failure: { error in print(error.localizedDescription) }) }` ``` and in my view controller when i want to login `// // ViewController.swift // OAuthSwiftExample // // Created by Tunde Adegoroye on 07/07/2016. // Copyright © 2016 Tunde Adegoroye. All rights reserved. // import UIKit class ViewController: RootViewController { ``` @IBOutlet weak var loginBtn: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // Do verification here } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func loginBtnDidTouch(sender: AnyObject) { authMethods.doOAuthSoundCloud("xxxxxxxx", consumerSecret: "xxxxxxxxxxx") } ``` } `
Author
Owner

@tunds commented on GitHub (Jul 8, 2016):

Move authentication to the correct class and it's now working.

<!-- gh-comment-id:231391853 --> @tunds commented on GitHub (Jul 8, 2016): Move authentication to the correct class and it's now working.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#144
No description provided.