iOS开发之模拟接口假数据

一、前言

我们在iOS开发的过程中,往往后端和前端都是并行的,当后端的接口没有完善的时候就会影响到我们的开发,而我也在我编程的过程中也经历过很多方法去制造假数据,下面我就一一说一下吧。

二、方法

下面我们以如下的JSON字段进行一下说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
   "code" 0 ,
   "message" "成功" ,
   "data" : {
     "website" : "https://www.ianisme.com" ,
     "list" : [
       {
         "day" "30" ,
         "month" "10" ,
         "year" "2017"
       }
     ]
   }
}

2.1 Dictionary假数据

这种方法应该说是最不建议使用的方法了。假设我们工程中使用的是JSModel进行Model层转换的。

我们需要这样写一个NSDictionary

如图所示我们就可以这样去写假数据了。这种方法是每次都要去修改和编辑大量代码,并且关于网络请求的代码还测试不了,不推荐使用。

2.2 搭建后台假数据

我们可以本地用搭建一个网站环境或者使用远程服务器去请求。

我会保存一个json文件,然后用一个php文件去调用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!--?php
header( "Content-type: text/html; charset=utf-8" );
$test = $_POST[ "s" ];
$json_string = file_get_contents($test . &# 39 ;.json&# 39 ;);
echo $json_string;</pre--><p style= "text-align: center;" ><img src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception1.png" _src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception1.png" ></p><p style= "text-align: center;" ><img src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception2.png" _src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception2.png" ></p><p>这种情况下,我们可以直接把app端的网络请求代码全部写好,就相当于模仿后台的接口一样,到时候切换后台接口我们只需要更换下接口地址就行了。</p><p><strong> 2.3 APP端修改服务器数据</strong></p><p>这里我们以 AFNetworking2.x 为例使用 NSURLProtocol 拦截 HTTP 请求。</p><p>创建NSURLProtocol的一个子类,重写里面的startLoading方法。</p><pre class = "brush:as3;toolbar:false" >- ( void )startLoading
{
     NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];
 
     [NSURLProtocol setProperty:@YES forKey:hasInitKey inRequest:mutableReqeust];
 
     if   ([mutableReqeust.URL.description containsString:@ "api.ianisme.com" ]) {
 
         NSDictionary *dic = @{
                               @ "code" : @ 0 ,
                               @ "message" : @ "成功" ,
                               @ "data" : @{
                                       @ "website" : @ "https://www.ianisme.com" ,
                                       @ "list" : @[
                                               @{
                                                   @ "day" : @ "30" ,
                                                   @ "month" : @ "10" ,
                                                   @ "year" : @ "2017"
                                                   }
                                               ]
                                       }
                               };
         NSData *tempData = [self toJSONData:dic];
         NSString *jsonString = [[NSString alloc] initWithData:tempData
                                                      encoding:NSUTF8StringEncoding];
 
         NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
         NSURLResponse *response = [[NSURLResponse alloc] initWithURL:mutableReqeust.URL
                                                             MIMEType:@ "text/html"
                                                expectedContentLength:data.length
                                                     textEncodingName:nil];
         [self.client URLProtocol:self
               didReceiveResponse:response
               cacheStoragePolicy:NSURLCacheStorageNotAllowed];
 
         [self.client URLProtocol:self didLoadData:data];
         [self.client URLProtocolDidFinishLoading:self];
 
     }
     else   {
         self.myConnection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];
     }
}</pre><p style= "text-align: center;" ><img src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception3.png" _src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception3.png" ></p><p>此方法我们可以把相关网络请求写好,然后本地代码拦截后台的网络请求,用假数据修改就行了。以上只是一个原理的演示,实际开发,推荐使用强大的开源库OHHTTPStubs, 他可以伪造的网络数据和模拟的缓慢网络来进行调试。</p><p>传送门-><a href= "https://github.com/AliSoftware/OHHTTPStubs" target= "_blank" _href= "https://github.com/AliSoftware/OHHTTPStubs" >OHHTTPStubs</a></p><p><strong> 2.4 代理拦截网络请求</strong></p><p>这个是我比较推荐的一个方案,不需要修改app端代码。一切无损对接后台。</p><p>这就是利用代理软件的 Map Local 功能,将请求转换为请求电脑本地的静态json文件。</p><p>我们以Charles为例,我们把本地的接口写好之后,我们使用Charles抓一下这个接口的请求,此时肯定是失败的。</p><p>如图:</p><p style= "text-align: center;" ><img src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception4-768x343.png" _src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception4-768x343.png" ></p><p>我们去 Map Local 指向电脑中的一个json文件。</p><p>如图:</p><p style= "text-align: center;" ><img src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception5.png" _src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception5.png" ></p><p style= "text-align: center;" ><img src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception6.png" _src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception6.png" ></p><p style= "text-align: center;" ><img src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception7.png" _src= "https://www.ianisme.com/wp-content/uploads/2017/11/networkInterception7.png" ></p><p>这样我们就将此接口指向了电脑本地的一个json文件,我们可以用此方法,将所有的接口都分别指向本地的各自的 json 文件,当后台接口完毕后,我们就可以关闭 Map Local 无缝衔接到真正的后台。</p><p>本文涉及到的部分代码 -> <a href= "https://github.com/ianisme/NetworkInterception" target= "_blank" _href= "https://github.com/ianisme/NetworkInterception" >点我下载</a></p><p><span style= "font-size: 24px;" ><strong>三、总结</strong></span></p><p>以上四个方法,我的推荐程度是由小到大的。我认为最好不要去动APP的代码,所以我推荐第四种,赶快去尝试一下吧!你用的哪种呢?欢迎留言交流。</p><p>参考与延伸:</p><ul class = " list-paddingleft-2" ><li><p><a href= "https://draveness.me/intercept" target= "_blank" _href= "https://draveness.me/intercept" >《iOS 开发中使用 NSURLProtocol 拦截 HTTP 请求》</a></p></li><li><p><a href= "https://draveness.me/http-mock" target= "_blank" _href= "https://draveness.me/http-mock" >《如何进行 HTTP Mock(iOS)》</a></p></li><li><p><a href= "http://blog.devtang.com/2015/11/14/charles-introduction/" target= "_blank" _href= "http://blog.devtang.com/2015/11/14/charles-introduction/" >《Charles 从入门到精通》</a></p></li><li><p><a href= "https://www.ianisme.com/ios/2502.html" target= "_blank" _href= "https://www.ianisme.com/ios/2502.html" >《关于Charles抓HTTPS包的tips》</a></p></li><li><p><a href= "http://blog.csdn.net/colorapp/article/details/47007431" target= "_blank" _href= "http://blog.csdn.net/colorapp/article/details/47007431" >《iOS单元测试:Specta + Expecta + OCMock + OHHTTPStubs + KIF》</a></p></li></ul>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值