Last active
December 4, 2019 02:26
-
-
Save hanfengs/faa99708abff159279ba16dc01fa639d to your computer and use it in GitHub Desktop.
[下载]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 下载、解压加载文件 | |
- (void)getGamesZipHtml:(NSString *)urlStr | |
{ | |
self.activityIndicator.hidden = NO; | |
NSLog(@"接口zip = %@",urlStr); | |
NSURL *URL = [NSURL URLWithString:urlStr]; | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); | |
NSString *documentsDirectory = [documents objectAtIndex:0]; | |
// 创建games路径,没有就直接先创建 | |
NSString *unZipPath = [documentsDirectory stringByAppendingPathComponent:@"games/game"]; | |
if (![fileManager fileExistsAtPath:unZipPath]) { | |
[fileManager createDirectoryAtPath:unZipPath withIntermediateDirectories:YES attributes:nil error:nil]; | |
} | |
// NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:unZipPath error:nil]; | |
// 判断游戏文件是否存在,存在就直接加载 | |
NSString *htmlPath = [NSString stringWithFormat:@"%@/index.html",unZipPath]; | |
// NSLog(@"%@",htmlPath); | |
if ([fileManager fileExistsAtPath:htmlPath]) { | |
self.index = 2; | |
self.progressView.hidden = NO; | |
[self.progressView setProgress:1.0 animated:YES]; | |
// 显示加载进度 | |
self.loadingLabel.text = [NSString stringWithFormat:@"当前加载进度:100%@",@"%"]; | |
// 加载webview地址 | |
NSURL *fileUrl = [NSURL fileURLWithPath:htmlPath]; | |
[self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]]; | |
self.isLoading = NO; | |
return; | |
} | |
// 数据请求 | |
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
//AFN3.0+基于封住URLSession的句柄 | |
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; | |
//请求 | |
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; | |
//下载Task操作 | |
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { | |
// 回到主队列刷新UI | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
NSLog(@"当前进度 = %f",(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount)); | |
NSString *jsStr = [NSString stringWithFormat:@"native.getProgressPercent('%f')",(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount) * 100]; | |
[self.webView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) { | |
// NSLog(@"%@----%@",result, error); | |
}]; | |
CGFloat jindu = (1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount); | |
self.progressView.hidden = NO; | |
[self.progressView setProgress:jindu animated:YES]; | |
self.loadingLabel.text = [NSString stringWithFormat:@"当前加载进度:%.2f%@",(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount) * 100,@"%"]; | |
}); | |
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { | |
//- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径 | |
NSString *zipPath = [documentsDirectory stringByAppendingPathComponent:@"games/zipFile"]; | |
if ([fileManager fileExistsAtPath:zipPath]) { | |
[[NSFileManager defaultManager] removeItemAtPath:zipPath error:nil]; | |
} | |
[fileManager createDirectoryAtPath:zipPath withIntermediateDirectories:YES attributes:nil error:nil]; | |
NSString *path = [zipPath stringByAppendingPathComponent:response.suggestedFilename]; | |
// NSLog(@"zip path == %@", path); | |
return [NSURL fileURLWithPath:path]; | |
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { | |
// filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用 | |
NSString *zipPath = [filePath path];// 将NSURL转成NSString | |
NSLog(@"unZipPath == %@",unZipPath); //解压后的路径 | |
//解压zip游戏文件包,加载游戏webveiw | |
if ([fileManager fileExistsAtPath:htmlPath]) { | |
NSURL *fileUrl = [NSURL fileURLWithPath:htmlPath]; | |
[self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]]; | |
self.isLoading = NO; | |
return; | |
}else{ | |
if ([SSZipArchive unzipFileAtPath:zipPath toDestination:unZipPath]) { | |
self.index = 2; | |
// NSLog(@"YES"); | |
// NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:unZipPath error:nil]; | |
NSURL *fileUrl = [NSURL fileURLWithPath:htmlPath]; | |
[self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]]; | |
self.isLoading = NO; | |
}else{ | |
// NSLog(@"NO"); | |
} | |
} | |
}]; | |
[downloadTask resume]; | |
} | |
self.index = 2; | |
self.isLoading = NO; | |
NSURL *fileUrl = [NSURL fileURLWithPath:htmlPath]; | |
// NSURL *fileUrl =[NSURL URLWithString:@"https://www.baidu.com"]; | |
[self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]]; | |
NSURL * accessUrl = [[NSURL fileURLWithPath:htmlPath] URLByDeletingLastPathComponent]; | |
[self.webView loadFileURL:[NSURL URLWithString:htmlPath] allowingReadAccessToURL:accessUrl]; | |
NSString *htmlString2 = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil]; | |
[self.webView loadHTMLString:htmlString2 baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/", DSGameDirectory(key)]]]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment