SlideShare una empresa de Scribd logo
1 de 53
Network Service http://MobileDev.TW
iOS APP Developer
網路服務 – 資料傳送與接收
Ryan@MobileDev.TW
1
Network Service http://MobileDev.TW
大綱
1. 基本網路傳輸方式
• 取得一般字串資料
• 取得圖片資料
2. 網路常見資料格式一:JSON
• NSJSONSerialization
3. 網路常見資料格式二:XML
• XML Parser
• XML與Master-Detail ViewController
4. 資料來源與介接
• Open Data API
• 自己提供資料
2
Network Service http://MobileDev.TW
取得網路資料
• 透過URLSession進行一項data task,取得網路資料
3
Network Service http://MobileDev.TW
取得網路資料
1.在viewDidLoad中,加入以下內容
4
Network Service http://MobileDev.TW
取得網路資料
2.在Info.plist中加入:
5
Network Service http://MobileDev.TW
取得網路圖片
• 取得網路上的一張圖片,顯示在畫
面上
6
Network Service http://MobileDev.TW
Lab 取得網路圖片
1. storyboard加入imageView,建立關聯性(Outlet)
2. 修改viewDidLoad的內容
7
Network Service http://MobileDev.TW
PS.避免圖像比例失真
• ScaleToFill
• 完全滿版(可能比例會失真)
• ScaleAspectFit
• 維持影像比例與完整圖,其他顯示區域以透明顯示
• ScaleAspectFill
• 滿版+維持影像比例(但部分圖像內容可能會截掉)
8
Network Service http://MobileDev.TW
增加等待顯示
• Activity Indicator
• Button
9
Network Service http://MobileDev.TW
非同步傳輸
1.在畫面上增加UIActivityIndicatorView,勾選Hides
When Stopped,並建立程式關聯性
放置於畫面的正中央
10
Network Service http://MobileDev.TW
非同步傳輸
按鈕按下後,設定Indicator,開始運轉直到取得資料
11
Network Service http://MobileDev.TW
URL Session Class
12
Network Service http://MobileDev.TW
取得下載進度
1.畫面上加入progressView
2.設定session的Delegate為self,並改用
downloadTask
13
Network Service http://MobileDev.TW
取得下載進度
3.遵循相關delegate
4.實作doanloadTask這兩個方法
14
Network Service http://MobileDev.TW
取得下載進度
5.完成下載時,設定圖片以及讓indicator停止
15
Network Service http://MobileDev.TW
取得下載進度
6.取得目前下載進度,並顯示至ProgressView
16
Network Service http://MobileDev.TW
2.網路常見資料格式一:JSON
這誰?
17
Network Service http://MobileDev.TW
台北市立動物園資料
18
Network Service http://MobileDev.TW
資料來源
http://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=a3
e2b221-75e0-45c1-8f97-75acbd43d613
19
Network Service http://MobileDev.TW
單機 -> 網路
1.使用Master-Detail樣板,結合上例的資料傳輸,建立一個陣列來儲存資料
20
Network Service http://MobileDev.TW
單機 -> 網路
2.取得資料後,使用序列化,存入陣列,並重新整理
21
Network Service http://MobileDev.TW
單機 -> 網路
3.設定tableView的顯示與資料陣列的關聯性:
• 資料筆數:依據陣列元素數量
• 項目文字:依據陣列每間房間裡頭用A_Name_Ch對應的值
22
Network Service http://MobileDev.TW
Lab
• 找到圖片的資料來源,設定好第二頁可以呈現動物的圖
片
• 有值與無值該如何處理?
23
Network Service http://MobileDev.TW
在第二個畫面中加入預設圖片
Storyboard -> add imageView -> 建立Outlet
24
Network Service http://MobileDev.TW
第二個畫面中增加一個變數
thisAnimalDic
25
Network Service http://MobileDev.TW
修改第一個畫面中的prepareForSegue
controller.thisAnimalDic=object
26
Network Service http://MobileDev.TW
修改第二個畫面的viewDidLoad
有圖片網址的話,就去請求內容
27
Network Service http://MobileDev.TW
下載完成後,設定圖片
28
Network Service http://MobileDev.TW
3.處理XML資料
29
Network Service http://MobileDev.TW
處理XML資料
30
Network Service http://MobileDev.TW
處理XML資料
arrayResultObjects
NewsItemObject
stringTitle
stringLink
stringPubDate
stringDescription
31
Network Service http://MobileDev.TW
處理XML資料
1. 建立一個專案,選擇Master-Detail
2. 新建檔案,命名為NewsItemObject.swift
3. 依要處理的XML進行調整
32
Network Service http://MobileDev.TW
處理XML資料
4. 新增檔案,繼承至NSObject,命名為
MyXMLParserDelegate
33
Network Service http://MobileDev.TW
處理XML資料
6. MyXMLParserDelegate,實作三個方法
didStartElement:遇到一個元件的開始標籤
34
Network Service http://MobileDev.TW
處理XML資料
7. MyXMLParserDelegate,實作三個方法
foundCharacters:遇到一個元件的內容
35
Network Service http://MobileDev.TW
處理XML資料
8. MyXMLParserDelegate,實作三個方法
didEndElement:遇到一個元件的結束標籤
36
Network Service http://MobileDev.TW
處理XML資料
9. MasterViewController.swift
37
Network Service http://MobileDev.TW
處理XML資料
10.MasterViewController的viewDidLoad
38
Network Service http://MobileDev.TW
處理XML資料
11.處理拿到的Data,交給XML Parser
39
Network Service http://MobileDev.TW
處理XML資料
13.設定tableView的顯示內容
40
Network Service http://MobileDev.TW
Lab
• 修改範例,讓使用者按下該則新聞時,能在下一個畫
面呈現該則新聞網頁
41
Network Service http://MobileDev.TW
Lab
1. 在第二個畫面中,增加一個webView,一個String
42
Network Service http://MobileDev.TW
Lab
2. 第二個畫面元件載入完成時,就載入網頁
43
Network Service http://MobileDev.TW
Lab
3. 回到第一個畫面,設定prepareForSegue
44
Network Service http://MobileDev.TW
資料介接
別人家的Server
XML or JSON
自己架的Server PHP + MySQL
XML Parser
JSON
Array & Dic
45
Network Service http://MobileDev.TW
台北市政府公開資料平台
• 網址:http://data.taipei
• 提供資料格式:XML or jSON
46
Network Service http://MobileDev.TW
使用資料 -> API
47
Network Service http://MobileDev.TW
如果資料來源是自己的SERVER…
48
Network Service http://MobileDev.TW
自建Server提供內容
49
Network Service http://MobileDev.TW
建構順序
1. 建構資料庫資料
2. 撰寫API,吐出XML or JSON
3. 行動端程式接收資料
4. 顯示於手機畫面
50
Network Service http://MobileDev.TW
Data API By PHP
資料庫連線
撈出資料
儲存資料
資料呈現
51
Network Service http://MobileDev.TW
圖片來自base64字串
• Image base64字串
• 當作網址來源處理,接收變成NSData物件
52
Network Service http://MobileDev.TW
有的資料有第二頁(超過1000筆)
• 查看Response Header
• http://web-sniffer.net/
• http://taipeicityopendata.cloudapp.net/v1/TaipeiOGDI/EPA
01/?format=json&NextPartitionKey=1!48!.....&NextRowKey
=1!48!.....
53

