Mtnet self-use iOS network library open source

Source: Internet
Author: User

Mtnet self-use iOS network library Open source, for a long time, in a few arrivals shelves of the app running stable and reliable ~ deliberately open source out, interested students can look at Https://github.com/GangWang/MTNET


Mtnet
Mtnet is a very lightweight, powerful iOS HTTP network library that handles a large number of frequently initiated HTTP requests, and is good and stable in the number of arrivals apps that are already in use.

Other network libraries are easy to get started with, but it can be cumbersome to handle more dense and larger file downloads.

Using the download pool to manage all network downloads, you can set the number of simultaneous requests to be downloaded, abort and cancel for a single request, and provide two ways to receive data: Memory receive and disk receive.

Disk receive data will be small chunks of data to write to the disk, to avoid if the download file size caused by the memory crunch

Developers can inherit the Mtdownloadrecvdatafilesystem class, flexibly specify the storage path and store file name of the downloaded file, and provide a way for the file to cache and retrieve the cached file.

Mtnet is the MRC version

Use
Introducing Header Files

#import "MTDownloaderHelper.h"
#import "MTDownloadObjectModel.h"
#import "MTDownloadRequest.h"
#import "MTDownloadRecvData.h"
Declaring a download pool

@property (nonatomic, Strong) mtdownloaderhelper * downloadhelper;

...

Self.downloadhelper = [[Mtdownloaderhelper alloc] init];
Set the maximum number of tasks that can be performed at a download pool

[Self.downloadhelper Setmaxconcurrenttaskcount:5];
You can specify the same download timeout setting for all links in the download pool

[Self.downloadhelper settimeout:20];
Put a download request into the download pool

Mtdownloadrequest * request = [[Mtdownloadrequest alloc] init];
Mtdownloadobjectmodel * Downloadmdel = [[Mtdownloadobjectmodel alloc] init];
Downloadmdel.url = @ "http://www.baidu.com";
Request.downloadmodel = Downloadmdel;

Request.delegate = self;

[Self.downloadhelper Adddownloadrequest:request];
Cancels a download request so that the download request is removed from the download queue without starting the download. Abort the current download if it has started to open

[Self.downloadhelper CancelDownloadRequestByName:request.name];
Get download results using Mtdownloadrequestdelegate

-(void) Didtaskreceivedatafinish: (NSData *) Thedata Request: (mtdownloadrequest*) Request
{
NSLog (@ "Download OK");
}

-(void) taskdatadownloadfailed: (Nserror *) Error Request: (mtdownloadrequest*) Request
{
NSLog (@ "Download failed:%@", error);
}
Custom Receive Download rules

You can inherit mtdownloadrecvdatafilesystem to make the download rules yourself, and in this class are two methods of Processrecvalldatafinish and processrecvalldatafalied. Overrides can be customized to handle errors in the current data download or after successful operation

such as the download of data errors need to clean up the cache, after the successful download needs to extract files, compression, image scaling, etc.

#import "MTDownloadRecvData.h"

@interface Appimagedowloadrecv:mtdownloadrecvdatafilesystem

-(void) Setfilename: (NSString *) fileName;

@end



#import "AppImageDowloadRecv.h"
#import "MTPATH.H"
#import "MTFile.h"
#import "Nsstring+extension.h"
#import "McfDef.h"

@implementation APPIMAGEDOWLOADRECV

-(ID) init
{
self = [super init];
if (self)
{
Self.localcachedirpath = [Mtpath combine:kcache combinepath:@ "AppData"];
Static BOOL created = FALSE;
if (!created) {
created = TRUE;
[Mtpath CreateDirectory:self.localCacheDirPath Withattributes:nil];
}
}

return self;
}

-(void) Setfilename: (NSString *) fileName
{
if ([NSString Isnilorempty:filename])
{
Return
}

Self.name = FileName;
Self.localfullpath = [Mtpath Combine:self.localCacheDirPath combinepath:filename];
}

-(BOOL) bcached
{
return [Mtfile FileExistsAtPath:self.localFullPath];
}

-(NSData *) data
{
if ([Self bcached])
{
return [NSData DataWithContentsOfFile:self.localFullPath];
}

return nil;
}

@end
Put a request for your own definition download rule to the download pool

Mtdownloadrequest * request = [[Mtdownloadrequest alloc] init];

Mtdownloadobjectmodel * Downloadmdel = [[Mtdownloadobjectmodel alloc] init];
Downloadmdel.url = @ "http://www.baidu.com";
Request.downloadmodel = Downloadmdel;

APPIMAGEDOWLOADRECV * RecvData = [[Appimagedowloadrecv alloc] init];
[RecvData setfilename:@ "filename"];
Request.recvdata = RecvData;

Request.delegate = self;

[Self.downloadhelper Adddownloadrequest:request];

Mtnet self-use iOS network library open source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.