Skip to content

Instantly share code, notes, and snippets.

@edewit
Created November 25, 2014 10:35
Show Gist options
  • Save edewit/03199e68aeb094b8a27a to your computer and use it in GitHub Desktop.
Save edewit/03199e68aeb094b8a27a to your computer and use it in GitHub Desktop.
#import "AppDelegate+url.h"
#import "AeroGearHttp.h"
#import <objc/runtime.h>
@implementation AppDelegate (url)
// its dangerous to override a method from within a category.
// Instead we will use method swizzling. we set this up in the load call.
+ (void)load
{
Method original, swizzled;
original = class_getInstanceMethod(self, @selector(application:openURL:sourceApplication:annotation:));
swizzled = class_getInstanceMethod(self, @selector(swizzled_application:openURL:sourceApplication:annotation:));
method_exchangeImplementations(original, swizzled);
}
- (BOOL)swizzled_application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
NSNotification *notification = [NSNotification notificationWithName:@"AGAppLaunchedWithURLNotification" object:nil userInfo:[NSDictionary dictionaryWithObject:url forKey:UIApplicationLaunchOptionsURLKey]];
[[NSNotificationCenter defaultCenter] postNotification:notification];
return [self swizzled_application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}
@end
@edewit
Copy link
Author

edewit commented Nov 25, 2014

I would think this would be the equivalent in swift, but it seems I'm wrong

extension AppDelegate: NSObject, UIApplicationDelegate {
    init() {
        var originalMethod = class_getClassMethod(self, Selector("application:openURL:sourceApplication:annotation:"))
        var swizzledMethod = class_getClassMethod(self, Selector("swizzled_application:openURL:sourceApplication:annotation:"))

        method_exchangeImplementations(originalMethod, swizzledMethod)
    }

    func swizzled_application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
        let notification = NSNotification(name: AGAppLaunchedWithURLNotification, object:nil, userInfo:[UIApplicationLaunchOptionsURLKey:url])
        NSNotificationCenter.defaultCenter().postNotification(notification)
        return swizzled_application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
    }
}

Error: can not use init

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment