我在我的智慧结束,我已经在这方面工作了好几天似乎解决了这个问题。我对编码世界非常感兴趣,所以我确信这是一个新手的错误,但我不能再浪费时间了,这已经差不多一周了。
我在XCode 8.3,Swift 3中写作。我正在关注Auth0网站上的教程。在我输入下面粘贴的代码之前,我的应用程序会编译,但它不起作用。 Auth0说我正在修改这段代码。
我收到以下错误:使用未解析的标识符'accessToken'我没有其他错误或红色!但这不会消失。我试过移动代码,这是行不通的。我试过let,否则,if语句,这是行不通的。我花了很长时间用谷歌搜索谷歌,但我似乎无法理解我做错了什么。
以下是代码:
import UIKit
import Lock
import Auth0
import SimpleKeychain
class HomeViewController: UIViewController {
// MARK: - IBAction
@IBAction func showLoginController(_ sender: UIButton) {
Lock
.classic()
.withOptions {
$0.oidcConformant = true
$0.scope = "openid profile"
}
.onAuth { credentials in
let keychain = A0SimpleKeychain(service: "Auth0")
guard let accessToken = credentials.accessToken else { return }
keychain.setString(accessToken, forKey: "access_token")
self.showSuccessAlert(accessToken)
}
.present(from: self)
Auth0
.authentication()
.userInfo(withAccessToken: accessToken)
.start { result in
switch(result) {
case .sucess(let profile): break
case .failure(let error): break
}
}
Auth0
.webAuth()
.scope("openid profile")
.audience("https://mycompany.auth0.com/userinfo")
.start {
switch $0 {
case .failure(let error):
// Handle the error
print("Error: \(error)")
case .success(let credentials):
// Do something with credentials e.g.: save them.
// Auth0 will automatically dismiss the hosted login page
print("Credentials: \(credentials)")
}
}
Auth0
.webAuth()
.scope("openid profile offline_access")
.start {
switch $0 {
case .failure(let error):
// Handle the error
print("Error: \(error)")
case .success(let credentials):
guard let accessToken = credentials.accessToken, let refreshToken = credentials.refreshToken else { return }
let keychain = A0SimpleKeychain(service: "Auth0")
keychain.setString(accessToken, forKey: "access_token")
keychain.setString(refreshToken, forKey: "refresh_token")
}
}
}
// MARK: - Private
fileprivate func showSuccessAlert(_ accessToken: String) {
let alert = UIAlertController(title: "Success", message: "accessToken: \(accessToken)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
调试Swift代码的策略:
祝你项目的其余部分好运!