1.需要WMAppDelegateProxy類
@interfaceWMAppDelegateProxy :NSProxy
- (id)init;
@property(nonatomic,strong)NSObject *hjAppDelegate;
@property(nonatomic,strong)NSObject *originalAppDelegate;
@end
#import"WMAppDelegateProxy.h"
@implementationWMAppDelegateProxy
- (id)init
{
returnself;
}
- (NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature*sig;
sig = [self.originalAppDelegatemethodSignatureForSelector:aSelector];
if(sig) {
returnsig;
}else{
sig = [self.hjAppDelegatemethodSignatureForSelector:aSelector];
returnsig;
}
returnnil;
}
// Invoke the invocation on whichever real object had a signature for it.
- (void)forwardInvocation:(NSInvocation*)invocation
{
if([selfhjDelegateRespondsToSelector:[invocationselector]]) {
[invocationinvokeWithTarget:self.hjAppDelegate];
}
if([self.originalAppDelegatemethodSignatureForSelector:[invocationselector]]) {
[invocationinvokeWithTarget:self.originalAppDelegate];
}
}
// Override some of NSProxy's implementations to forward them...
- (BOOL)respondsToSelector:(SEL)aSelector
{
if([self.hjAppDelegaterespondsToSelector:aSelector])
returnYES;
if([self.originalAppDelegaterespondsToSelector:aSelector])
returnYES;
returnNO;
}
- (BOOL)hjDelegateRespondsToSelector:(SEL)selector
{
return[self.hjAppDelegaterespondsToSelector:selector] && ![[NSObjectclass]instancesRespondToSelector:selector];
}
@end
2.新建WMAppDelegate類
#import
@interfaceWMAppDelegate :NSObject
@end
#import"WMAppDelegate.h"
@implementationWMAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//如果sdk初值化是在didFinishLaunchingWithOptions方法,就無(wú)法運(yùn)行到sdk里面的這個(gè)方法
NSLog(@"SDK UIApplication didFinishLaunchingWithOptions:%@, %@", application, launchOptions);
returnYES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication*)application {
}
- (void)applicationDidBecomeActive:(UIApplication*)application {
}
- (void)applicationWillTerminate:(UIApplication*)application {
}
@end
3.在SDK初值化的時(shí)候 添加如下代碼
_appDelegateProxy=[[WMAppDelegateProxyalloc]init];
@synchronized([UIApplicationsharedApplication]) {
_appDelegateProxy.hjAppDelegate= [[WMAppDelegatealloc]init];
_appDelegateProxy.originalAppDelegate= [UIApplicationsharedApplication].delegate;
[UIApplicationsharedApplication].delegate=_appDelegateProxy;
}
原理:
[UIApplication sharedApplication].delegate, 這個(gè)delegate默認(rèn)是指向DemoAppDelegate, 現(xiàn)在我們可以把[UIApplication sharedApplication].delegate指向一個(gè)變量proxy, 那么這個(gè)變量將會(huì)獲取到所有原來(lái)應(yīng)用AppDelegate的回調(diào)方法, 其中包括appDidFinishLaunchedWithOption, appWillResignActive, appWillEnterForeground等回調(diào)用遏匆, 同樣也包括本地通知以及遠(yuǎn)程通知調(diào)用時(shí)的回調(diào)尸红。
上面這個(gè)方法是把這個(gè)[UIApplication sharedApplication].delegate指向一個(gè)變量proxy, 我們只需要在proxy這個(gè)變量中同時(shí)把a(bǔ)ppDelegate也進(jìn)行調(diào)用就可以了逛薇。 也即在proxy中捺疼,我們可以先處理一下我們想處理的回調(diào)方法, 然后繼續(xù)讓這些回調(diào)方法繼續(xù)流轉(zhuǎn)永罚, 如流轉(zhuǎn)到appDelegate即可啤呼。也即讓自己先處理完成后卧秘, 再進(jìn)行消息的轉(zhuǎn)發(fā)。