服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS-MVC层读取服务器接口JSON数据

IOS-MVC层读取服务器接口JSON数据

2021-05-28 16:21Aftery的博客 IOS

这篇文章主要为大家详细介绍了IOS-MVC层读取服务器接口JSON数据,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

此篇文章主要讲述获取服务器JSON接口数据,以便大家在今后的物联网信息时代,移动数据肯定都是需要网络数据的,那么就需要用一个分布集中,整体化的,规范性的创建所需的文件,同时方便以后维护;

分别为:M(model),M(manager),V(view),C(cell),C(controller);

具体程序如下:

一:YSMasonryManager.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#import <Foundation/Foundation.h>
#import "BaseManager.h"
#import "YSMasonryModel.h"
 
 
@protocol YSMasonryManagerDelegate <NSObject>
 
-(void)networkSuccess:(YSMasonryModel *_Nullable)result;
 
-(void)networkFail:(NSString *_Nullable)result;
 
@end
 
NS_ASSUME_NONNULL_BEGIN
 
@interface YSMasonryManager : BaseManager
 
@property(weak,nonatomic) id<YSMasonryManagerDelegate> delegate;
 
-(void)getDeviceProile:(NSString *)limit Offset:(NSString *)offset;
 
@end
 
NS_ASSUME_NONNULL_END

二:YSMasonryManager.m

 

?
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
#import "YSMasonryManager.h"
 
@implementation YSMasonryManager
 
-(void)getDeviceProile:(NSString *)limit Offset:(NSString *)offset{
  
  NSString *strUrl = [NSString stringWithFormat:@"%@api/internal/profile",DEVICE_BASE_URL];
  
  //JSON
  [PPNetworkHelper setRequestSerializer:PPRequestSerializerHTTP];
  [PPNetworkHelper setResponseSerializer:PPResponseSerializerJSON];
  //配置Jwt文件
  [PPNetworkHelper setValue:USERJWTKEYS forHTTPHeaderField:@"Grpc-Metadata-Authorization"];
  
  [PPNetworkHelper GET:strUrl parameters:@"" success:^(id responseObject){
    YSMasonryModel *net = [YSMasonryModel mj_objectWithKeyValues:responseObject];
    if(net.organizations.count>0){
      if([self.delegate respondsToSelector:@selector(networkSuccess:)]){
        [self.delegate networkSuccess:net];
      }
    }else{
      if([self.delegate respondsToSelector:@selector(networkFail:)]){
        [self.delegate networkFail:@"Get Fail!"];
      }
    }
    
  } failure:^(NSError *error){
    // NSError *newError = [NSError returnErrorWithError:error];
    if([self.delegate respondsToSelector:@selector(networkFail:)]){
      [self.delegate networkFail:@"cc"];
    }
  }];
}
 
@end

分析:

?
1
2
#import "BaseManager.h"//头部manager对象
#import "YSMasonryModel.h"//模型层

三:YSMasonryModel.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#import <Foundation/Foundation.h>
#import "BaseModel.h"
#import "YSMasonryPileModel.h"
 
NS_ASSUME_NONNULL_BEGIN
 
@interface YSMasonryModel : BaseModel
 
@property(nonatomic, copy) NSString *settings;
 
@property(nonatomic, copy) NSArray *organizations;
 
@property(nonatomic, copy) NSString *user;
 
 
 
+(NSDictionary*)mj_objectClassInArray;
 
@end
 
NS_ASSUME_NONNULL_END

四:YSMasonryModel.m

?
1
2
3
4
5
6
7
8
9
#import "YSMasonryModel.h"
 
@implementation YSMasonryModel
 
+(NSDictionary*)mj_objectClassInArray{
  return @{@"organizations":[YSMasonryPileModel class],};
}
 
@end

五:YSMasonryPileModel.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#import <Foundation/Foundation.h>
#import "BaseModel.h"
 
NS_ASSUME_NONNULL_BEGIN
 
