ios - Swift URL查询字符串获取参数

标签 ios xcode swift

我正在使用 OAuthSwift 库对我的应用程序的用户进行身份验证,但某些东西似乎不起作用,因为它会抛出错误。经过一些调试后,这一行似乎出错了:parameters = url.query!.parametersFromQueryString()

它确实有 url 查询字符串(设置为 url.query),但它以某种方式无法解析 parametersFromQueryString()。其他人有这个问题(这个库)吗?你是如何解决的?

“parametersFromQueryString”函数可以在这里找到:https://github.com/dongri/OAuthSwift/blob/1babc0f465144411c0dd9721271f239685ce83a9/OAuthSwift/String%2BOAuthSwift.swift

最佳答案

创建 URL 类的扩展并粘贴此代码:

import Foundation

extension URL {
public var parametersFromQueryString : [String: String]? {
    guard let components = URLComponents(url: self, resolvingAgainstBaseURL: true),
        let queryItems = components.queryItems else { return nil }
    return queryItems.reduce(into: [String: String]()) { (result, item) in
        result[item.name] = item.value
    }
}}

现在您可以从 URL 对象中获取这个计算属性的值,只需使用:

url.parametersFromQueryString

关于ios - Swift URL查询字符串获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31974558/

相关文章:

ios - 在 Xcode 中,委托(delegate)对象如何知道要调用委托(delegate)对象的哪个方法?

来自设备上部署的应用程序的 iOS os.log

ios - Swift - Mailgun - 未收到电子邮件

ios - 根据用户航向更新 iOS map

ios - Graph API 中的 Facebook 搜索

ios - 是否可以移动 UIPageControl 点

ios - 如何为 appcelerator 创建自定义启动屏幕

javascript - 如何快速获取html文本输入值和输出

ios - 应用程序未通过代码设计验证。签名无效,或者未使用 Apple 提交证书签名。 (-19011)

ios - 弹出窗口和 UIAlertController 错误