Latest 1.0.6
Homepage https://github.com/jezzmemo/JJNetwork
License MIT
Platforms ios 8.0, requires ARC
Dependencies AFNetworking
Authors

CocoaPods Compatible
Carthage Compatible
Platform
Build Status
License MIT

AFNetworking-based network library, with delegate to process network response, integrate more business and optimize network performance,中文使用说明,设计文档

Features

  • [x] Sign the http parameter by your customer key
  • [x] Http cache for the GET and POST
  • [x] Replace the domain to IP address improve performance and change customer http head field
  • [x] Interseptor request and response
  • [x] Support upload files
  • [x] Mock request

Requirements

  • iOS 8.0 or later
  • Xcode 7.3 or later

Installation

Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target 'TargetName' do
pod 'JJNetwork'
end

run the following command:

$ pod install

Installation with Carthage

To integrate JJNetwork into your Xcode project using Carthage, specify it in your Cartfile:

github "jezzmemo/JJNetwork"

Run carthage to build the framework and drag the built AFNetworking.framework,JJNetwork.framework into your Xcode project.

Usage

JJAPIRequest

Every network request extends from JJAPIRequest,and then implement JJRequestInput protocol,overwrite some method.

For example:JJNetwork http://api.imemo8.com/diary.php send GET request,parameter:mod=getHotDiary

#import "JJNetwork.h"

@interface DemoRequest : JJAPIRequest

@end

#import "DemoRequest.h"

@implementation DemoRequest

- (NSString*)requestURL{
    return @"http://api.imemo8.com/diary.php";
}

- (HTTPMethod)requestMethod{
    return JJRequestGET;
}
@end
  • requestURL

Fill whole request URL,this method is required,other is optional

  • requestMethod

return enum,POST,GET,PUT,DELETE,default is GET,if you did not implement

Parameter and Start request

#import "PresentViewController.h"
#import "DemoRequest.h"

@interface PresentViewController ()<JJRequestDelegate>

@property(nonatomic,readwrite,strong)DemoRequest* demoRequest;

@end

@implementation PresentViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.demoRequest startRequest];
}

#pragma mark - Get property

- (DemoRequest*)demoRequest{
    if (_demoRequest != nil) {
        return _demoRequest;
    }
    _demoRequest = [DemoRequest new];
    _demoRequest.delegate = self;
    return _demoRequest;
}

#pragma mark - Request parameter

- (NSDictionary*)requestParameters:(JJAPIRequest *)request{
    return @{@"mod":@"getHotDiary"};
}

#pragma mark - Network response

- (void)responseSuccess:(JJAPIResponse *)response responseData:(id)data{
    NSLog(@"responseSuccess");
}

- (void)responseFail:(JJAPIResponse *)response errorMessage:(NSError *)error{
    NSLog(@"responseFail");
}
@end
  • Invoke startRequest will process network request
  • Implement requestParameters request parameter
  • responseSuccess and responseFail,network response

Sign parameter by the customer key

- (NSString*)signParameterKey{
    return @"key";
}

If implement signParameterKey,request will add two parameters,sign and timestamp,sign = md5(parameters + timestamp + key)

Select cache policy for GET and POST

- (HTTPCachePolicy)requestCachePolicy{
    return JJReloadFromCacheTimeLimit;
}

//UNIT Second
- (NSUInteger)cacheLimitTime{
    return 120;
}
  • JJReloadFromNone: Default mode,request from network
  • JJReloadFromLocalCache: If have cache,will return the cache,if origin source update,will replace new data to old cache
  • JJReloadFromCacheTimeLimit: First time load request origin source,save the cache for the limit time,if expire,will return nil,if origin source update,will replace new data to old cache

If choose JJReloadFromCacheTimeLimit policy,you must implement cacheLimitTime

Final,Extends from JJAPIRequest any class,invoke cacheFromCurrentRequest method,for example:

id cacheData = [self.demoRequest cacheFromCurrentRequest];
NSLog(@"Local cache:%@",cacheData);
//show cache data
[self.demoRequest startRequest];
//request network and refresh UI

Replace the domain to IP address improve performance and change customer http head field

  • JJAPIDominIPModule
@interface DomainModule : NSObject<JJAPIDominIPModule>

@end
@implementation DomainModule
- (NSDictionary*)domainIPData{
    return @{@"api.imemo8.com":@"218.244.140.1"};
}
@end
  • JJAPIHttpHeadModule
@interface HttpHeadModule : NSObject<JJAPIHttpHeadModule>

@end
@implementation HttpHeadModule

- (NSDictionary*)customerHttpHead{
    return @{@"user-token":@"xxxxx",@"device-id":@"xxxxx"};
}

@end

Register module to JJAPIService+Extension

[JJAPIService registerDomainIP:[[DomainModule alloc] init]];
[JJAPIService registerHttpHeadField:[[HttpHeadModule alloc] init]];

