将登录凭据传递到UIWebView

Tam*_*isk 1 uiwebview ios swift

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

编辑:我以为我只能用网址中的SID将URL加载到我的webview中,但是这样做会出错.

Mic*_*nas 5

据推测,您正在显示一个网站(例如,www.foo.com),UIWebView并希望将SID传递UIWebView给登录www.foo.com.

您将要建立一个NSMutableURLRequestHTMLBodyHTTPMethod正确设置来实现这一目标.将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)
Run Code Online (Sandbox Code Playgroud)