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)
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
How can log into this page using uiwebview?? Please help!
Logging into a webpage in general (using JS, PHP, etc.) uses a POST
request, right?
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
Likewise, in Swift, you're going to load a UIWebView
with a similar request. Presumably, you are receiving the username and password from UITextField
s 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)
}
Now, if you're not getting the user's credentials via UITextField
s, 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With