Interseptor

  • Implement from JJAPIServiceInterseptor to the instance JJAPIService object:
  • (DemoAPIService*)apiService{
    if (_apiService != nil) {
    return _apiService;
    }
    _apiService = [[DemoAPIService alloc] init];
    _apiService.serviceProtocol = self;
    _apiService.serviceInterseptor = self;
    return _apiService;
    }

  • JJAPIService (Extension)
  • (void)addServiceInterseptor:(id)interseptor forServiceClass:(Class)className;
  • (void)removeServiceInterseptor:(id)interseptor forServiceClass:(Class)className;

For example:

[JJAPIService addServiceInterseptor:self forServiceClass:[DemoAPIService class]];

- (void)beforeRequest:(JJAPIRequest*)request{
    NSLog(@"beforeRequest");
}

- (void)afterRequest:(JJAPIRequest*)request{
    NSLog(@"afterRequest");
}

- (void)response:(JJAPIResponse*)response beforeResponseData:(id)data{
    NSLog(@"beforeResponse");
}

- (void)response:(JJAPIResponse*)response afterResponseData:(id)data{
    NSLog(@"afterResponse");
}
  • Control Loading show/hide
  • AOP Request

Upload files

Support upload one or more files,UploadFileDemoRequest’s demo:


#import <JJNetwork/JJNetwork.h>

@interface UploadFileDemoRequest : JJAPIRequest

@end

@implementation UploadFileDemoRequest

- (NSString*)requestURL{
    return @"http://api.imemo8.com/xxxx.php";
}

- (HTTPMethod)requestMethod{
    return JJRequestPOST;
}

- (JJUploadFileBlock)requestFileBody{
    return ^(id<JJUploadFileBody> fileBody){
        NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
        [fileBody addFileURL:[NSURL fileURLWithPath:filePath] name:@"file" fileName:@"backup" mimeType:@"json"];
    };
}

@end

ViewController’s Demo:


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.demoRequest startRequest];
}

#pragma mark - Upload file

- (NSDictionary*)requestParameters:(JJAPIRequest *)request{
    return @{@"mod":@"upload"};
}

#pragma mark - Get property

- (UploadFileDemoRequest*)demoRequest{
    if (_demoRequest != nil) {
        return _demoRequest;
    }
    _demoRequest = [UploadFileDemoRequest new];
    _demoRequest.delegate = self;
    return _demoRequest;
}

Implement requestFileBody method,add file information,This is upload file by HTTP, it is recommended to upload a smaller file

Mock request

Setup mock request and add mock request:

-(instancetype)init{
    self = [super init];
    if (self) {
        [self setupMockConfing];
        [self setupMockRequest];
    }
    return self;
}

- (void)setupMockConfing{
    //Global settings
    JJAPIMock.mockSwitch = NO;
}

- (void)setupMockRequest{
    //Add mock request 
    [JJAPIMock testRequest:[DemoRequest class] responseString:@"<HTML></HTML>" isOn:YES];
}

License

JJNetwork is released under the MIT license. See LICENSE for details.

Latest podspec

{
    "name": "JJNetwork",
    "version": "1.0.6",
    "summary": "JJNetwork.",
    "description": "iOS Netwok Library based on AFNetworking",
    "homepage": "https://github.com/jezzmemo/JJNetwork",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Jezz": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/jezzmemo/JJNetwork.git",
        "tag": "1.0.6"
    },
    "public_header_files": "JJNetwork/Source/JJNetwork.h",
    "source_files": "JJNetwork/Source/JJNetwork.h",
    "requires_arc": true,
    "dependencies": {
        "AFNetworking": [
            "~> 3.0"
        ]
    },
    "subspecs": [
        {
            "name": "Category",
            "source_files": "JJNetwork/Source/Category/*.{h,m}",
            "public_header_files": "JJNetwork/Source/Category/*.h"
        },
        {
            "name": "Request",
            "source_files": "JJNetwork/Source/Request/*.{h,m}",
            "public_header_files": "JJNetwork/Source/Request/*.h"
        },
        {
            "name": "ThirdParty",
            "source_files": "JJNetwork/Source/ThirdParty/*.{h,m}",
            "public_header_files": "JJNetwork/Source/ThirdParty/*.h",
            "frameworks": "AFNetworking"
        },
        {
            "name": "Cache",
            "dependencies": {
                "JJNetwork/Category": []
            },
            "source_files": "JJNetwork/Source/Cache/*.{h,m}",
            "public_header_files": "JJNetwork/Source/Cache/*.h"
        },
        {
            "name": "APIService",
            "dependencies": {
                "JJNetwork/Category": [],
                "JJNetwork/Request": [],
                "JJNetwork/ThirdParty": [],
                "JJNetwork/Cache": []
            },
            "source_files": "JJNetwork/Source/APIService/*.{h,m}",
            "public_header_files": "JJNetwork/Source/APIService/*.h"
        }
    ]
}

Pin It on Pinterest

Share This