From 3523dfaa838380bea2575db96f1f8b1688f2219f Mon Sep 17 00:00:00 2001 From: huangshuni <15889638723@163.com> Date: Mon, 18 Sep 2023 15:42:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DiOS=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ios/Plugins/AppDelegate+JPush.m | 31 ++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/ios/Plugins/AppDelegate+JPush.m b/src/ios/Plugins/AppDelegate+JPush.m index 81e7820..88cdf39 100644 --- a/src/ios/Plugins/AppDelegate+JPush.m +++ b/src/ios/Plugins/AppDelegate+JPush.m @@ -16,11 +16,32 @@ @implementation AppDelegate (JPush) +(void)load{ - Method origin1; - Method swizzle1; - origin1 = class_getInstanceMethod([self class],@selector(init)); - swizzle1 = class_getInstanceMethod([self class], @selector(init_plus)); - method_exchangeImplementations(origin1, swizzle1); + + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + Class class = [self class]; + + SEL originalSelector = @selector(init); + SEL swizzledSelector = @selector(init_plus); + + Method origin = class_getInstanceMethod(class, originalSelector); + Method swizzle = class_getInstanceMethod(class, swizzledSelector); + + BOOL didAddMethod = + class_addMethod(class, + originalSelector, + method_getImplementation(swizzle), + method_getTypeEncoding(swizzle)); + + if (didAddMethod) { + class_replaceMethod(class, + swizzledSelector, + method_getImplementation(origin), + method_getTypeEncoding(origin)); + } else { + method_exchangeImplementations(origin, swizzle); + } + }); } -(instancetype)init_plus{