欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

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

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

分别为:m(model),m(manager),v(view),c(cell),c(controller);

具体程序如下:

一:ysmasonrymanager.h

#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

#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

分析:

#import "basemanager.h"//头部manager对象
#import "ysmasonrymodel.h"//模型层

三:ysmasonrymodel.h

#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

#import "ysmasonrymodel.h"

@implementation ysmasonrymodel

+(nsdictionary*)mj_objectclassinarray{
  return @{@"organizations":[ysmasonrypilemodel class],};
}

@end

五:ysmasonrypilemodel.h

#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

#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

#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

#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

#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

#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

#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

#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

#import <foundation/foundation.h>

#define base_url @"http://110.22.33/illorawan/"

#define userjwtkeys @"eyjhbgcioijiuzi1niisinr5cci6ikpxvcj9.eyjpc3mioijsb3jhlwfwcc1zzxj2zxiilcjhdwqioijsb3jhlwfwc"

至此!

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

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