Permalink
Browse files

ASBasicImageDownloader iOS 7 support

On iOS 7, NSURLSessionTask is actually a __NSCFURLSessionTask (private class) and our NSURLSessionTask category to add the associated object asyncdisplaykit_metadata was crashing on iOS 7 stating that there was no method. This commit changes the category to extend NSURLRequest as well as change NSURLSessionTask to NSURLSessionDownloadTask.
  • Loading branch information...
1 parent db085c4 commit c6459356312d3f77cd6d3cfbe9d27605eeeed78c @rnystrom rnystrom committed Dec 4, 2014
@@ -31,13 +31,13 @@ @implementation ASBasicImageDownloaderMetadata
#pragma mark -
/**
- * NSURLSessionTask lacks a `userInfo` property, so add this association ourselves.
+ * NSURLSessionDownloadTask lacks a `userInfo` property, so add this association ourselves.
*/
-@interface NSURLSessionTask (ASBasicImageDownloader)
+@interface NSURLRequest (ASBasicImageDownloader)
@property (nonatomic, strong) ASBasicImageDownloaderMetadata *asyncdisplaykit_metadata;
@end
-@implementation NSURLSessionTask (ASBasicImageDownloader)
+@implementation NSURLRequest (ASBasicImageDownloader)
static const char *kMetadataKey = NSStringFromClass(ASBasicImageDownloaderMetadata.class).UTF8String;
- (void)setAsyncdisplaykit_metadata:(ASBasicImageDownloaderMetadata *)asyncdisplaykit_metadata
{
@@ -85,14 +85,14 @@ - (id)downloadImageWithURL:(NSURL *)URL
completion:(void (^)(CGImageRef, NSError *))completion
{
// create download task
- NSURLSessionTask *task = [_session downloadTaskWithURL:URL];
+ NSURLSessionDownloadTask *task = [_session downloadTaskWithURL:URL];
// associate metadata with it
ASBasicImageDownloaderMetadata *metadata = [[ASBasicImageDownloaderMetadata alloc] init];
metadata.callbackQueue = callbackQueue ?: dispatch_get_main_queue();
metadata.downloadProgressBlock = downloadProgressBlock;
metadata.completionBlock = completion;
- task.asyncdisplaykit_metadata = metadata;
+ task.originalRequest.asyncdisplaykit_metadata = metadata;
// start downloading
[task resume];
@@ -107,8 +107,8 @@ - (void)cancelImageDownloadForIdentifier:(id)downloadIdentifier
return;
}
- ASDisplayNodeAssert([downloadIdentifier isKindOfClass:NSURLSessionTask.class], @"unexpected downloadIdentifier");
- NSURLSessionTask *task = (NSURLSessionTask *)downloadIdentifier;
+ ASDisplayNodeAssert([downloadIdentifier isKindOfClass:NSURLSessionDownloadTask.class], @"unexpected downloadIdentifier");
+ NSURLSessionDownloadTask *task = (NSURLSessionDownloadTask *)downloadIdentifier;
[task cancel];
}
@@ -121,7 +121,7 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
- ASBasicImageDownloaderMetadata *metadata = downloadTask.asyncdisplaykit_metadata;
+ ASBasicImageDownloaderMetadata *metadata = downloadTask.originalRequest.asyncdisplaykit_metadata;
if (metadata.downloadProgressBlock) {
metadata.downloadProgressBlock((CGFloat)totalBytesWritten / (CGFloat)totalBytesExpectedToWrite);
}
@@ -133,7 +133,7 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
- ASBasicImageDownloaderMetadata *metadata = downloadTask.asyncdisplaykit_metadata;
+ ASBasicImageDownloaderMetadata *metadata = downloadTask.originalRequest.asyncdisplaykit_metadata;
if (metadata.completionBlock) {
dispatch_async(metadata.callbackQueue, ^{
metadata.completionBlock(image.CGImage, nil);
@@ -142,10 +142,10 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
}
// invoked unconditionally
-- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionDownloadTask *)task
didCompleteWithError:(NSError *)error
{
- ASBasicImageDownloaderMetadata *metadata = task.asyncdisplaykit_metadata;
+ ASBasicImageDownloaderMetadata *metadata = task.originalRequest.asyncdisplaykit_metadata;
if (metadata && error) {
dispatch_async(metadata.callbackQueue, ^{
metadata.completionBlock(NULL, error);
@@ -311,6 +311,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Sample/Info.plist;
+ IPHONEOS_DEPLOYMENT_TARGET = 7.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
};
@@ -322,6 +323,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Sample/Info.plist;
+ IPHONEOS_DEPLOYMENT_TARGET = 7.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
};

0 comments on commit c645935

Please sign in to comment.