Swift -AWS身份验证提取 - 发挥全球功能
我在应用程序上使用AWS身份验证。而且我想创建一个全局函数,以获取用户令牌,以便每当需要获取新ID令牌时,我都可以在应用程序中的任何地方访问它,代币每5分钟都会更改一次,因此我需要不时调用它。我这样做了
func fetchSession() {
Amplify.Auth.fetchAuthSession { result in
do {
let session = try result.get()
// Get cognito user pool token
if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
let tokens = try cognitoTokenProvider.getCognitoTokens().get()
return tokens.idToken
}
} catch {
print("Fetch auth session failed with error - \(error)")
}
}
}
,但是我遇到了这个错误,
Unexpected non-void return value in void function
我也尝试这样做
func fetchSession() {
let token = Amplify.Auth.fetchAuthSession { result -> AnyObject in
do {
let session = try result.get()
// Get cognito user pool token
if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
let tokens = try cognitoTokenProvider.getCognitoTokens().get()
return tokens.idToken
}
} catch {
print("Fetch auth session failed with error - \(error)")
}
}
}
,这是我得到的错误
Cannot convert value of type '(AmplifyOperation<AuthFetchSessionRequest, AuthSession, AuthError>.OperationResult) -> AnyObject' (aka '(Result<AuthSession, AuthError>) -> AnyObject') to expected argument type '((AmplifyOperation<AuthFetchSessionRequest, AuthSession, AuthError>.OperationResult) -> Void)?' (aka 'Optional<(Result<AuthSession, AuthError>) -> ()>')
I am using AWS authentication on my app. And I want to create a global function for fetching the user token so I can access it anywhere in the app whenever I need to fetch the new id token, the token is changing every 5 mins so I need to call it from time to time. I do it like this
func fetchSession() {
Amplify.Auth.fetchAuthSession { result in
do {
let session = try result.get()
// Get cognito user pool token
if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
let tokens = try cognitoTokenProvider.getCognitoTokens().get()
return tokens.idToken
}
} catch {
print("Fetch auth session failed with error - \(error)")
}
}
}
but I am getting this error
Unexpected non-void return value in void function
I also try to make it like this
func fetchSession() {
let token = Amplify.Auth.fetchAuthSession { result -> AnyObject in
do {
let session = try result.get()
// Get cognito user pool token
if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
let tokens = try cognitoTokenProvider.getCognitoTokens().get()
return tokens.idToken
}
} catch {
print("Fetch auth session failed with error - \(error)")
}
}
}
and this is the error I got
Cannot convert value of type '(AmplifyOperation<AuthFetchSessionRequest, AuthSession, AuthError>.OperationResult) -> AnyObject' (aka '(Result<AuthSession, AuthError>) -> AnyObject') to expected argument type '((AmplifyOperation<AuthFetchSessionRequest, AuthSession, AuthError>.OperationResult) -> Void)?' (aka 'Optional<(Result<AuthSession, AuthError>) -> ()>')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要指定什么
fetchsession
将返回。现在,您说的是它将返回void
,因为您已经忽略了返回类型。这是指定返回类型的示例。
不要忘记在捕获量中退还零或重新误差错误。
Swift Docs on functions: https://docs.swift.orgg/swift.org/swift.orgbook /languageGuide/functions.html
Looks like you need to specify what
fetchSession
will return. Right now you are saying it will returnVoid
because you have neglected the return type.Here is an example of specifying the returned type.
Do not forgot to return nil in the catch or rethrow the error.
Swift docs on functions: https://docs.swift.org/swift-book/LanguageGuide/Functions.html