update freebee method

This commit is contained in:
Pan, Xinlei (Ryan) 2018-05-10 11:32:08 -04:00
parent 374ae5973a
commit 1e7fb38f17
3 changed files with 19 additions and 42 deletions

View File

@ -44,8 +44,7 @@ typedef void(^FreebeeLoadFinishedHandler)(MVMCoreOperation* _Nullable freebeeOpe
- (BOOL)isFreeBeeAuthorizedValidUrl:(nullable NSURL*)url; - (BOOL)isFreeBeeAuthorizedValidUrl:(nullable NSURL*)url;
- (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId; - (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId;
- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; - (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url;
- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *_Nullable)imageURL;
- (void)configureFreeBeeWithDict:(nullable NSDictionary*)configDict withSessionReset:(BOOL)isReset; - (void)configureFreeBeeWithDict:(nullable NSDictionary*)configDict withSessionReset:(BOOL)isReset;
@end @end

View File

@ -230,53 +230,31 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) {
} }
#pragma mark FreeBee Helper for NSData #pragma mark FreeBee Helper for NSData
- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *)url { - (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *)url {
NSData* data = nil; NSData* data = nil;
NSDictionary* proxyDict = [self proxyDictionaryforUrl:url]; NSDictionary* proxyDict = [self proxyDictionaryforUrl:url];
NSURLResponse* response = nil;
NSError* error = nil;
if ([self isFreeBeeEnabled] && if ([self isFreeBeeEnabled] &&
[self isValidCampaign] && proxyDict && [self isValidCampaign] && proxyDict &&
![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) { ![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) {
MVMCoreLog(@"Free Data Url, %@", url); MVMCoreLog(@"Free Data Url, %@", url);
NSURLResponse* response = nil; data = [self sendSynchronousRequest:url returningResponse:&response error:&error];
NSError* error = nil;
data = [self sendSynchronousRequest:url returningResponse:&response error:&error];
if (error)
data = nil;
MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length);
} else {
data = [NSData dataWithContentsOfURL:url];
}
return data;
}
- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *)imageURL {
NSData* data = nil;
NSDictionary* proxyDict = [self proxyDictionaryforUrl:imageURL];
if ([self isFreeBeeEnabled] &&
[self isValidCampaign] && proxyDict &&
![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) {
MVMCoreLog(@"Free Image Data Url, %@", imageURL);
NSURLResponse* response = nil;
NSError* error = nil;
data = [self sendSynchronousRequest:imageURL returningResponse:&response error:&error];
if (error) { if (error) {
data = nil; data = nil;
} }
MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length);
MVMCoreLog(@"freebee_dataWithImageURL:Free Image Data, %lu", (unsigned long)data.length); } else {
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; //create response for identify
return cachedResponse; response = [[NSURLResponse alloc] initWithURL:url MIMEType:nil expectedContentLength:100 textEncodingName:nil];
data = [NSData dataWithContentsOfURL:url];
} }
return nil; NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
return cachedResponse;
} }
#pragma mark - FreeBee Registration #pragma mark - FreeBee Registration

View File

@ -459,11 +459,12 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = nil; NSData *imageData = nil;
if (pathOrName.length > 0) { if (pathOrName.length > 0) {
if(pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) { if (pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) {
// Gets the full path // Gets the full path
NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:CGRectNull flipImage:NO]; NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:CGRectNull flipImage:NO];
if (s7URL) { if (s7URL) {
[self downloadImage:s7URL isGif:YES fallbackImageName:fallbackImageName completionHandler:completionHandler]; [self downloadImage:s7URL isGif:YES fallbackImageName:fallbackImageName completionHandler:completionHandler];
return;
} }
} else { } else {
// Try Local. Check in memory cache // Try Local. Check in memory cache
@ -514,16 +515,15 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
} }
//download image //download image
- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ - (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
NSURLCache *sharedCache = [NSURLCache sharedURLCache]; NSURLCache *sharedCache = [NSURLCache sharedURLCache];
NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [MVMCoreSessionObject sharedGlobal].session;
configure.URLCache = sharedCache;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configure];
NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut]; NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut];
//use Freebee to download image first, then store image to cache //use Freebee to download image first, then store image to cache
NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request]; NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request];
if (!cachedResponse.data) { if (!cachedResponse.data) {
cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL]; cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL];
if (cachedResponse) { if (cachedResponse) {
//system stores cache based datatask and check cache based on response, //system stores cache based datatask and check cache based on response,
//need to manually store cache for response //need to manually store cache for response