Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire Swift get html source

I just want to retrieve the html source from a simple website.

@IBAction func scan_func(sender: AnyObject) {
        Alamofire.request(.GET, "http://www.example.com")
            .response { request, response, data, error in
                print(request)
                print(response)
                print(data)
        }
    }

I already have successfully added "App Transport Security Settings" and "Allow Arbitrary Loads" to info.plist to load http content.

When I run that code I only get an output like this: XCODE - hexadecimal output I hope you can help me.

kind regards

like image 947
Alexander J Avatar asked Dec 10 '22 15:12

Alexander J


1 Answers

You are printing out the raw bytes of the data. I'm guessing you are looking for a way to print out the corresponding string representation. You could for instance do this with Alamofire's provided closure:

Alamofire.request(.GET, "http://www.example.com")
    .responseString { response in
        print("Response String: \(response.result.value)")
}

(Check out the documentation here)

Alternatively, you could convert the string yourself:

Alamofire.request(.GET, "http://www.example.com")
    .response { request, response, data, error in
        print(String(data: data, encoding: String.Encoding.utf8))
}

Apple String reference documentation

like image 65
Jonas W Avatar answered Dec 24 '22 12:12

Jonas W