This article mainly talks about obtaining server JSON interface data so that in the future Internet of Things information age, mobile data will definitely require network data. Then, it is necessary to create the required files in a centralized, holistic, and standardized manner, and facilitate future maintenance;
They are: M(model), M(manager), V(view), C(cell), and C(controller);
The specific procedures are as follows:
one:
#import <Foundation/> #import "" #import "" @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
two:
#import "" @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]; //Configure Jwt file [PPNetworkHelper setValue:USERJWTKEYS forHTTPHeaderField:@"Grpc-Metadata-Authorization"]; [PPNetworkHelper GET:strUrl parameters:@"" success:^(id responseObject){ YSMasonryModel *net = [YSMasonryModel mj_objectWithKeyValues:responseObject]; if(>0){ if([ respondsToSelector:@selector(networkSuccess:)]){ [ networkSuccess:net]; } }else{ if([ respondsToSelector:@selector(networkFail:)]){ [ networkFail:@"Get Fail!"]; } } } failure:^(NSError *error){ // NSError *newError = [NSError returnErrorWithError:error]; if([ respondsToSelector:@selector(networkFail:)]){ [ networkFail:@"cc"]; } }]; } @end
analyze:
#import ""//Head manager object#import ""//Model layer
three:
#import <Foundation/> #import "" #import "" 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
Four:
#import "" @implementation YSMasonryModel +(NSDictionary*)mj_objectClassInArray{ return @{@"organizations":[YSMasonryPileModel class],}; } @end
five:
#import <Foundation/> #import "" 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
six:
#import "" @implementation YSMasonryPileModel + (NSDictionary *)mj_replacedKeyFromPropertyName { return @{@"uid": @"uid"}; } -(id)initWithDict:(NSDictionary*)dict{ if(self = [super init]){ = dict[@"createdAt"]; //@"gatewayDiscoveryEnabled":@(enable), = dict[@"isAdmin"]; = dict[@"organizationID"]; = dict[@"networkServerID"]; = dict[@"updatedAt"]; = dict[@"disableAssignExistingUsers"]; = dict[@"email"]; = dict[@"id"]; = dict[@"isActive"]; = dict[@"note"]; = dict[@"sessionTTL"]; = dict[@"username"]; } return self; } @end
seven:
#import <UIKit/> #import "" NS_ASSUME_NONNULL_BEGIN @interface YSMasonryView : BaseView @property(nonatomic,strong) UITableView *mDevericePile; @end NS_ASSUME_NONNULL_END
eight:
#import <Foundation/> #import "" #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 "" #endif @implementation YSMasonryView -(void)initView{ _mDevericePile = [[UITableView alloc] init]; _mDevericePile.separatorStyle = UITableViewCellSeparatorStyleSingleLine; [self addSubview:_mDevericePile]; [_mDevericePile makeConstraints:^(MASConstraintMaker *make) { (self).offset(0); (self).offset(0); (self).offset(0); (self).offset(0); }]; } @end
Nine:
#import <UIKit/> #import "" 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
ten:
#import "" @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]; = 8; = [[UIColor grayColor] CGColor]; = 1; [self addSubview:mainView]; [mainView makeConstraints:^(MASConstraintMaker *make){ (self).offset(10); (self).offset(-10); (self).offset(10); (self).offset(-10); }]; _mNetImgVw = [[UIImageView alloc] init]; [mainView addSubview:_mNetImgVw]; [_mNetImgVw makeConstraints:^(MASConstraintMaker *make){ (self); (self).offset(20); (@25); (@25); }]; //Net name _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){ (mainView).offset(5); ().offset(35); (mainView).offset(-5); (@30); }]; //Creation time _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){ (mainView).offset(10); (mainView).offset(-10); (@160); (@30); }]; //Port number _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){ (self).offset(-15); ().offset(35); (mainView).offset(-10); (@30); }]; } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; } @end
eleven:
#import <UIKit/> #import "" #import "" #import "" #import "" #import "" #import "" #import "" #import "" 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
twelve:
#import <Foundation/> #import "" #define CELLIDENTIFITER "NETTABLEVIEWCELL" @interface YSMasonryContller()<YSMasonryManagerDelegate,UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate> @end @implementation YSMasonryContller - (void)viewDidLoad { [super viewDidLoad]; [self initData]; } //Head title a- (void)customContentView{ UIColor *commonBlue = [ stringToColor:@"#333333"]; [ setBarTintColor:commonBlue]; = [UIColor whiteColor]; = @"NetWork Server"; } - (void)customNavigationLeftItem{ UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; = item; } -(void)customNavigationRightItem{ UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(onAddClick)]; = [UIColor whiteColor]; = add; } -(void)initData{ UIView *view = [[UIView alloc] init]; = view; = ; [ addSubview:]; _limit = 5; _offsert = 0; } //Operation data at startup- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; //Load data NSString *slimit = [NSString stringWithFormat:@"%d",_limit]; NSString *soffset = [NSString stringWithFormat:@"%d",_offsert]; [ getDeviceProile:slimit Offset:soffset]; } -(YSMasonryView*)lgdView{ if(_lgdView==nil){ _lgdView = [[YSMasonryView alloc] init]; _lgdView. = (id)self; _lgdView. = (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{ [.mj_header endRefreshing]; [.mj_footer endRefreshing]; = [NSMutableArray arrayWithArray:]; [ reloadData]; } -(void)networkFail:(NSString *_Nullable)result{ [.mj_header endRefreshing]; [.mj_footer endRefreshing]; [WHToast showMessage:result originY:500 duration:2 finishHandler:^{ }]; } #pragma mark tableView -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return ; } - (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:[ objectAtIndex:]]; if(model!=nil){ // = [UIImage imageNamed:@"net"]; // = ; // = [self dateAndStrings:]; = @"vv"; } } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(_dataList.count>0){ YSMasonryPileModel *model = [[YSMasonryPileModel alloc] initWithDict:[ objectAtIndex:]]; if(model!=nil){ // [self updateNetWorkServer:]; } } } -(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 add device-(void)onAddClick{ } @end
Thirteen: Some definitions of constants used:
#import <Foundation/> #define BASE_URL @"http://110.22.33/ilLoRaWan/" #define USERJWTKEYS @"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJsb3JhLWFwcC1zZXJ2ZXIiLCJhdWQiOiJsb3JhLWFwc"
So far!
The above detailed code is mainly about the mode data layer. There is no other difficulty in modifying your own json return data and converting it with some dictionary types. The main purpose of writing this way is to have clearer ideas and logic, simpler post-maintenance, and simpler code writing separately.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.