@interface YSMasonryPileModel : BaseModel
@property(nonatomic,assign) NSString *isAdmin;
@property(nonatomic, copy) NSString *organizationID;
@property(nonatomic, copy) NSString *organizationName;
@property(nonatomic, copy) NSString *createdAt;
@property(nonatomic, copy) NSString *updatedAt;
@property(nonatomic,assign) NSString *disableAssignExistingUsers;
@property(nonatomic, copy) NSString *email;
@property(nonatomic, copy) NSString *uid;
@property(nonatomic,assign) NSString *isActive;
@property(nonatomic, copy) NSString *note;
@property(nonatomic,assign) NSString *sessionTTL;
@property(nonatomic, copy) NSString *username;
 
-(id)initWithDict:(NSDictionary*)dict;
 
@end
 
NS_ASSUME_NONNULL_END

六:YSMasonryPileModel.m

?
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
#import "YSMasonryPileModel.h"
 
@implementation YSMasonryPileModel
 
 
+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
  return @{@"uid": @"uid"};
}
 
-(id)initWithDict:(NSDictionary*)dict{
  if(self = [super init]){
    self.createdAt = dict[@"createdAt"];
    //@"gatewayDiscoveryEnabled":@(enable),
    self.isAdmin = dict[@"isAdmin"];
    self.organizationID = dict[@"organizationID"];
    self.organizationName = dict[@"networkServerID"];
    self.updatedAt = dict[@"updatedAt"];
    
     self.disableAssignExistingUsers = dict[@"disableAssignExistingUsers"];
    
    self.email = dict[@"email"];
    self.uid = dict[@"id"];
    self.isActive = dict[@"isActive"];
    self.note = dict[@"note"];
    self.sessionTTL = dict[@"sessionTTL"];
    self.username = dict[@"username"];
  }
  return self;
}
 
@end

七:YSMasonryView.h

?
1
2
3
4
5
6
7
8
9
10
11
12
#import <UIKit/UIKit.h>
#import "BaseView.h"
 
NS_ASSUME_NONNULL_BEGIN
 
@interface YSMasonryView : BaseView
 
@property(nonatomic,strong) UITableView *mDevericePile;
 
@end
 
NS_ASSUME_NONNULL_END

八:YSMasonryView.m

?
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
#import <Foundation/Foundation.h>
#import "YSMasonryView.h"
#ifdef __OBJC__
//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
#endif
 
@implementation YSMasonryView
 
-(void)initView{
  
  _mDevericePile = [[UITableView alloc] init];
  _mDevericePile.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  [self addSubview:_mDevericePile];
  
  [_mDevericePile makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self).offset(0);
    make.left.equalTo(self).offset(0);
    make.right.equalTo(self).offset(0);
    make.bottom.equalTo(self).offset(0);
  }];
}
 
@end

九:YSMasonryCell.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#import <UIKit/UIKit.h>
#import "BaseCell.h"
 
NS_ASSUME_NONNULL_BEGIN
 
@interface YSMasonryCell : BaseCell
@property(nonatomic, strong) UIImageView *mNetImgVw;
@property(nonatomic, strong) UILabel *mNetNameLb;
@property(nonatomic, strong) UILabel *mNetServerLb;
@property(nonatomic, strong) UILabel *mNetTimeLb;
 
@end
 
NS_ASSUME_NONNULL_END

十:YSMasonryCell.m

?
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#import "YSMasonryCell.h"
 
@implementation YSMasonryCell
 
