如何使用 uiwebview 快速登录网站?

How to log into web site using uiwebview with swift?(如何使用 uiwebview 快速登录网站?)

本文介绍了如何使用 uiwebview 快速登录网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试在 ios 应用程序中使用 UIWebView 登录网页.但是,我不知道该怎么做.我正在显示带有

I want to try to log into a web page using UIWebView in an ios applicaiton. However, I do not know how can I do this. I am showing web site with

let url = NSURL(string: "http://m.falalem.com")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)

我想使用我的用户名和密码参数登录此页面.登录页面 -> http://m.falalem.com/Login登录页面后 -> http://m.falalem.com/User-Info

I want to log into this page using my username and password parameter. Login Page -> http://m.falalem.com/Login After login page -> http://m.falalem.com/User-Info

如何使用uiwebview登录这个页面??请帮忙!

How can log into this page using uiwebview?? Please help!

推荐答案

一般来说登录网页(使用JS、PHP等)使用POST请求,对吧?

Logging into a webpage in general (using JS, PHP, etc.) uses a POST request, right?

特别是在您的网站上,对于密码为 world 的用户 helloPOST 如下所示:

Specifically on your website, for the user hello with the password world the POST looks like the following:

http://m.falalem.com/System/Falalem?LoginUser=c7e28a618886dba19eac0892ad90cf51&RTrn=http%3A%2F%2Fm.falalem.com%2FUser-Info&EPostam=hello&Sifrem=world

您的特定请求标头在哪里:

where your specific request headers are:

LoginUser: c7e28a618886dba19eac0892ad90cf51
RTrn: http://m.falalem.com/User-Info
EPostam (user): hello
Sifrem (password): world

同样,在 Swift 中,您将加载具有类似请求的 UIWebView.大概,您从 UITextFields 接收用户名和密码,并将使用它们登录到 UIWebView:

Likewise, in Swift, you're going to load a UIWebView with a similar request. Presumably, you are receiving the username and password from UITextFields and are going to use them to login to a UIWebView:

@IBOutlet weak var usernameTextField: UITextField!

// Note: you will want to secure this either by checking the box 
// in Attributes Inspector or programmatically using 
// `passwordTextField.secureTextEntry = true`
@IBOutlet weak var passwordTextField: UITextField!

@IBOutlet weak var webView: UIWebView!

...

// fire when the UIButton for "sign in" or "Giriş Yap" is pressed
@IBAction func signIn(sender: AnyObject) {
    var url = NSURL(string: "http://m.falalem.com/System/Falalem")

    // Note that you will want to use a `NSMutableURLRequest` 
    // because otherwise, you will not be able to edit the `request.HTTPMethod`
    var request = NSMutableURLRequest(URL: url!)

    // You need to specifically tell the request what HTTP method to use
    // (e.g., GET, POST, PUT, DELETE)
    request.HTTPMethod = "POST"

    // Is this generated/an API key? If generated per user, use `var`
    let loginUser: String = "c7e28a618886dba19eac0892ad90cf51"

    // The page you want to go to after login
    let redirectPage: String = "http://m.falalem.com/User-Info"

    // Get the user's credentials from the `UITextField`s
    var username: String = usernameTextField.text
    var password: String = passwordTextField.text

    // Concatenating everything together
    var userCredentials: String = "EPostam=(username)&Sifrem=(password)"
    var bodyData: String = "(userCredentials)&RTrn=(redirectPage)&LoginUser=(loginUser)"

    // Encode the bodyData string into a 
    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)

    // load the UIWebView `webView` with this request
    webView.loadRequest(request)
}

现在,如果您没有通过 UITextFields 获取用户的凭据,您可以简单地继续

Now, if you're not getting the user's credentials via UITextFields, you can simply proceed with

let url = NSURL(string: "http://m.falalem.com")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)

因为用户随后只需通过移动网站输入他/她的凭据并以这种方式重定向.

because the user will then just input his/her credentials via the mobile website and be redirected that way.

最后,您可能希望使用 SSL,否则您会以纯文本形式发送密码(或哈希),这是不安全的.

Lastly, you'll probably want to use SSL because otherwise you're sending the password (or the hash) over as plain text, which is not secure.

这篇关于如何使用 uiwebview 快速登录网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 uiwebview 快速登录网站?

基础教程推荐