Created
December 13, 2012 08:11
-
-
Save anonymous/4274929 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// UIApplication+Versioning.h | |
// The Joys of Code | |
// | |
// Created by MacBook Pro on 21/09/12. | |
// Copyright (c) 2012 Tall Developments. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@protocol UIApplicationDelegateVersioning <UIApplicationDelegate> | |
@optional | |
- (void) application:(UIApplication *)application willUpdateToVersion: (NSString*) newVersion fromVersion: (NSString*) previousVersion; | |
- (void) application:(UIApplication *)application didUpdateToVersion: (NSString*) newVersion fromVersion: (NSString*) previousVersion; | |
@end | |
@interface UIApplication (Versioning) | |
@end |
This file contains 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
// | |
// UIApplication+Versioning.m | |
// The Joys of Code | |
// | |
// Created by MacBook Pro on 21/09/12. | |
// Copyright (c) 2012 Tall Developments. All rights reserved. | |
// | |
#import "UIApplication+Versioning.h" | |
#import <objc/message.h> | |
#import <objc/runtime.h> | |
static NSString* UIApplicationVersionFileName = @"app.ver"; | |
@implementation UIApplication (Versioning) | |
+ (void) load { | |
Method original, swizzled; | |
original = class_getInstanceMethod(self, @selector(setDelegate:)); | |
swizzled = class_getInstanceMethod(self, @selector(swizzled_setDelegate:)); | |
method_exchangeImplementations(original, swizzled); | |
} | |
- (void) swizzled_setDelegate: (id<UIApplicationDelegate>) delegate { | |
IMP implementation = class_getMethodImplementation([self class], @selector(swizzled_application:didFinishLaunchingWithOptions:)); | |
class_addMethod([delegate class], @selector(swizzled_application:didFinishLaunchingWithOptions:), implementation, "B@:@@"); | |
Method original, swizzled; | |
original = class_getInstanceMethod([delegate class], @selector(application:didFinishLaunchingWithOptions:)); | |
swizzled = class_getInstanceMethod([delegate class], @selector(swizzled_application:didFinishLaunchingWithOptions:)); | |
method_exchangeImplementations(original, swizzled); | |
[self swizzled_setDelegate: delegate]; | |
} | |
- (BOOL)swizzled_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
//Check for a version change | |
NSError* error; | |
NSArray* directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString* versionFilePath = [[directories objectAtIndex: 0] stringByAppendingPathComponent: UIApplicationVersionFileName]; | |
NSString* oldVersion = [NSString stringWithContentsOfFile: versionFilePath | |
encoding: NSUTF8StringEncoding | |
error: &error]; | |
NSString* currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleVersion"]; | |
if( error ) { | |
switch (error.code) { | |
case NSFileReadNoSuchFileError: | |
{ | |
NSLog(@"Warning: first time to access version file -> Creating file"); | |
break; | |
} | |
default: | |
{ | |
NSLog(@"Warning: An error '%@' occured will loading the application version file -> Recreating file", error); | |
break; | |
} | |
} | |
} | |
id<UIApplicationDelegateVersioning> delegate = (id<UIApplicationDelegateVersioning>)[application delegate]; | |
if( ![oldVersion isEqualToString: currentVersion] ) { | |
if ([delegate respondsToSelector: @selector(application:willUpdateToVersion:fromVersion:)]) { | |
//objc_msgSend([application delegate], @selector(application:willUpdateToVersion:fromVersion:), currentVersion, oldVersion); | |
[delegate application: application willUpdateToVersion: currentVersion fromVersion: oldVersion]; | |
} | |
[currentVersion writeToFile: versionFilePath | |
atomically: YES | |
encoding: NSUTF8StringEncoding | |
error: &error]; | |
if ([delegate respondsToSelector: @selector(application:didUpdateToVersion:fromVersion:)]) { | |
[delegate application: application didUpdateToVersion: currentVersion fromVersion: oldVersion]; | |
} | |
} | |
SEL realSelector = @selector(swizzled_application:didFinishLaunchingWithOptions:); | |
return (BOOL) objc_msgSend([application delegate], realSelector, application, launchOptions); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment