三方库的使用




##AFNetworking
#import “AFNetworking.h”//主要用于网络请求方法
#import “UIKit+AFNetworking.h”//里面有异步加载图片的方法


基本使用格式:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

/** AFHTTPSessionManager单例类对象的配置 */
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; //表示不做SSL pinning

manager.responseSerializer = [AFJSONResponseSerializer serializer];//响应类型:Json类型
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"text/heml", nil]; //支持的类型 

 
manager.requestSerializer.timeoutInterval = 6; //请求超时

[manager GET:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"JSON DATA: %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error: %@", error);
}];
复制代码


进行再封装时,可配合block的使用!参考:《Block使用 配合网络请求(AFNetworking)》。


详细参考:《AFNetworking的介绍与使用





##Masonry
#import “Masonry.h”


######基本使用方法:

  • 新增 约束 :- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;

    [  mas_makeConstraints:^(MASConstraintMaker *make) { 
      //只负责新增约束(与Autolayout不能同时存在)
    
    }];
    复制代码


  • 清除 约束 :- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;

    [  mas_updateConstraints:^(MASConstraintMaker *make) { //
      //清除 之前的所有约束,仅保留最新的约束
    
    }];
    复制代码


  • 更新 约束 :- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

    [  mas_remakeConstraints:^(MASConstraintMaker *make) {
       //更新  在block中出现的约束,不会导致出现两个相同约束的情况
    
    }];
    复制代码





##SDWebImage

#import "UIImageView+WebCache.h"//SDWebImage缓存
#import <SDWebImage/UIButton+WebCache.h>//处理按钮的图片
复制代码


######基本使用方法:

NSString * picUrlStr = @"http://www.domain.com/path/pics/blabla_1.jpg";//图片的地址

[_imgView sd_setImageWithURL:[NSURL URLWithString: picUrlStr] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
//占位图片的名称:“placeholder.png”
复制代码


详细参考:《iOS图片加载框架-SDWebImage解读





数据解析

  • YYModel
    #import "YYModel.h"
    复制代码


    ######基本使用方法:

    ######模型类UserInfoLogModel


    “UserInfoLogModel.h”定义:

    @interface UserInfoLogModel : BaseModel //继承自 基础模型类
    复制代码


    基础模型类 BaseModel
    #import "YYModel.h"
    @interface BaseModel : NSObject <YYModel> //遵守YYModel协议
    复制代码


    UserInfoLogModel的使用 :

    NSDictionary * jsonDict = responseObject[@"DataList"];
    // 将 JSON数据(NSData,NSString,NSDictionary) 转换为 Model
    UserInfoLogModel * userInfoModel = [UserInfoLogModel yy_modelWithJSON:jsonDict ];
          
    复制代码




###展示例子?
DetailsOfDiscussModel类:
JSONKey值 与 对应Model属性名 不同(改变 JSON的Key对应的Model属性名);
Model的属性含 容器类型



######DetailsOfDiscussModel.h:

#import "BaseModel.h"

@interface DetailsOfDiscussModel : BaseModel

@property (nonatomic,strong) NSString * content; //内容
@property (nonatomic,strong) NSString * _id;//⭐️⭐️⭐️改变 JSON的Key对应的Model属性名⭐️⭐️⭐️

@property (nonatomic,copy) NSArray * members;//⭐️⭐️⭐️含容器类型⭐️⭐️⭐️

@property (nonatomic,strong) NSString * topic; //主题

@end



@interface DiscussMemberModel : BaseModel

@property(nonatomic,copy) NSString * avatar;    //头像
@property(nonatomic,copy) NSString * cityCode;  //城市码
@property(nonatomic,copy) NSString * cityName;  //城市名
@property(nonatomic,copy) NSString * countyCode;//区市码
@property(nonatomic,copy) NSString * countyName;//区市名
@property(nonatomic,copy) NSString * mobile;//手机号
@property(nonatomic,copy) NSString * userId;    //用户id
@property(nonatomic,copy) NSString * userName;  //用户名

@end
复制代码


######DetailsOfDiscussModel.m:

#import "DetailsOfDiscussModel.h"

@implementation DetailsOfDiscussModel

//返回一个Dict,Model属性名 和 映射到JSON的Key。
+ (NSDictionary *)modelCustomPropertyMapper {
   return @{
          @"_id" : @"id" //⭐️⭐️⭐️改变 JSON的Key对应的Model属性名⭐️⭐️⭐️
          };
}

//返回一个Dict,Model属性名 和 Model属性容器中需要存放的对象类型
+ (NSDictionary *)modelContainerPropertyGenericClass
{
    return @{@"members":[DiscussMemberModel class]}; //⭐️⭐️⭐️含容器类型⭐️⭐️⭐️
}
  
@end



@implementation DiscussMemberModel //仅仅实现就好了

@end
复制代码



详细参考:《YYModel 简介与使用




  • JSONModel
    #import "JSONModel.h"
    复制代码


    ######基本使用方法:

    ######模型类UserInfoLogModel


    “UserInfoLogModel.h”定义:

     @interface UserInfoLogModel : JSONModel //继承自 JSONModel类
    复制代码


    UserInfoLogModel的使用 :

    NSDictionary * jsonDict = responseObject[@"DataList"];
    NSError* err = nil;
    UserInfoLogModel * userInfoModel = [[UserInfoLogModel alloc] initWithDictionary:jsonDict error:&err];
    复制代码





##IQKeyboardManager

#import "IQKeyboardManager.h"    
复制代码


######基本使用方法:

//IQKeyboardManager单例类对象
IQKeyboardManager *manager = [IQKeyboardManager sharedManager]; 


//关闭功能 设置为YES  (默认值为NO)
manager.enable = YES;

//点击空白区域键盘收缩 的开关   (默认值为NO)
manager.shouldResignOnTouchOutside = YES;

//是否显示它自带键盘工具条 的开关  (默认值为YES)
manager.enableAutoToolbar = NO;
复制代码

当某一个特定输入框(_editTF) 不需要使用键盘上的工具条时:
_editTF.inputAccessoryView = [[UIView alloc] init];


需要在页面里,禁止自动键盘处理的事件:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[IQKeyboardManager sharedManager] setEnable:NO];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [[IQKeyboardManager sharedManager] setEnable:YES];
}
复制代码





##MJRefresh

#import "MJRefresh.h"
复制代码


######基本使用方法:

直接定义tableView的header、footer:(mj_headermj_footer)

self.discussListTabV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //下拉更新    (网络请求)
    [self getDataWith:10 and:0]; 
}];
self.discussListTabV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    //上拉刷新    (网络请求)
    [self getDataWith:10 and:(int)self.dataArray.count]; 
}];


//显示 “上拉刷新”  (初次)
self.discussListTabV.mj_footer.hidden = NO; //显示(“上拉刷新”) 底部的“mj_footer”  
复制代码


隐藏显示 底部的“mj_footer”(“上拉刷新”) 的情况:

//上、下滑动tableView时,显示(“上拉刷新”) 底部的“mj_footer”
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    //可判断“是否已经请求完数据??”,再来设置显示与否!

    self.discussListTabV.mj_footer.hidden = NO; //显示(“上拉刷新”) 底部的“mj_footer”
}
复制代码

//网请成功
 dispatch_async(dispatch_get_main_queue(), ^{ //停止刷新
   [self.discussListTabV.mj_header endRefreshing];
   [self.discussListTabV.mj_footer endRefreshing];

   self.discussListTabV.mj_footer.hidden = YES; //隐藏(“上拉刷新”) 底部的“mj_footer”
});
复制代码

//网请失败
dispatch_async(dispatch_get_main_queue(), ^{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = [error localizedDescription]; //显示报错信息
    hud.margin = 10.f;
    hud.offset = CGPointMake(0, 0.f);
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1.5f];
    hud.userInteractionEnabled = NO;
            
    self.discussListTabV.mj_footer.hidden = NO;//显示(“上拉刷新”) 底部的“mj_footer”
});
复制代码





进度指示器(“菊花转”)

  • SVProgressHUD

######基本使用方法:

+ (void)setBackgroundColor:(UIColor*)color; //背景颜色 (默认:白色)
+ (void)setForegroundColor:(UIColor*)color; //progress和label的颜色


//图片记住要渲染!!
+ (void)setInfoImage:(UIImage*)image; //消息的图片  
+ (void)setSuccessImage:(UIImage*)image; //成功时的图片 
+ (void)setErrorImage:(UIImage*)image; //失败时的图片


