You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ac...@apache.org on 2017/05/17 02:40:34 UTC
[2/7] incubator-weex git commit: * [ios] use resource handler
download iconfont
* [ios] use resource handler download iconfont
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/3272a237
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/3272a237
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/3272a237
Branch: refs/heads/0.13-dev
Commit: 3272a23794fe7480df9ae627bb95d7477f922069
Parents: f803104
Author: acton393 <zh...@gmail.com>
Authored: Tue May 16 15:44:28 2017 +0800
Committer: acton393 <zh...@gmail.com>
Committed: Tue May 16 15:44:28 2017 +0800
----------------------------------------------------------------------
ios/sdk/WeexSDK/Sources/Utility/WXUtility.m | 82 +++++++++++++-----------
1 file changed, 46 insertions(+), 36 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3272a237/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
index d2c2e1a..3aae39f 100644
--- a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
@@ -25,6 +25,8 @@
#import "WXRuleManager.h"
#import "WXSDKEngine.h"
#import "WXConvert.h"
+#import "WXResourceRequest.h"
+#import "WXResourceLoader.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import <sys/utsname.h>
@@ -539,44 +541,52 @@ static BOOL WXNotStat;
+ (void)getIconfont:(NSURL *)url completion:(void(^)(NSURL *url, NSError *error))completionBlock
{
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- if ([url isFileURL]) {
- // local file url
- NSError * error = nil;
- if (![WXUtility isFileExist:url.path]) {
- error = [NSError errorWithDomain:WX_ERROR_DOMAIN code:-1 userInfo:@{@"errMsg":[NSString stringWithFormat:@"local font %@ is't exist", url.absoluteString]}];
- }
- completionBlock(url, error);
- return;
+ if ([url isFileURL]) {
+ // local file url
+ NSError * error = nil;
+ if (![WXUtility isFileExist:url.path]) {
+ error = [NSError errorWithDomain:WX_ERROR_DOMAIN code:-1 userInfo:@{@"errMsg":[NSString stringWithFormat:@"local font %@ is't exist", url.absoluteString]}];
}
- // remote font url
- NSURLSession *session = [NSURLSession sharedSession];
- NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
- NSURL * downloadPath = nil;
- NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse*)response;
- if (200 == httpResponse.statusCode && !error && location) {
- NSString *file = [NSString stringWithFormat:@"%@/%@",WX_FONT_DOWNLOAD_DIR,[WXUtility md5:[url absoluteString]]];
- downloadPath = [NSURL fileURLWithPath:file];
- NSFileManager *mgr = [NSFileManager defaultManager];
- NSError * error ;
- if (![mgr fileExistsAtPath:[file stringByDeletingLastPathComponent]]) {
- // create font cache directory and its parent if not exist
- [mgr createDirectoryAtPath:[file stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error];
- }
- BOOL result = [mgr moveItemAtURL:location toURL:downloadPath error:&error];
- if (!result) {
- downloadPath = nil;
- }
- } else {
- if (200 != httpResponse.statusCode) {
- error = [NSError errorWithDomain:WX_ERROR_DOMAIN code:-1 userInfo:@{@"ErrorMsg": [NSString stringWithFormat:@"can not load the font url %@ ", url.absoluteString]}];
- }
+ completionBlock(url, error);
+ return;
+ }
+
+ WXResourceRequest *request = [WXResourceRequest requestWithURL:url resourceType:WXResourceTypeFont referrer:@"" cachePolicy:NSURLRequestUseProtocolCachePolicy];
+
+ request.userAgent = [self userAgent];
+ WXResourceLoader *iconfontLoader = [[WXResourceLoader alloc] initWithRequest:request];
+ iconfontLoader.onFinished = ^(const WXResourceResponse * response, NSData * data) {
+ NSURL * downloadPath = nil;
+ NSError * error = nil;
+ NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse*)response;
+ if (200 == httpResponse.statusCode) {
+ NSString *file = [NSString stringWithFormat:@"%@/%@",WX_FONT_DOWNLOAD_DIR,[WXUtility md5:[url absoluteString]]];
+ downloadPath = [NSURL fileURLWithPath:file];
+ NSFileManager *mgr = [NSFileManager defaultManager];
+
+ if (![mgr fileExistsAtPath:[file stringByDeletingLastPathComponent]]) {
+ // create font cache directory and its parent if not exist
+ [mgr createDirectoryAtPath:[file stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error];
}
- completionBlock(downloadPath, error);
- }];
-
- [task resume];
- });
+
+ BOOL result = [data writeToFile:downloadPath atomically:NO];
+ if (!result) {
+ downloadPath = nil;
+ }
+ } else {
+ if (200 != httpResponse.statusCode) {
+ error = [NSError errorWithDomain:WX_ERROR_DOMAIN code:-1 userInfo:@{@"ErrorMsg": [NSString stringWithFormat:@"can not load the font url %@ ", url.absoluteString]}];
+ }
+ }
+ completionBlock(downloadPath, error);
+
+ };
+
+ iconfontLoader.onFailed = ^(NSError* error) {
+ completionBlock(nil, error);
+ };
+
+ [iconfontLoader start];
}
+ (BOOL)isFileExist:(NSString *)filePath