Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // ASPTT
- //
- // Created by Yacine MESBAT on 12/04/16.
- // Copyright © 2016 Yacine MESBAT. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet var login: UITextField!
- @IBOutlet weak var pass: UITextField!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- @IBAction func ChangeView(sender: UIButton) {
- self.performSegueWithIdentifier("ConnexionSegue", sender: self)
- }
- @IBAction func FromConnexionToMain(sender: UIButton) {
- self.performSegueWithIdentifier("BackToMain", sender: self)
- }
- @IBAction func MainToUp(sender: UIButton) {
- self.performSegueWithIdentifier("SignUpSegue", sender: self)
- }
- @IBAction func Connect(sender: AnyObject) {
- self.recup_info()
- }
- func recup_info()
- {
- let username:NSString = login.text!
- let password:NSString = pass.text!
- if (username.isEqualToString("") || password.isEqualToString("")) {
- self.show_alert_message("Les champs sont vides")
- }
- else
- {
- // let request = NSMutableURLRequest(URL: NSURL(string: "https://www.a-j-evolution.com/tutos/exemples/ioslogin/login.php")!)
- let myUrl = NSURL(string: "http://10.104.3.214/codecamps/membres/connexion.php");
- let request = NSMutableURLRequest(URL:myUrl!);
- request.HTTPMethod = "POST";
- // Compose a query string
- let postString = "email=\(username)&mot_de_passe=\(password)"
- let postData:NSData = postString.dataUsingEncoding(NSASCIIStringEncoding)!
- let postLength:NSString = String( postData.length )
- request.HTTPBody = postData
- request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
- request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
- request.setValue("application/json", forHTTPHeaderField: "Accept")
- NSLog(postLength as String)
- let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
- {
- data, response, error in
- print (error)
- print (response)
- if error != nil
- {
- // print("error=\(error)")
- dispatch_async(dispatch_get_main_queue(), {
- self.show_alert_message("Veuillez vous connecter à internet")
- })
- return
- }
- var myJSON:NSDictionary = NSDictionary()
- do {
- myJSON = try NSJSONSerialization.JSONObjectWithData(data!,options: .MutableLeaves) as! NSDictionary
- }catch { // handle error
- dispatch_async(dispatch_get_main_queue(), {
- self.show_alert_message("Erreur de récupération des données")
- })
- print ("Erreur de traitement de JSON")
- return
- }
- let success:NSInteger = myJSON.valueForKey("error") as! NSInteger
- if (success == 1)
- {
- dispatch_async(dispatch_get_main_queue(), {
- self.show_alert_message("Vos identifiants ne correspondent pas")
- })
- }
- else{
- print("ok login")
- }
- }
- task.resume()
- }
- }
- func show_alert_message(text: String) {
- var alert: UIAlertController!
- alert = UIAlertController(title: "Info", message: text, preferredStyle: UIAlertControllerStyle.Alert)
- let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
- alert.addAction(action)
- presentViewController(alert, animated: true, completion: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement