1

I have a small script written in python and works fine so i want to implement the same thing in my swift app but could not find a way

here is my python code:

payload = {'login': 'Submit', 'username': 'username', 'password': 'password', 'redirect': 'iph.php'}
with session() as c:
    c.post('http://mywebsite.com/iph.php', data=payload)
    response = c.get('http://mywebsite.com/mobile.php?page=profile')
    tree = html.fromstring(response.content)
    males = tree.xpath('//td[@id="someinfo"]/text()')
    females = tree.xpath('//td[@id="someinfo2"]/text()')
    print(response.headers)
    print(response.text)

So basically i receive 2 text infos from the script (someinfo and someinfo2) and in swift app i need to print them on labels.

Thank you

ggnoredo
  • 801
  • 1
  • 13
  • 33
  • 1
    Two methods that come to mind. NSSession & NSStream; investigate, try something and come back if you need more help? – user3069232 Mar 15 '16 at 11:05

1 Answers1

2

Use a third-party framework like Alamofire:

let payload = ["login": "Submit", "username": "username", "password": "password", "redirect": "iph.php"]

Alamofire.request(.POST, "http://mywebsite.com/iph.php", parameters: payload)
    .response { request, response, data, error in
        guard let data = data else {
            fatalError("No data returned")
        }
        do {
            let html = try NSXMLDocument(data: data, options: NSXMLDocumentTidyHTML)
            let root = html.rootElement()!
            let males = try root.nodesForXPath("//td[@id='someinfo']/text()")
            let females = try root.nodesForXPath("//td[@id='someinfo2']/text()")
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }

Use only Foundation (i.e. Apple-provided) classes:

let payload = ["login": "Submit", "username": "username", "password": "password", "redirect": "iph.php"]
let request = NSMutableURLRequest(URL: NSURL(string: "http://mywebsite.com/iph.php")!)
request.HTTPMethod = "POST"
request.HTTPBody = NSKeyedArchiver.archivedDataWithRootObject(payload)

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request) { data, response, error in
    guard let data = data else {
        fatalError("No data returned")
    }
    do {
        let html = try NSXMLDocument(data: data, options: NSXMLDocumentTidyHTML)
        let root = html.rootElement()!
        let males = try root.nodesForXPath("//td[@id='someinfo']/text()")
        let females = try root.nodesForXPath("//td[@id='someinfo2']/text()")
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

task.resume()

Notes

  • Since your request is transported over unsecured HTTP, you may need to set App Transport Security to allow it.
  • You can't use Alamofire in a console application, or in a playground.
  • Both methods execute asynchronously. You must keep the main queue going while waiting for the response. In a GUI application, the run loop will keep your app going so it's not a concern. For a console application, you can pause the main queue by calling sleep(secs) or use Grand Central Dispatch to wait for the request.
Community
  • 1
  • 1
Code Different
  • 90,614
  • 16
  • 144
  • 163