簡(jiǎn)述
關(guān)于iOS客戶端在模擬數(shù)據(jù)的時(shí)候如何避免污染代碼撒妈,之前已經(jīng)寫過如何通過charles代理攔截請(qǐng)求,返回本地構(gòu)造的假數(shù)據(jù)。iOS開發(fā)之模擬數(shù)據(jù)(一)排监,而本篇主要介紹另外一種方法狰右,通過搭建本地服務(wù)器的方式,返回構(gòu)造的數(shù)據(jù)舆床。
本地服務(wù)器介紹:
如果你會(huì)編寫python
棋蚌、java
、php
等后臺(tái)語音挨队,那么你可以自己輕松搞一個(gè)本地服務(wù)器進(jìn)行測(cè)試谷暮,如果不會(huì)怎么辦?相信大家都知道swift已經(jīng)支持后臺(tái)開發(fā)盛垦,并且支持跨平臺(tái)(linux湿弦,macOS(OS X),iOS)腾夯,并且現(xiàn)在已經(jīng)出現(xiàn)了很多比較知名的swift服務(wù)器框架(Perfect颊埃、Vapor蔬充、Kitura、Zewo
等) 班利,接下來主要介紹如何通過Perfect
搭建一個(gè)本地服務(wù)器饥漫,并返回模擬的數(shù)據(jù),至于為什么選擇Perfect
罗标?因?yàn)?code>Perfect相對(duì)于其他框架性能更快庸队,知名度也更高。
但是就像在iOS開發(fā)之模擬數(shù)據(jù)(一)中說的闯割,這種方式也會(huì)產(chǎn)生測(cè)試代碼(把域名改為本地服務(wù)器的域名)皿哨,需要在發(fā)布的時(shí)候修改過來。
Perfect介紹:
Perfect
是一個(gè)使用Swift
編程語言開發(fā)Web和其他REST服務(wù)的框架纽谒,其主要目標(biāo)是簡(jiǎn)化需要后端服務(wù)器軟件的移動(dòng)應(yīng)用的開發(fā),使開發(fā)人員可以使用同一種語言進(jìn)行客戶端和服務(wù)端開發(fā)如输。
因?yàn)槭腔赟wift開發(fā)鼓黔,所以對(duì)于在iOS平臺(tái)上可以達(dá)到客戶端與服務(wù)端使用相同的類和一些封裝好的工具,一定程度上可以減少代碼重復(fù)不见,這一點(diǎn)有點(diǎn)像Android與Java服務(wù)器協(xié)作的好處澳化,它完全支持使用Xcode開發(fā)和調(diào)試。由于Swift的開源特性稳吮,所以它必須能夠在Linux上跑起來缎谷。
Perfect
的工程可以在git上查看并且附有Demo,官網(wǎng)也有關(guān)于Perfect的詳細(xì)文檔與很多視頻教程
git:https://github.com/PerfectlySoft/Perfect
官網(wǎng):https://www.perfect.org
Perfect主要包含以下組件灶似。詳細(xì)查閱
http://www.infoq.com/cn/news/2015/11/perfect-swift
Perfect搭建服務(wù)器流程
1列林、從遠(yuǎn)程git倉(cāng)庫拉取代碼
2、編譯模板代碼酪惭,并開啟服務(wù)器
3希痴、在瀏覽器地址欄輸入:http://localhost:8181 驗(yàn)證服務(wù)器是否正常
如何模擬數(shù)據(jù)
上面介紹了Perfect官方demo的使用,我們可以看到上面demo中服務(wù)器只返回了一個(gè)hello春感,world的字符串砌创,但是在實(shí)際的iOS客戶端開發(fā)中,大部分返回的應(yīng)該是json數(shù)據(jù)鲫懒,那么接下來我們就模擬一下json數(shù)據(jù)并返回嫩实。
1、進(jìn)入Sources目錄窥岩,編輯main.swift
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
// An example request handler.
// This 'handler' function can be referenced directly in the configuration below.
func handler(data: [String:Any]) throws -> RequestHandler {
return {
request, response in
//設(shè)置返回的數(shù)據(jù)類型
response.setHeader(.contentType,value:"application/json")
//模擬json數(shù)據(jù)
let data:[String:Any] = [
"respcd":"0000",
"data":["hotTopics":[
["title":"這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題",
"author":"小黃老師",
"thumbnail":"http://gaopin-preview.bj.bcebos.com/133200518955.jpg",
"authorAvatar":"http://gaopin-preview.bj.bcebos.com/133200518470.jpg"],
["title":"這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題",
"author":"小郭老師",
"thumbnail":"http://gaopin-preview.bj.bcebos.com/133200511150.jpg",
"authorAvatar":"http://gaopin-preview.bj.bcebos.com/133200511116.jpg"],
["title":"這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題",
"author":"小王老師",
"thumbnail":"http://gaopin-preview.bj.bcebos.com/133200527866.jpg",
"authorAvatar":"http://mpic.tiankong.com/3b3/4a1/3b34a176b15a7fbad2859ce70b8d14cf/640.jpg@360h"],
["title":"這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題",
"author":"小李老師",
"thumbnail":"http://mpic.tiankong.com/1d0/7ac/1d07ac0b780fe16ee6d66b7277900563/640.jpg@360h",
"authorAvatar":"http://mpic.tiankong.com/a20/eaf/a20eaf991de87eed5042525226d00309/640.jpg@360h"],
["title":"這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題",
"author":"小趙老師",
"thumbnail":"http://mpic.tiankong.com/dbc/27d/dbc27d13e6a2e09cb719f0ef4ff68c28/640.jpg@360h",
"authorAvatar":"http://mpic.tiankong.com/07c/fe8/07cfe85595cf4a84df8a8f66409e83de/bld031659.jpg@360h"],
["title":"這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題這是一個(gè)標(biāo)題",
"author":"小張老師",
"thumbnail":"http://gaopin-preview.bj.bcebos.com/133200511150.jpg",
"authorAvatar":"http://gaopin-preview.bj.bcebos.com/133200511116.jpg"]
]]]
do{
//框架中帶有json解析框架
try response.setBody(json: data)
}catch{
//....
}
// Ensure that response.completed() is called when your processing is done.
response.completed()
}
}
// Configuration data for an example server.
// This example configuration shows how to launch a server
// using a configuration dictionary.
let confData = [
"servers": [
// Configuration data for one server which:
// * Serves the hello world message at <host>:<port>/
// * Serves static files out of the "./webroot"
// directory (which must be located in the current working directory).
// * Performs content compression on outgoing data when appropriate.
[
"name":"localhost",
"port":8181,
"routes":[
//設(shè)置接口路徑
["method":"get", "uri":"/explore/hotTopics", "handler":handler],
["method":"get", "uri":"/**", "handler":PerfectHTTPServer.HTTPHandler.staticFiles,
"documentRoot":"./webroot",
"allowResponseFilters":true]
],
"filters":[
[
"type":"response",
"priority":"high",
"name":PerfectHTTPServer.HTTPFilter.contentCompression,
]
]
]
]
]
do {
//開啟服務(wù)器
try HTTPServer.launch(configurationData: confData)
} catch {
fatalError("\(error)") // fatal error launching one of the servers
}
2甲献、編譯代碼并開啟服務(wù)器
swift build
./.build/debug/PerfectTemplate
3、瀏覽器訪問輸入請(qǐng)求路徑: http://localhost:8181/explore/hotTopics
4颂翼、客戶端訪問指定接口:
總結(jié)
以上就是通過swift的Perfect框架搭建一個(gè)本地測(cè)試服務(wù)器的流程竟纳,如果感覺不錯(cuò)記得點(diǎn)贊撵溃。