将登录凭据传递到UIWebView
问题描述:
我有一个获取SID的应用内登录页面。我如何将这些登录凭据传递到Swift中显示的UIWebView?将登录凭据传递到UIWebView
编辑:我想我只是能够加载到我的网络视图与SID在Web地址,但我得到一个错误这样做。
答
假设您在UIWebView
中显示网站(例如www.foo.com),并且希望将SID传递给UIWebView
以登录www.foo.com。
您需要设置一个NSMutableURLRequest
,使用HTMLBody
和HTTPMethod
正确设置以实现此目的。通过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)
你得到了什么错误?另外,当你说“在网址中使用SID”时,你如何将它放在网址中? –