2015-08-15 83 views
1

我有一個獲取SID的應用內登錄頁面。我如何將這些登錄憑據傳遞到Swift中顯示的UIWebView?將登錄憑據傳遞到UIWebView

編輯:我想我只是能夠加載到我的網絡視圖與SID在Web地址,但我得到一個錯誤這樣做。

+0

你得到了什麼錯誤?另外,當你說「在網址中使用SID」時,你如何將它放在網址中? –

回答

5

假設您在UIWebView中顯示網站(例如www.foo.com),並且希望將SID傳遞給UIWebView以登錄www.foo.com。

您需要設置一個NSMutableURLRequest,使用HTMLBodyHTTPMethod正確設置以實現此目的。通過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)