Más contenido relacionado

Similar a iOS APP Development - Network Service

進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!Will Huang
 
Mysql HandleSocket技术在SNS Feed存储中的应用
Mysql HandleSocket技术在SNS Feed存储中的应用Mysql HandleSocket技术在SNS Feed存储中的应用
Mysql HandleSocket技术在SNS Feed存储中的应用iammutex
 
2016-12-15 NewSQL資料庫在IoT的應用 - iServDB
2016-12-15 NewSQL資料庫在IoT的應用 - iServDB2016-12-15 NewSQL資料庫在IoT的應用 - iServDB
2016-12-15 NewSQL資料庫在IoT的應用 - iServDBJosé Lin
 
App+protocol+analysis
App+protocol+analysisApp+protocol+analysis
App+protocol+analysisSunny Summer
 
App+protocol+analysis (1)
App+protocol+analysis (1)App+protocol+analysis (1)
App+protocol+analysis (1)Sunny Summer
 
開放原始碼 Ch2.5 app - oss - 3rd party api(ver 1.0)
開放原始碼 Ch2.5   app - oss - 3rd party api(ver 1.0) 開放原始碼 Ch2.5   app - oss - 3rd party api(ver 1.0)
開放原始碼 Ch2.5 app - oss - 3rd party api(ver 1.0) My own sweet home!
 
