To ensure compatibility of mobile terminals
urlsession that cannot use ios
The curl Library of C language can be used to implement the download operation.
Compilation of curl in ios environment
Because I compiled openssl before, I took it directly and used it.
openssl is used to increase https support
After compilation is passed, it is very simple to use.
Use code
// // ViewController.m // libcurlHTTPS // // Created by kai_leedarson on 2018/10/29. // Copyright 2018 maple_leedarson. All rights reserved. // #import "ViewController.h" #include <curl/curl.h> #import "JsonDictExchange.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. curl_version_info_data *data= curl_version_info(CURLVERSION_NOW); printf("\nopenssl version %s\n",data->ssl_version); } static int ProgressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) { static int pre_percent =0; if ( dltotal > -0.1 && dltotal < 0.1 ) return 0; int nPos = (int) ( (dlnow/dltotal)*100 ); pre_percent = nPos; NSLog(@"%d%%",pre_percent); return 0; } bool getUrl(const char *filename,char *url) { CURL *curl; CURLcode res; FILE *fp; if ((fp = fopen(filename, "wt+")) == NULL){ // Return results are stored in files return false; } struct curl_slist *headers = NULL; //Add HTTP header // headers = curl_slist_append(headers, "Accept:application/json"); // headers = curl_slist_append(headers, "Content-Type:application/json"); // headers = curl_slist_append(headers, "charset:utf-8"); curl = curl_easy_init(); // Initialization if (curl) { // curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); //change protocol header curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); curl_easy_setopt(curl, CURLOPT_URL,url); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, ProgressCallback); NSLog(@"request"); res = curl_easy_perform(curl); // implement NSLog(@"The request return value is%i",res); curl_slist_free_all(headers); curl_easy_cleanup(curl); } fclose(fp); return true; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self testWeb]; } - (void)testWeb { NSString * path0 = [[NSBundle bundleForClass:[self class]] pathForResource:[NSString stringWithUTF8String:"testCloud.json"] ofType:nil]; NSData *receiveData = [[NSData alloc] initWithContentsOfFile:path0]; //The obtained json is first converted into a string NSString *receiveStr = [[NSString alloc]initWithData:receiveData encoding:NSUTF8StringEncoding]; //Re analysis NSDictionary *jsonDict = [JsonDictExchange convertJsonWith:receiveStr]; NSMutableArray *dataArray = [jsonDict[@"data"] mutableCopy]; for (NSInteger i=0; i<dataArray.count; i++) { NSMutableDictionary *info = [dataArray[i] mutableCopy]; NSLog(@"Start downloading[%ld]url\n%@",(long)i,info[@"url"]); NSString *downloadPath = info[@"url"]; NSArray *array = [info[@"url"] componentsSeparatedByString:@"/"]; NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:array.lastObject]; //6.3 Perform clipping char *charPath1 = (char *)[downloadPath UTF8String]; char *charPath2 = (char *)[fullPath UTF8String]; getUrl(charPath2,charPath1); } } @end
Project structure, remember to remove bitcode as no and add a libz Library
Download Effect Pictures
I only compiled the real-time versions of armv7 and arm64, and I had to compile the rest myself.
At present, there is no problem of using multithreaded download to speed up the process.