+ (void)setDefaultMaskType:(SVProgressHUDMaskType)maskType; //HUD的显示模式
    / **
     SVProgressHUDMaskTypeNone = 1,  // 允许用户进行其他操作
     SVProgressHUDMaskTypeClear,     // 不允许用户进行其他操作
     SVProgressHUDMaskTypeBlack,     // 不允许用户进行其他操作,且背景为黑色
     SVProgressHUDMaskTypeGradient   // 允许用户进行其他操作,且背景是渐变的黑色
    * /



+ (void)show; //显示加载框:一个迅速转动的圈

+ (void)showWithStatus:(NSString*)status; //显示加载框,并且带着文字
+ (void)setStatus:(NSString*)string; //改变正在显示HUD上的文字

+ (void)showProgress:(float)progress;  //显示进度框:一个进度圈

+ (void)showImage:(UIImage*)image status:(NSString*)status; //显示 自己设置的图片(图片大小:28px * 28px)


+ (void)dismiss; //让进度框消失

 

+ (void)setOffsetFromCenter:(UIOffset)offset; //距离中心点的偏移量
+ (void)resetOffsetFromCenter; //返回中心点


+ (void)showSuccessWithStatus:(NSString*)string; //显示成功的消息提示
+ (void)showErrorWithStatus:(NSString *)string; //显示错误的消息提示


+ (BOOL)isVisible; //监测:是否正在显示
复制代码


延时停止“菊花转”
  • 延时0.6s,执行 停止方法

    [SVProgressHUD showWithStatus:NSLocaString(@"加载中……")];
    
    //延时0.6s,停止“活动指示器”
    [self performSelector:@selector(progressDelayDismiss) withObject:nil afterDelay:0.6f];  
    
       
    -(void)progressDelayDismiss {  // 停止“活动指示器”
       [SVProgressHUD dismiss];
    }
    复制代码


  • 使用GCD延时0.6s,隐藏SVProgressHUD单例类

    //SVProgressHUD偏移位置
    [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, 0)];
    
    [SVProgressHUD showWithStatus:NSLocaString(@"加载中……")];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 延迟0.6秒后消失
        [SVProgressHUD dismiss];
    });
    复制代码



  • MBProgressHUD

######基本使用方法:

  • 只显示文字:

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; //定义及加载到的视图
    hud.mode = MBProgressHUDModeText; //模式:显示文字
    hud.label.text = @"不能拨号!"; //显示的文字
    hud.margin = 10.f; //hud内部视图相对于hud的内边距
    hud.offset = CGPointMake(0, 0.f); //hud的偏移量
    
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1.5f]; //1.5s后移除掉hud
    hud.userInteractionEnabled = NO; //hud显示期间,其父视图 不可用户交互!
    复制代码


  • 加载动画:旋转加载圈

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.mode = MBProgressHUDModeCustomView; //自定义视图
    UIImage * image = [[UIImage imageNamed:@"01"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; //渲染好的图片
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    anima.toValue = @(M_PI*2);
    anima.duration = 1.0f;
    anima.repeatCount = 10;
    [imgView.layer addAnimation:anima forKey:nil]; //layer层动画
    hud.customView = imgView;
    
    //加上 加载文字
    hud.mode = MBProgressHUDModeText; //显示文字
    hud.label.text = @"加载中……";
    hud.margin = 10.f;
    hud.offset = CGPointMake(0, -100.f);
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1.5f]; //显示周期
    复制代码



更多,参考《源码笔记—MBProgressHUD





##SDCycleScrollView

#import <SDCycleScrollView.h>//无限轮播
复制代码





##SKPSMTPMessage
cocoaPods导入:pod 'skpsmtpmessage' #邮件发送

#import "SKPSMTPMessage.h"          //邮件
#import "NSData+Base64Additions.h"  //邮件
复制代码












参考
三方库大全》:各种三方库的介绍!
CocoaPods》:CocoaPods介绍和使用
Cocoapods导入 常用第三方库》:使用Cocoapods导入三方库
三方库 mistake》:三方库使用的问题及解决

文章持续更新中~~
总结的文章全部分开了,以便总结!






goyohol’s essay

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享