美文网首页各种服务器
iOS开发 真机调试访问本地服务器

iOS开发 真机调试访问本地服务器

作者: 冰雨9527 | 来源:发表于2018-11-08 14:52 被阅读518次

背景

1.客户需求
url:http://x.x.x.x:8080/uflow/exposeIface.do?method=isAllowLogin
{"code":0,"msg":"成功","data":{"isAllowLogin":true,"hintMessage":"请从任我行统一登陆"}}
注:isAllowLogin值为true时,允许app登陆;
2.客户是内网,我们无法访问到,于是想到了本地服务器,详细搭建见
手机app开发(iOS+Android) 本地服务器的搭建


本地服务器目录结构,如图所示:

settings.json

[
 {
     "include" : "asset/assetList.json"
 }
]

assetList.json

[
  {
    "description":"模拟一个带参数的post请求",
    "request":{
      "uri":"/uflow/exposeIface",
      "method":"post",
      "forms":{
        "method" : "isAllowLogin"
      }
    },
    "response" :
    {
      "file" : "./asset/assetList_response.json"
    }
  }
]

assetList_response.json

{
    "code":0,
    "msg":"成功",
    "data":
    {
        "isAllowLogin":true,
        "hintMessage":"请从任我行统一登陆"
    }
}

配置完以上内容后在终端里输入以下方法

cd /Users/macOne/Desktop/moco-server
//注意 -g 不是 -c
java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -g settings.json

得到以下内容说明你的配置文件没有错误。Server is started!

INFO  Server is started at 12306
INFO  Shutdown port is 51783

上 postman

iOS 模拟器

访问: NSString *url=@"http://localhost:12306/uflow/exposeIface";

- (void)requestAssetList
{
//    __weak typeof(self) weakSelf = self;
    NSString *url=@"http://localhost:12306/uflow/exposeIface";
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    parameter[@"method"] = @"isAllowLogin";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功:%@",responseObject);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if (dict.count)
        {
            NSLog(@"isAllowLogin = %@",dict);
            NSLog(@"hintMessage = %@",dict[@"data"][@"hintMessage"]);
            NSLog(@"isAllowLogin = %@",dict[@"data"][@"isAllowLogin"]);
        }
        else
        {
            return ;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
    {
        NSLog(@"获取信息失败::%@",error);
    }];
}

iOS 真机

访问: NSString *url=@"http://192.168.2.114:12306/uflow/exposeIface";

- (void)requestAssetList
{
//    __weak typeof(self) weakSelf = self;
    NSString *url=@"http://192.168.2.114:12306/uflow/exposeIface";
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    parameter[@"method"] = @"isAllowLogin";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功:%@",responseObject);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if (dict.count)
        {
            NSLog(@"isAllowLogin = %@",dict);
            NSLog(@"hintMessage = %@",dict[@"data"][@"hintMessage"]);
            NSLog(@"isAllowLogin = %@",dict[@"data"][@"isAllowLogin"]);
        }
        else
        {
            return ;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
    {
        NSLog(@"获取信息失败::%@",error);
    }];
}

注:其中192.168.2.114为你本机 IP 地址
具体查看方法
mac电脑查看ip、MAC地址和dns配置的方法
Mac查看本机IP的终端命令
iOS开发 真机调试访问本地服务器的url问题
让电脑和手机连接同一个wifi,这样手机跟电脑就连接在同一个局域网上了。然后查看本机的ip地址,例如我电脑本机的ip地址是:192.168.2.114,然后利用http://192.168.2.114:12306来访问本地的服务器了。不单只是手机可以访问,只要是连接在这个局域网上面的所有设备均能够访问到。

iOS 真机+青花瓷+Moco(本地服务器)


下载见: Charles 4.2.5 破解
配置见: Charles 安装图解(Mac 抓包工具)
参考见: 菜鸟搭建 Mock 服务器实践:Anyproxy+Moco

学习资料
Moco接口框架学习(http协议)

相关文章

  • iOS开发 真机调试访问本地服务器

    背景 1.客户需求url:http://x.x.x.x:8080/uflow/exposeIface.do?met...

  • ios真机调试 打包

    ios真机调试 1.1打开 Finder->应用->钥匙串访问 ...

  • APP生成证书.真机调试.上线流程(详细版)

    IOS中生成证书、真机调试、上线发布程序的步骤 前言:学习ios开发快一年了生成证书,真机调试,程序上线发布的总结...

  • 手机访问mac,window本地服务器

    我们在做安卓或者ios开发时,开发阶段会用手机访问本地服务器,而本地服务器开发阶段也是部署在后台开发的电脑上。ma...

  • 资源

    iOS真机调试包 更新iOS15.0 更新iOS15.0 真机调试包 请点击下载[https://github.c...

  • ios申请证书

    首先介绍下常用苹果iOS证书的常用类型 iOS Development:iOS开发证书(用于真机调试测试) iOS...

  • 真机调试+打包+发布

    真机调试需要两个文件 ios7之前真机调试需要的步骤 打包测试 打包测试和真机调试的区别 注意: ios7之前的真...

  • 移动端开发本地联调环境搭建

    本地联调环境搭建   当在本地进行移动端开发时,由于项目没有发布到bate机或开发机上,此时的真机调试就稍显困难,...

  • iOS APP真机调试图文介绍

    iOS真机调试首先要申请iOS证书 调试证书分两种 开发证书和Ad hoc证书 如果你用xcode开发,可以直接用...

  • iOS真机调试

    作为一个iOS开发者免不了会将应用装到各种真机上进行调试,为免以后的遗忘与麻烦,对iOS开发的真机调试做个简单的小...

网友评论

    本文标题:iOS开发 真机调试访问本地服务器

    本文链接:https://www.haomeiwen.com/subject/iwkwxqtx.html