Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminating app due to uncaught exception 'Attempt to call unavailable initializer.', reason: 'Called: init Designated Initializer:initWithAuthState:' #20

Open
ibhavin opened this issue Apr 7, 2017 · 3 comments

Comments

@ibhavin
Copy link

ibhavin commented Apr 7, 2017

I am using google drive sdk and I have implemented GTMAppAuth for login in my swift project.

In AppDelegate.swift:

weak var currentAuthorizationFlow: OIDAuthorizationFlowSession?
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        if (currentAuthorizationFlow?.resumeAuthorizationFlow(with: url))! {
            currentAuthorizationFlow = nil
            return true
        }
        // Your additional URL handling (if any) goes here.
        return false
    }

In ViewController.swift:

//MARK:- Login Tapped
    @IBAction func login_Tapped(_ sender: RBButton) {
        let issuer = URL.init(string: kIssuer)
        let redirectUrl = URL.init(string: kRedirectURI)
        
        OIDAuthorizationService.discoverConfiguration(forIssuer: issuer!) { (configuration, error) in
            if !(configuration != nil) {
                print("Error retrieving discovery document: \(error?.localizedDescription)")
                //self.setGTMAuthorization(authorization: nil)
                return
            }
            print("Got Configuration: \(configuration)")
            
            //authentication request
            let request = OIDAuthorizationRequest.init(configuration: configuration!, clientId: kClientID, scopes: [OIDScopeOpenID, OIDScopeProfile], redirectURL: redirectUrl!, responseType: OIDResponseTypeCode, additionalParameters: nil)
            
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            print("Initiating authorization request with scope: \(request.scope)")
            
            appDelegate.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request, presenting: self, callback: { (authState, error) in
                if (authState != nil) {
                    let authorization = GTMAppAuthFetcherAuthorization.init(authState: authState!)
                    self.setGTMAuthorization(authorization: authorization)
                    print("Got authorization access token: \(authState?.lastTokenResponse?.accessToken)")
                } else {
                    //self.setGTMAuthorization(authorization: nil)
                    print("Authorization error: \(error?.localizedDescription)")
                }
            })
        }
    }
    
    func setGTMAuthorization(authorization: GTMAppAuthFetcherAuthorization) {
        if appAuthorization.isEqual(authorization) {
            return
        }
        appAuthorization = authorization
        self.stateChanged()
    }
    
    func stateChanged() {
        self.saveState()
        self.updateUI()
    }
    
    func saveState() {
        if appAuthorization.canAuthorize() {
            GTMAppAuthFetcherAuthorization.save(appAuthorization, toKeychainForName: kExampleAuthorizerKey)
        } else {
            GTMAppAuthFetcherAuthorization.removeFromKeychain(forName: kExampleAuthorizerKey)
        }
    }

But app is crashing(as title says) while view load. BTW my viewDidLoad() is empty.

I have put exception breakpoint and it stops here:
GTM_UNAVAILABLE_USE_INITIALIZER(@selector(initWithAuthState:));

Please help regarding this issue.

@WilliamDenniss
Copy link
Collaborator

I think:
let authorization = GTMAppAuthFetcherAuthorization.init(authState: authState!)

Should be:
let authorization = GTMAppAuthFetcherAuthorization. initWithAuthState(authState: authState!)

That exception is thrown when the default initializer is called.

@grantkemp
Copy link

I managed to get it working with my sample project for Swift/ macOs - if this is still an issue- then please see the code there. I think its down to how you handle your URL pickup.

Pull request is #60

@xaphod
Copy link

xaphod commented Feb 20, 2019

This issue should track the fact that because of the way the initializers are declared, Swift does not know that GTMAppAuthFetcherAuthorization.init() is an invalid initializer. So you can compile code that will always crash. That's not right, it should be marked as unavailable to swift or whatever pragma you like, in order that GTMAppAuthFetcherAuthorization.init() does not compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants