iOS项目开发实战——iOS网络编程获取网页Html源代码
来源:互联网 发布:linux 压缩文件夹 命令 编辑:程序博客网 时间:2024/05/22 06:53
如今我们身处互联网的时代,任何一个软件或是App,都会或多或少与网络打交道,并不断发生数据交互。一个没有涉及网络编程的应用会显得比较low,这里我们将会开始使用Swift开发iOS应用,并且主要来实现网络操作方面的功能。
这里的需求是获取某个网页的Html源代码,即从网上获取数据。具体实现如下:
(1)创建一个iOS项目,Language选择Swift。然后在ViewController.swift中实现如下代码:
override func viewDidLoad() { super.viewDidLoad() var str = NSString(contentsOfURL: NSURL(string: "http://www.baidu.com")!, encoding: NSUTF8StringEncoding, error: nil) println(str!)//上述返回的是Optional Type可选值,返回值有可能为空,在我确保有返回值的情况下,使用感叹号!获取该值; }
(2)运行程序,在控制台打印结果:(百度主页Html内容太多,我只复制出一部分)
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <meta content="always" name="referrer"> <meta name="theme-color" content="#2932e1"> <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"> <link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索"> <link rel="icon" sizes="any" mask="" href="//www.baidu.com/img/baidu.svg"> <link rel="dns-prefetch" href="//s1.bdstatic.com"> <link rel="dns-prefetch" href="//t1.baidu.com"> <link rel="dns-prefetch" href="//t2.baidu.com"> <link rel="dns-prefetch" href="//t3.baidu.com"> <link rel="dns-prefetch" href="//t10.baidu.com"> <link rel="dns-prefetch" href="//t11.baidu.com"> <link rel="dns-prefetch" href="//t12.baidu.com"> <link rel="dns-prefetch" href="//b1.bdstatic.com"> <title> 百度一下,你就知道 </title> <style index="index" id="css_index" type="text/css">html,body{height:100%}html{overflow-y:auto}#wrapper{position:relative;_position:;min-height:100%}#head{padding-bottom:100px; text-align:center;*z-index:1}#ftCon{height:100px;position:absolute;bottom:44px;text-align:center;width:100%;margin:0 auto;z-index:0;overflow:hidden}#ftConw{width:720px;margin:0 auto}body{font:12px arial;text-align:;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}.bg{background-image:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_45de3f02.png);background-repeat:no-repeat;_background-image:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_15f748ea.gif)}.bg_tuiguang_browser{width:16px;height:16px;background-position:-600px 0;display:inline-block;vertical-align:text-bottom;font-style:normal;overflow:hidden;margin-right:5px}.bg_tuiguang_browser_big{width:56px;height:56px;position:absolute;left:10px;top:10px;background-position:-600px -24px} .bg_tuiguang_weishi{width:56px;height:56px;position:absolute;left:10px;top:10px;background-position:-672px -24px}.c-icon{display:inline-block;width:14px;height:14px;vertical-align:text-bottom;font-style normal;overflow:hidden;background:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_45de3f02.png) no-repeat 0 0;_background-image:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_15f748ea.gif)}.c-icon-triangle-down-blue{background-position:-480px -168px}.c-icon-chevron-unfold2{background-position:-504px -168px}#m{width:720px;margin:0 auto}#nv a,#nv b,.btn,#lk{font-size:14px}input{border:0;padding:0}#nv{height:19px;font-size:16px;margin:0 0 4px;text-align:left;text-indent:137px}.s_btn{width:95px;height:32px;padding-top:2px\9;font-size:14px;background-color:#ddd;background-position:0 -48px;cursor:pointer}.s_btn_h{background-position:-240px -48px}.s_btn_wr{width:97px;height:34px;display:inline-block;background-position:-120px -48px;*position:relative;z-index:0;vertical-align:top} </style> </head> <body> </body></html>
通过以上代码,我们就能从网页上成功获取源代码。但是由于我在上述注释中关于可选型的问题,我决定优化一下代码,就算网络数据访问不成功或者出现为空有异常等等情况,也能反馈给用户一个提示,优化代码如下,注意对Optional Type为空的操作。
override func viewDidLoad() { super.viewDidLoad() var strHTML = NSString(contentsOfURL: NSURL(string: "111111")!, encoding: NSUTF8StringEncoding, error: nil) if let print = strHTML{ println(strHTML!) }else{ println("未能获取网络数据") } }
运行以上代码,就能返回”未能获取网络数据“的提示了。就算网络有异常系统也不会崩溃。
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
2 0
- iOS项目开发实战——iOS网络编程获取网页Html源代码
- iOS项目开发实战——使用异步请求获取网页HTML源代码
- iOS项目开发实战——网页源代码实现二进制和HTML的转换
- iOS项目开发实战——获取网页源代码的二进制数据
- iOS项目开发实战——使用同步请求获取网页源代码
- iOS项目开发实战——获取系统当前时间
- iOS 获取当前网页html
- iOS项目开发实战——使用CoreLocation获取当前位置信息
- iOS项目开发实战——获取本机IP地址
- iOS项目开发实战——获取当前的UUID(通用唯一识别码)
- iOS项目开发实战——使用代码获取屏幕宽高
- IOS开发——网络编程总汇
- iOS开发——网络编程
- iOS开发网络篇—Socket编程
- iOS开发网络篇—Socket编程
- iOS项目开发实战(Swift)—项目目录和结构
- ios开发---网络编程
- iOS项目开发实战——实现视图切换动画
- HDU 5372 Segment Game
- java微信开发自定义菜单
- Download Manager (HDU3233,UVA12231,UVALive4486) 解题报告
- Java 多线程实现的两种方式
- HDU5375 Gray code
- iOS项目开发实战——iOS网络编程获取网页Html源代码
- 带分区的UITableView的创建(省市区字典)
- 窗口与视图UIScreen and UIView
- (3.1.4.2)struts1和struts2
- HDU 5373 The shortest problem
- 修改Android studio 中svn版本
- android ButterKnife
- threading.Timer
- java微信开发发送消息