gpt4 book ai didi

ios - 将登录凭据传递到 UIWebView

转载 作者:搜寻专家 更新时间:2023-11-01 06:08:10 26 4
gpt4 key购买 nike

我有一个获取 SID 的应用内登录页面。我如何将这些登录凭据传递到 Swift 中显示的 UIWebView?

编辑:我原以为我可以使用网址中的 SID 将 URL 加载到我的 webview 中,但我在执行此操作时遇到错误。

最佳答案

假设您正在 UIWebView 中显示一个网站(例如 www.foo.com),并希望将 SID 传递给 UIWebView 以登录 www。 foo.com.

您需要使用 HTMLBodyHTTPMethod 设置一个 NSMutableURLRequest 来实现这一点。将 SID 传递给登录的方法是通过查询参数。

var url = NSURL(string: "http://www.foo.com")

var request = NSMutableURLRequest(URL: url!)

// usually to login you send a POST with the user's credentials
request.HTTPMethod = "POST"

// this is where you would assign the SID (from a UITextView, etc.)
var sid = "1234" // getSid()

var bodyData = "SID=\(sid)"

// encode the query params as UTF8
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)

// this will load the UIWebView by sending the POST with the query string:
// http://www.foo.com/?SID=1234
webView.loadRequest(request)

关于ios - 将登录凭据传递到 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32020266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com