考虑到上述用例,我该如何检索用于启动应用程序的URL(如果有的话),或者拦截“openUrl”' Applications AppDelegate实现的方法?
答案 0 :(得分:1)
您可以创建自己的实现UIApplicationDelegate
协议的类。在SDK的启动代码中(用户必须尽早调用application:willFinishLaunchingWithOptions:
之类的代码),实例化它,并用您的自定义对象替换主UIApplication对象的delegate
。
您的自定义对象现在是应用程序委托,而不是他们的。
在设置UIApplication对象的委托之前,将类的委托设置为当前的UIApplication委托,以便您可以将任何消息转发回其应用程序委托对象。
不需要调整。
答案 1 :(得分:1)
这实际上非常简单,将SDK链接为DyLib,并在" 属性((构造函数))中初始化主对象"部分,以下是需要的:
IMP g_originalSetDelegate = nil;
IMP g_originalOpenUrl = nil;
BOOL swizzle_openUrl(id self, SEL _cmd, UIApplication* application, NSURL* url, NSString* sourceApplication, id annotation) {
// TBD: Do your magic
return ((BOOL(*)(id,SEL,UIApplication*,NSURL*,NSString*,id))g_originalOpenUrl)(self, _cmd, application, url, sourceApplication, annotation);
}
void swizzle_setDelegate(id self, SEL _cmd, id delegate) {
((void(*)(id,SEL,id))g_originalSetDelegate)(self, _cmd, delegate);// Call the original method
auto method = class_getInstanceMethod([delegate class], @selector(application:openURL:sourceApplication:annotation:));
g_originalOpenUrl = method_getImplementation(method);
method_setImplementation(method, (IMP)swizzle_openUrl);
}
-(id)init {
auto method = class_getInstanceMethod([UIApplication class], @selector(setDelegate:));
g_originalSetDelegate = method_getImplementation(method);
method_setImplementation(method, (IMP)swizzle_setDelegate);
...
}