In the network programming, the asynchronous request is the most request way, compared with the synchronization, does not cause the user interface the card to die, will have the good user experience. Now we use Swift to get the HTML source code for a webpage in an iOS project.
Override Func Viewdidload () { super.viewdidload () //Asynchronous Request Nsurlconnection.sendasynchronousrequest ( Nsurlrequest (Url:nsurl (string: "http://www.baidu.com"), Queue:nsoperationqueue ()) { (resp:nsurlresponse!, data:nsdata!, error:nserror!) -Void in//closure function println (NSString (Data:data, encoding:nsutf8stringencoding)!) Convert the acquired binary data into HTML for output;} }
Run the program to achieve the following effects:
。
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS project development--get web page HTML source code using asynchronous request