gpt4 book ai didi

ios - 如何使用 uiwebview 和 swift 登录网站?

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:14 26 4
gpt4 key购买 nike

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

显示网站
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

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

最佳答案

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

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

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

您的特定请求 header 在哪里:

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

同样,在 Swift 中,您将使用类似的请求加载 UIWebView。据推测,您正在从 UITextField 接收用户名和密码,并打算使用它们登录到 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)
}

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

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

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

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

关于ios - 如何使用 uiwebview 和 swift 登录网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30653480/

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