Skip to content

Instantly share code, notes, and snippets.

@nicolastinkl
Created December 14, 2016 02:44
Show Gist options
  • Save nicolastinkl/ed78dd7f31746e42595a4f8982a19f9e to your computer and use it in GitHub Desktop.
Save nicolastinkl/ed78dd7f31746e42595a4f8982a19f9e to your computer and use it in GitHub Desktop.
wechat openurl
//// 检测微信OpenURL时回传参数和回调逻辑
@implementation UIApplication(MethodSwizzing)
static void MySetFrame(id self, SEL _cmd, NSURL* url);
static void (*SetFrameIMP)(id self, SEL _cmd, NSURL* url);
static void MySetFrame(id self, SEL _cmd, NSURL* url) {
// do custom work
NSLog(@" url : %@",url);
SetFrameIMP(self, _cmd, url);
}
+ (void)load {
[self swizzle:@selector(openURL:) with:(IMP)MySetFrame store:(IMP *)&SetFrameIMP];
}
typedef IMP *IMPPointer;
BOOL class_swizzleMethodAndStore(Class class, SEL original, IMP replacement, IMPPointer store) {
IMP imp = NULL;
Method method = class_getInstanceMethod(class, original);
if (method) {
const char *type = method_getTypeEncoding(method);
imp = class_replaceMethod(class, original, replacement, type);
if (!imp) {
imp = method_getImplementation(method);
}
}
if (imp && store) { *store = imp; }
return (imp != NULL);
}
+ (BOOL)swizzle:(SEL)original with:(IMP)replacement store:(IMPPointer)store {
return class_swizzleMethodAndStore(self, original, replacement, store);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment