Created
August 7, 2014 01:34
-
-
Save oa414/49e673ac3eb5e0d09126 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// SXHTTPHelper.h | |
// sx | |
// | |
// Created by xiangyu on 4/1/14. | |
// Copyright (c) 2014 xiangyu. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface SXSplashHelper : NSObject | |
+(NSURLRequest *)urlRequestFromString:(NSString *)string; | |
+(void) checkNewSplashImage; | |
+(UIImage *) splashImage; | |
@end |
This file contains hidden or 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
// | |
// SXHTTPHelper.m | |
// sx | |
// | |
// Created by xiangyu on 4/1/14. | |
// Copyright (c) 2014 xiangyu. All rights reserved. | |
// | |
#import "SXSplashHelper.h" | |
#define kSplashImageURLKey @"splash" | |
#define kSplashImageRemoteURLKey @"splash-url" | |
@implementation SXSplashHelper | |
+(NSURLRequest *)urlRequestFromString:(NSString *)string{ | |
NSURL *url = [NSURL URLWithString:string]; | |
return [NSURLRequest requestWithURL:url]; | |
} | |
/** | |
* 检查最新启动图片,如果与已有不同则下载 | |
*/ | |
+(void) checkNewSplashImage{ | |
// [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; | |
NSString *url; | |
if (IS_IPHONE_5) { | |
url = @""; | |
}else { | |
url = @""; | |
} | |
__block NSString *imageUrl = nil; | |
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; | |
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
DebugLog(@"JSON: %@", responseObject); | |
imageUrl = APIRemoteFromPath(responseObject[@"url"]); | |
[self downloadImage:imageUrl]; | |
if (![[[NSUserDefaults standardUserDefaults] stringForKey:kSplashImageRemoteURLKey] isEqualToString:imageUrl]){ | |
[self downloadImage:imageUrl]; | |
}else { | |
DebugLog(@"Slpash image not changed!"); | |
} | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
DebugLog(@"Error: %@", error); | |
}]; | |
} | |
/** | |
* 下载启动图片到 Document/图片名 | |
* | |
* @param imageUrl 图片HTTP地址 | |
*/ | |
+(void)downloadImage : (NSString *)imageUrl{ | |
DebugLog(@"Download Image .... %@", imageUrl); | |
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
AFURLSessionManager *sm = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; | |
NSURL *URL = [NSURL URLWithString:imageUrl]; | |
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; | |
NSURLSessionDownloadTask *downloadTask = [sm downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { | |
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; | |
NSURL *localUrl = [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; | |
return localUrl; | |
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { | |
DebugLog(@"File downloaded to: %@", filePath); | |
[[NSUserDefaults standardUserDefaults] setObject:imageUrl forKey:kSplashImageRemoteURLKey]; | |
[[NSUserDefaults standardUserDefaults] setURL:filePath forKey:kSplashImageURLKey]; | |
[[NSUserDefaults standardUserDefaults] synchronize]; | |
}]; | |
[downloadTask resume]; | |
} | |
/** | |
* 获取启动图片 | |
* | |
* @return 启动图片UIImage | |
*/ | |
+(UIImage *) splashImage{ | |
NSURL *url = [[NSUserDefaults standardUserDefaults] URLForKey:kSplashImageURLKey]; | |
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment