In my previous blog, "iOS Project development Combat--ios Web Programming Web page HTML source code " describes how to get a Web page HTML source code, can meet certain needs. But for special reasons, we want to get a binary data of a Web page, so what should we do?
The specific implementation is as follows:
(1) Create an iOS project, language select Swift, and write the following code in Viewcontroller.swift:
Override Func Viewdidload () { super.viewdidload () var data = NSData (Contentsofurl:nsurl (string: "/HTTP// Www.baidu.com ")!) println (data!) }
(2) Run the program, the result is as follows, can successfully obtain the Web page binary data:
。
(3) optimization is now available for situations where the return may be empty, and the optimization code is as follows:
Override Func Viewdidload () { super.viewdidload () var data = NSData (Contentsofurl:nsurl (string: "FDSVSDFGVDF" )!) If let BinaryData = data{ println (data!) } else{ println ("Failed to get Network data")} }
The output at this point is "failed to get network data". Prevents a crash when a system network request fails.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS project development--Get binary data of Web page source code