- (void)awakeFromNib {
  [super awakeFromNib];
}
 
 
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if(self){
    UIView *mainView = [[UIView alloc] init];
    mainView.layer.cornerRadius = 8;
    mainView.layer.borderColor = [[UIColor grayColor] CGColor];
    mainView.layer.borderWidth = 1;
    [self addSubview:mainView];
    
    [mainView makeConstraints:^(MASConstraintMaker *make){
      make.left.equalTo(self).offset(10);
      make.right.equalTo(self).offset(-10);
      make.top.equalTo(self).offset(10);
      make.bottom.equalTo(self).offset(-10);
    }];
    
    _mNetImgVw = [[UIImageView alloc] init];
    [mainView addSubview:_mNetImgVw];
    
    [_mNetImgVw makeConstraints:^(MASConstraintMaker *make){
      make.centerY.equalTo(self);
      make.left.equalTo(self).offset(20);
      make.width.equalTo(@25);
      make.height.equalTo(@25);
    }];
    
    //Net名称
    _mNetNameLb = [[UILabel alloc] init];
    _mNetNameLb.textColor = [UIColor grayColor];
    _mNetNameLb.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    _mNetNameLb.textAlignment = NSTextAlignmentLeft;
    [mainView addSubview:_mNetNameLb];
    
    [_mNetNameLb makeConstraints:^(MASConstraintMaker *make){
      make.top.equalTo(mainView).offset(5);
      make.left.equalTo(self.mNetImgVw).offset(35);
      make.right.equalTo(mainView).offset(-5);
      make.height.equalTo(@30);
    }];
    
    //创建时间
    _mNetTimeLb = [[UILabel alloc] init];
    _mNetTimeLb.textColor = [UIColor grayColor];
    _mNetTimeLb.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    _mNetTimeLb.textAlignment = NSTextAlignmentRight;
    [mainView addSubview:_mNetTimeLb];
    
    [_mNetTimeLb makeConstraints:^(MASConstraintMaker *make){
      make.top.equalTo(mainView).offset(10);
      make.right.equalTo(mainView).offset(-10);
      make.width.equalTo(@160);
      make.height.equalTo(@30);
    }];
    
    //端口号
    _mNetServerLb = [[UILabel alloc] init];
    _mNetServerLb.textColor = [UIColor grayColor];
    _mNetServerLb.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    _mNetServerLb.textAlignment = NSTextAlignmentLeft;
    [mainView addSubview:_mNetServerLb];
    
    [_mNetServerLb makeConstraints:^(MASConstraintMaker *make){
      make.bottom.equalTo(self).offset(-15);
      make.left.equalTo(self.mNetImgVw).offset(35);
      make.right.equalTo(mainView).offset(-10);
      make.height.equalTo(@30);
    }];
  }
  return self;
}
 
 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];
}
 
@end

十一:YSMasonryContller.h

?
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
#import <UIKit/UIKit.h>
#import "BaseViewController.h"
#import "YSMasonryView.h"
#import "YSMasonryCell.h"
#import "YSMasonryManager.h"
#import "YSMasonryPileModel.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "WHToast.h"
 
 
NS_ASSUME_NONNULL_BEGIN
 
@interface YSMasonryContller : BaseViewController
 
@property(nonatomic,strong) YSMasonryView *lgdView;
 
@property(nonatomic, strong) YSMasonryManager *lgdManager;
 
@property(strong, nonatomic) NSMutableArray *dataList;
 
@property (strong,nonatomic) MBProgressHUD *hud;
 
@property(nonatomic, strong) YSMasonryPileModel *model;
 
@property(nonatomic, strong) NSString *organizations;
 
@property(assign,nonatomic) int limit;
 
@property(assign,nonatomic) int offsert;
 
@end
 
NS_ASSUME_NONNULL_END

十二:YSMasonryContller.m

 

?
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#import <Foundation/Foundation.h>
#import "YSMasonryContller.h"
 
#define CELLIDENTIFITER "NETTABLEVIEWCELL"
 
@interface YSMasonryContller()<YSMasonryManagerDelegate,UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>
 
@end
 
@implementation YSMasonryContller
 
- (void)viewDidLoad {
  [super viewDidLoad];
  [self initData];
  
}
 
//头部标题a
- (void)customContentView{
  UIColor *commonBlue = [self.commonUtil stringToColor:@"#333333"];
  [self.navigationController.navigationBar setBarTintColor:commonBlue];
  self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  self.navigationItem.title = @"NetWork Server";
}
 
- (void)customNavigationLeftItem{
  UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
  self.navigationItem.backBarButtonItem = item;
}
 
 
-(void)customNavigationRightItem{
  UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(onAddClick)];
  add.tintColor = [UIColor whiteColor];
  self.navigationItem.rightBarButtonItem = add;
}
 
 
 
