google / google/GTMAppAuth

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

Open
#20 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
448
Forks
247
Avg merge
2d 8h
Merged PRs (30d)
5

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.