Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read JSON data of server interface in MVC layer of IOS

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the IOS MVC layer how to read server interface JSON data, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The specific procedures are as follows:

One: YSMasonryManager.h

# import # import "BaseManager.h" # import "YSMasonryModel.h" @ protocol YSMasonryManagerDelegate-(void) networkSuccess: (YSMasonryModel * _ Nullable) result;- (void) networkFail: (NSString * _ Nullable) result;@endNS_ASSUME_NONNULL_BEGIN@interface YSMasonryManager: BaseManager@property (weak,nonatomic) id delegate;- (void) getDeviceProile: (NSString *) limit Offset: (NSString *) offset;@endNS_ASSUME_NONNULL_END

Two: 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]; / / configure Jwt file [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

Analysis:

# import "BaseManager.h" / / header manager object # import "YSMasonryModel.h" / / Model layer

Three: YSMasonryModel.h

# import # 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;@endNS_ASSUME_NONNULL_END

Four: YSMasonryModel.m

# import "YSMasonryModel.h" @ implementation YSMasonryModel+ (NSDictionary*) mj_objectClassInArray {return @ {@ "organizations": [YSMasonryPileModel class],};} @ end

Five: YSMasonryPileModel.h

# import # 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) nonatomic,assign * NSString (NSString, isActive;@property) NSString @ property (nonatomic,assign) NSString * sessionTTL;@property (nonatomic, copy) NSString * username;- (id) initWithDict: (NSDictionary*) dict;@endNS_ASSUME_NONNULL_END

Six: 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

Seven: YSMasonryView.h

# import # import "BaseView.h" NS_ASSUME_NONNULL_BEGIN@interface YSMasonryView: BaseView@property (nonatomic,strong) UITableView * mDevericePile;@endNS_ASSUME_NONNULL_END

Eight: YSMasonryView.m

# import # 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

Nine: YSMasonryCell.h

# import # 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;@endNS_ASSUME_NONNULL_END

Ten: 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 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) {make.top.equalTo (mainView) .offset (5) Make.left.equalTo (self.mNetImgVw) .offset (35); make.right.equalTo (mainView) .offset (- 5); make.height.equalTo (@ 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) {make.top.equalTo (mainView) .offset (10); make.right.equalTo (mainView). Offset (- 10); make.width.equalTo (@ 160); make.height.equalTo (@ 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) {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

Eleven: YSMasonryContller.h

# import # 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;@endNS_ASSUME_NONNULL_END

Twelve: YSMasonryContller.m

# import # import "YSMasonryContller.h" # define CELLIDENTIFITER "NETTABLEVIEWCELL" @ interface YSMasonryContller () @ end@implementation YSMasonryContller- (void) viewDidLoad {[super viewDidLoad]; [self initData];} / / header title 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;} / / run data at startup-(void) viewDidAppear: (BOOL) animated {[super viewDidAppear:animated]; / / load data 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 (modelled roomnil) {/ / 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 (modelling cycles nil) {/ / [self updateNetWorkServer:model.deviceprolieid];}-(CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {return 100 }-(NSString*) dateAndStrings: (NSString*) date {if (datestamnil) {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:

Constants.h

# import # define BASE_URL @ "http://110.22.33/ilLoRaWan/"#define USERJWTKEYS @" eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJsb3JhLWFwcC1zZXJ2ZXIiLCJhdWQiOiJsb3JhLWFwc "

Thank you for reading this article carefully. I hope the article "how to read server interface JSON data in MVC layer in IOS" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report