20150528联动技术大讲堂15(刘胜)业务系统上线标准指引
20150528联动技术大讲堂15(刘胜)业务系统上线标准指引20150528联动技术大讲堂15(刘胜)业务系统上线标准指引
20150528联动技术大讲堂15(刘胜)业务系统上线标准指引liu sheng
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練22016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2Duran Hsieh
 
基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Betazhu02
 
基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Betafulin tang
 
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Justin Lin
 
组网实践
组网实践组网实践
组网实践telab
 
Windows Phone 7 in azure
Windows Phone 7 in azureWindows Phone 7 in azure
Windows Phone 7 in azureTao Wang
 
Internet System Security Overview
Internet System Security OverviewInternet System Security Overview
Internet System Security OverviewChinaNetCloud
 

Similar a iOS APP Development - Network Service (20)

網路概論
網路概論網路概論
網路概論
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
 
Mysql HandleSocket技术在SNS Feed存储中的应用
Mysql HandleSocket技术在SNS Feed存储中的应用Mysql HandleSocket技术在SNS Feed存储中的应用
Mysql HandleSocket技术在SNS Feed存储中的应用
 
2016-12-15 NewSQL資料庫在IoT的應用 - iServDB
2016-12-15 NewSQL資料庫在IoT的應用 - iServDB2016-12-15 NewSQL資料庫在IoT的應用 - iServDB
2016-12-15 NewSQL資料庫在IoT的應用 - iServDB
 
App+protocol+analysis
App+protocol+analysisApp+protocol+analysis
App+protocol+analysis
 
App+protocol+analysis (1)
App+protocol+analysis (1)App+protocol+analysis (1)
App+protocol+analysis (1)
 
開放原始碼 Ch2.5 app - oss - 3rd party api(ver 1.0)
開放原始碼 Ch2.5   app - oss - 3rd party api(ver 1.0) 開放原始碼 Ch2.5   app - oss - 3rd party api(ver 1.0)
開放原始碼 Ch2.5 app - oss - 3rd party api(ver 1.0)
 
20150528联动技术大讲堂15(刘胜)业务系统上线标准指引
20150528联动技术大讲堂15(刘胜)业务系统上线标准指引20150528联动技术大讲堂15(刘胜)业务系统上线标准指引
20150528联动技术大讲堂15(刘胜)业务系统上线标准指引
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練22016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta
 
基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta
 
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
 
组网实践
组网实践组网实践
组网实践
 
Windows Phone 7 in azure
Windows Phone 7 in azureWindows Phone 7 in azure
Windows Phone 7 in azure
 
Internet System Security Overview
Internet System Security OverviewInternet System Security Overview
Internet System Security Overview
 

Más de Ryan Chung

Voice-First Games for Alexa
Voice-First Games for AlexaVoice-First Games for Alexa
Voice-First Games for AlexaRyan Chung
 
AI Service Integration - Education
AI Service Integration - EducationAI Service Integration - Education
AI Service Integration - EducationRyan Chung
 
AI Service Integration
AI Service IntegrationAI Service Integration
AI Service IntegrationRyan Chung
 
AI Adoption in the Enterprise
AI Adoption in the EnterpriseAI Adoption in the Enterprise
AI Adoption in the EnterpriseRyan Chung
 
Intro to Dialogflow Chatbot Development
Intro to Dialogflow Chatbot DevelopmentIntro to Dialogflow Chatbot Development
Intro to Dialogflow Chatbot DevelopmentRyan Chung
 
AI in Classroom
AI in ClassroomAI in Classroom
AI in ClassroomRyan Chung
 
