Skip to content

Commit

Permalink
Merge pull request #1287 from OneSignal/user_model/fix_swizzling_subc…
Browse files Browse the repository at this point in the history
…lasses

Do not swizzle a subclass of an already swizzled class
  • Loading branch information
emawby authored and nan-li committed Oct 30, 2023
2 parents 9daa536 + dbcd753 commit 4e24e72
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import "OneSignalSelectorHelpers.h"
#import "SwizzlingForwarder.h"
#import "OSNotificationsManager.h"
#import <objc/runtime.h>

// This class hooks into the UIApplicationDelegate selectors to receive iOS 9 and older events.
// - UNUserNotificationCenter is used for iOS 10
Expand All @@ -58,7 +59,7 @@ - (void) setOneSignalDelegate:(id<UIApplicationDelegate>)delegate {

Class delegateClass = [delegate class];

if (delegate == nil || [swizzledClasses containsObject:delegateClass]) {
if (delegate == nil || [OneSignalNotificationsAppDelegate swizzledClassInHeirarchy:delegateClass]) {
[self setOneSignalDelegate:delegate];
return;
}
Expand Down Expand Up @@ -93,6 +94,22 @@ - (void) setOneSignalDelegate:(id<UIApplicationDelegate>)delegate {
[self setOneSignalDelegate:delegate];
}

+ (BOOL)swizzledClassInHeirarchy:(Class)delegateClass {
if ([swizzledClasses containsObject:delegateClass]) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"OneSignal already swizzled %@", NSStringFromClass(delegateClass)]];
return true;
}
Class superClass = class_getSuperclass(delegateClass);
while(superClass) {
if ([swizzledClasses containsObject:superClass]) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"OneSignal already swizzled %@ in super class: %@", NSStringFromClass(delegateClass), NSStringFromClass(superClass)]];
return true;
}
superClass = class_getSuperclass(superClass);
}
return false;
}

- (void)oneSignalDidRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSData*)inDeviceToken {
[OneSignalNotificationsAppDelegate traceCall:@"oneSignalDidRegisterForRemoteNotifications:deviceToken:"];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#import "UIApplicationDelegate+OneSignalNotifications.h"
#import "OSNotificationsManager.h"
#import <OneSignalCore/OneSignalCore.h>
#import <objc/runtime.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"

Expand Down Expand Up @@ -182,7 +183,7 @@ - (void) setOneSignalUNDelegate:(id)delegate {

Class delegateClass = [delegate class];

if (delegate == nil || [OneSignalUNUserNotificationCenter swizzledClassInHeirarchy:delegateClass]) {
if (delegate == nil || [OneSignalNotificationsUNUserNotificationCenter swizzledClassInHeirarchy:delegateClass]) {
[self setOneSignalUNDelegate:delegate];
return;
}
Expand Down
16 changes: 16 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/UIApplicationDelegate+OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ - (void) setOneSignalDelegate:(id<UIApplicationDelegate>)delegate {
[self setOneSignalDelegate:delegate];
}

+ (BOOL)swizzledClassInHeirarchy:(Class)delegateClass {
if ([swizzledClasses containsObject:delegateClass]) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"OneSignal already swizzled %@", NSStringFromClass(delegateClass)]];
return true;
}
Class superClass = class_getSuperclass(delegateClass);
while(superClass) {
if ([swizzledClasses containsObject:superClass]) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"OneSignal already swizzled %@ in super class: %@", NSStringFromClass(delegateClass), NSStringFromClass(superClass)]];
return true;
}
superClass = class_getSuperclass(superClass);
}
return false;
}

-(void)oneSignalApplicationWillTerminate:(UIApplication *)application {
[OneSignalAppDelegate traceCall:@"oneSignalApplicationWillTerminate:"];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ of this software and associated documentation files (the "Software"), to deal
*/

#import <XCTest/XCTest.h>
#import "OneSignal.h"
#import "OneSignalFramework.h"
#import "OneSignalUserDefaults.h"
#import "OneSignalHelper.h"
#import "OSInAppMessageInternal.h"
Expand Down

0 comments on commit 4e24e72

Please sign in to comment.