-(void)initData{
  UIView *view = [[UIView alloc] init];
  self.lgdView.mDevericePile.tableFooterView = view;
  
  self.lgdView.frame = self.view.bounds;
  [self.view addSubview:self.lgdView];
  
  _limit = 5;
  _offsert = 0;
  
}
//启动时的运行数据
- (void)viewDidAppear:(BOOL)animated{
  [super viewDidAppear:animated];
  //加载数据
  NSString *slimit = [NSString stringWithFormat:@"%d",_limit];
  NSString *soffset = [NSString stringWithFormat:@"%d",_offsert];
  [self.lgdManager getDeviceProile:slimit Offset:soffset];
}
 
-(YSMasonryView*)lgdView{
  if(_lgdView==nil){
    _lgdView = [[YSMasonryView alloc] init];
    _lgdView.mDevericePile.dataSource = (id)self;
    _lgdView.mDevericePile.delegate = (id)self;
  }
  return _lgdView;
}
 
-(YSMasonryManager*)lgdManager{
  if(_lgdManager==nil){
    _lgdManager = [[YSMasonryManager alloc] init];
    _lgdManager.delegate = (id)self;
  }
  return _lgdManager;
}
 
-(NSMutableArray*)dataList{
  if(_dataList==nil){
    _dataList = [NSMutableArray array];
  }
  return _dataList;
}
 
#pragma mark get network data
 
-(void)networkSuccess:(YSMasonryModel *_Nullable)result{
  [self.lgdView.mDevericePile.mj_header endRefreshing];
  [self.lgdView.mDevericePile.mj_footer endRefreshing];
  self.dataList = [NSMutableArray arrayWithArray:result.organizations];
  [self.lgdView.mDevericePile reloadData];
 
}
 
 
-(void)networkFail:(NSString *_Nullable)result{
  [self.lgdView.mDevericePile.mj_header endRefreshing];
  [self.lgdView.mDevericePile.mj_footer endRefreshing];
  [WHToast showMessage:result originY:500 duration:2 finishHandler:^{
  }];
}
 
 
#pragma mark tableView
 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return self.dataList.count;
}
 
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  YSMasonryCell *cell = [tableView dequeueReusableCellWithIdentifier:@CELLIDENTIFITER];
  
  if(cell==nil){
    cell = [[YSMasonryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@CELLIDENTIFITER];
  }
  
  if(_dataList.count>0){
    YSMasonryPileModel *model = [[YSMasonryPileModel alloc] initWithDict:[self.dataList objectAtIndex:indexPath.row]];
    if(model!=nil){
      //cell.mNetImgVw.image = [UIImage imageNamed:@"net"];
      // cell.mNetNameLb.text = model.isAdmin;
      // cell.mNetTimeLb.text = [self dateAndStrings:model.createdAt];
      cell.mNetServerLb.text = @"vv";
    }
  }
  return cell;
}
 
 
 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(_dataList.count>0){
    YSMasonryPileModel *model = [[YSMasonryPileModel alloc] initWithDict:[self.dataList objectAtIndex:indexPath.row]];
    
    if(model!=nil){
      // [self updateNetWorkServer:model.deviceprolieid];
    }
  }
}
 
 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return 100;
}
 
-(NSString*)dateAndStrings:(NSString*)date{
  if(date!=nil){
    NSArray *strtime = [date componentsSeparatedByString:@"T"];
    //NSString *strtime = [date stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    return strtime[0];
  }
  return @"None";
}
 
//添加add设备
-(void)onAddClick{
 
}
@end

十三:所用到的常量一些定义:

Constants.h

?
1
2
3
4
5
#import <Foundation/Foundation.h>
 
#define BASE_URL @"http://110.22.33/ilLoRaWan/"
 
#define USERJWTKEYS @"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJsb3JhLWFwcC1zZXJ2ZXIiLCJhdWQiOiJsb3JhLWFwc"

至此!

以上详细的代码,主要就是mode数据层那,修改自己的json的返回数据,与一些字典类型的转换,就没其他的难点了。这样写的主要目的是,思路逻辑更清晰,后期维护也简单,分开着写代码更简洁。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_37523448/article/details/101206799

延伸 · 阅读

精彩推荐
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10