iOS project development--Get binary data of Web page source code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.