AWS re:Invent 2018 Recap
AWS re:Invent 2018 RecapAWS re:Invent 2018 Recap
AWS re:Invent 2018 RecapRyan Chung
 
Service Integration Workshop
Service Integration WorkshopService Integration Workshop
Service Integration WorkshopRyan Chung
 
Smart Home Intro Lab
Smart Home Intro LabSmart Home Intro Lab
Smart Home Intro LabRyan Chung
 
Introduction to DialogFlow
Introduction to DialogFlow Introduction to DialogFlow
Introduction to DialogFlow Ryan Chung
 
Conversational AI Orientation
Conversational AI OrientationConversational AI Orientation
Conversational AI OrientationRyan Chung
 
Udacity Meet Up - 0413
Udacity Meet Up - 0413Udacity Meet Up - 0413
Udacity Meet Up - 0413Ryan Chung
 
Amazon Alexa Development Part II
Amazon Alexa Development Part IIAmazon Alexa Development Part II
Amazon Alexa Development Part IIRyan Chung
 
Amazon Alexa Development
Amazon Alexa DevelopmentAmazon Alexa Development
Amazon Alexa DevelopmentRyan Chung
 
Microsoft Professional Program - AI
Microsoft Professional Program - AIMicrosoft Professional Program - AI
Microsoft Professional Program - AIRyan Chung
 
AI Service Integration
AI Service IntegrationAI Service Integration
AI Service IntegrationRyan Chung
 
Hour of Code - Amazon Alexa
Hour of Code - Amazon AlexaHour of Code - Amazon Alexa
Hour of Code - Amazon AlexaRyan Chung
 

Más de Ryan Chung (20)

Voice-First Games for Alexa
Voice-First Games for AlexaVoice-First Games for Alexa
Voice-First Games for Alexa
 
AI Service Integration - Education
AI Service Integration - EducationAI Service Integration - Education
AI Service Integration - Education
 
AI Service Integration
AI Service IntegrationAI Service Integration
AI Service Integration
 
AI Adoption in the Enterprise
AI Adoption in the EnterpriseAI Adoption in the Enterprise
AI Adoption in the Enterprise
 
Intro to Dialogflow Chatbot Development
Intro to Dialogflow Chatbot DevelopmentIntro to Dialogflow Chatbot Development
Intro to Dialogflow Chatbot Development
 
AI in Classroom
AI in ClassroomAI in Classroom
AI in Classroom
 
AWS re:Invent 2018 Recap
AWS re:Invent 2018 RecapAWS re:Invent 2018 Recap
AWS re:Invent 2018 Recap
 
MovieBot
MovieBotMovieBot
MovieBot
 
Service Integration Workshop
Service Integration WorkshopService Integration Workshop
Service Integration Workshop
 
MPP AI
MPP AIMPP AI
MPP AI
 
Smart Home Intro Lab
Smart Home Intro LabSmart Home Intro Lab
Smart Home Intro Lab
 
Introduction to DialogFlow
Introduction to DialogFlow Introduction to DialogFlow
Introduction to DialogFlow
 
Conversational AI Orientation
Conversational AI OrientationConversational AI Orientation
Conversational AI Orientation
 
Udacity Meet Up - 0413
Udacity Meet Up - 0413Udacity Meet Up - 0413
Udacity Meet Up - 0413
 
Amazon Alexa Development Part II
Amazon Alexa Development Part IIAmazon Alexa Development Part II
Amazon Alexa Development Part II
 
Amazon Alexa Development
Amazon Alexa DevelopmentAmazon Alexa Development
Amazon Alexa Development
 
Microsoft Professional Program - AI
Microsoft Professional Program - AIMicrosoft Professional Program - AI
Microsoft Professional Program - AI
 
AI Service Integration
AI Service IntegrationAI Service Integration
AI Service Integration
 
Intro to AI
Intro to AIIntro to AI
Intro to AI
 
Hour of Code - Amazon Alexa
Hour of Code - Amazon AlexaHour of Code - Amazon Alexa
Hour of Code - Amazon Alexa
 

iOS APP Development - Network Service