Created
March 14, 2014 22:12
-
-
Save Daniel1of1/9558067 to your computer and use it in GitHub Desktop.
A quick and dirty fix / fun with hopper for coins app
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
@interface NSURLSession (DHCDirtyCoinsFix) | |
//lazy way to keep compiler happy | |
+(void)setLoading:(BOOL)isLoading; | |
-(UIButton *)updateButton; | |
- (void)_updateTimerPaused:(BOOL)on; | |
@end | |
@implementation NSURLSession (DHCDirtyCoinsFix) | |
//lazy way to keep compiler happy | |
+(void)setLoading:(BOOL)isLoading { | |
} | |
-(UIButton *)updateButton{ | |
return nil; | |
} | |
- (void)_updateTimerPaused:(BOOL)on{ | |
} | |
static NSURLSessionDownloadTask * (*OriginalDownloadedTaskWithRequestCompletionHandler)(id, SEL, NSURLRequest *, void (^completionHandler)(NSURL *location, NSURLResponse *response, NSError *error)); | |
+ (void)load | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
Class class = [NSURLSession sharedSession].class; | |
SEL originalSelector = @selector(downloadTaskWithRequest:completionHandler:); | |
IMP replacement = imp_implementationWithBlock(^NSURLSessionDownloadTask *(id _self, NSURLRequest *request, void (^completionHandler)(NSURL *location, NSURLResponse *response, NSError *error)){ | |
void (^fixedCompletionHandlerBlock)(NSURL *location, NSURLResponse *response, NSError *error) = ^(NSURL *location, NSURLResponse *response, NSError *error) { | |
// This is basically the part that needs to implemented | |
// if there's an error a nil data value can be passed to the +JSONObjectWithData:options:error: | |
//which throws the relevant exception | |
if (error) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
//This is the terrible part, how does one reference | |
//the self that would have been referenced in the original creation of the completionHandler? | |
//BTCValueViewController instance (hopefully) set loading 0 | |
if ([[(UINavigationController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController] viewControllers][0] respondsToSelector:@selector(setLoading:)]) { | |
[[(UINavigationController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController] viewControllers][0] setLoading:0]; | |
//BTCValueViewController display unobtrusive error message | |
[[[(UINavigationController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController] viewControllers][0] updateButton] setTitle:@"There was a problem updating" forState:UIControlStateNormal]; | |
} | |
}); | |
return; | |
} | |
completionHandler(location,response,error); | |
}; | |
return OriginalDownloadedTaskWithRequestCompletionHandler(_self, _cmd, request, fixedCompletionHandlerBlock); | |
}); | |
IMP *store = (IMP *)&OriginalDownloadedTaskWithRequestCompletionHandler; | |
IMP originalImp = NULL; | |
Method method = class_getInstanceMethod(class, originalSelector); | |
if (method) { | |
const char *type = method_getTypeEncoding(method); | |
originalImp = class_replaceMethod(class, originalSelector, replacement, type); | |
if (!originalImp) { | |
originalImp = method_getImplementation(method); | |
} | |
} | |
if (originalImp && store) { *store = originalImp; } | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment