iOS專案開發實戰——iOS網路程式設計獲取網頁Html原始碼
如今我們身處網際網路的時代,任何一個軟體或是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 。歡迎大家訪問!
相關文章
- iOS專案開發實戰——使用非同步請求獲取網頁HTML原始碼iOS非同步網頁HTML原始碼
- iOS專案開發實戰——使用同步請求獲取網頁原始碼iOS網頁原始碼
- iOS專案開發實戰——獲取網頁原始碼的二進位制資料iOS網頁原始碼
- iOS專案開發實戰——網頁原始碼實現二進位制和HTML的轉換iOS網頁原始碼HTML
- iOS專案開發實戰——使用程式碼獲取螢幕寬高iOS
- iOS專案開發實戰——獲取本機IP地址iOS
- iOS專案開發實戰——使用程式碼實現頁面跳轉iOS
- iOS專案開發實戰——獲取系統當前時間iOS
- iOS專案開發實戰——使用CoreLocation獲取當前位置資訊iOS
- 【iOS開發】獲取區域網IPiOS
- iOS專案開發實戰——獲取當前的UUID(通用唯一識別碼)iOSUI
- QWebView獲取網頁原始碼WebView網頁原始碼
- ios UIWebView 載入網頁、檔案、 htmliOSUIWebView網頁HTML
- 高效獲取網頁原始碼COM網頁原始碼
- iOS開發那些事-iOS網路程式設計同步GET方法請求程式設計iOS程式設計
- iOS開發那些事-iOS網路程式設計非同步GET方法請求程式設計iOS程式設計非同步
- 淺談設計模式在iOS開發實戰專案中的應用設計模式iOS
- iOS專案開發實戰——使用CoreLocation實現定位iOS
- iOS專案開發實戰——理解frame,bounds,centeriOS
- iOS專案開發實戰——檢視動畫效果iOS動畫
- iOS專案開發實戰——配置自定義動畫iOS動畫
- iOS專案開發實戰——plist陣列解析iOS陣列
- 網頁設計中常用的HTML程式碼網頁HTML
- iOS開發 面向切面程式設計之 Aspects 原始碼解析iOS程式設計原始碼
- 專業建站網站 網站建設 網站開發 官網開發 網頁設計 網頁網站網頁
- iOS 開發工具網頁下載iOS網頁
- iOS專案開發實戰——CoreLocation地理編碼和反地理編碼iOS
- iOS開發之網路篇iOS
- HarmonyOS NEXT應用開發之Axios獲取解析網路資料iOS
- iOS專案開發實戰——使用UICollectionView實現瀑布流iOSUIView
- IOS遍歷網頁獲取網頁中<img>標籤中的圖片urliOS網頁
- iOS專案開發實戰——storyboard設定介面技巧與注意事項iOS
- iOS專案開發實戰——實現檢視切換動畫iOS動畫
- Delphi自動提交網頁表單和獲取框架網頁原始碼網頁框架原始碼
- iOS專案開發實戰——UIView的層級關係iOSUIView
- iOS專案開發實戰——如何進行延時操作iOS
- Python大型網路爬蟲專案開發實戰(全套)Python爬蟲
- 獲取含跨域網址的框架網頁的原始碼跨域框架網頁原始碼