当前位置 主页 > 网站技术 > 代码类 >

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

    栏目:代码类 时间:2019-12-25 21:08

    此篇文章主要讲述获取服务器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