@radhya/mach-push-react-native 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,19 +6,54 @@ NSString * const MachPushDidRegisterTokenNotification = @"MachPushDidRegisterTok
6
6
  NSString * const MachPushDidFailRegistrationNotification = @"MachPushDidFailRegistrationNotification";
7
7
 
8
8
  static BOOL MachPushSwizzle(Class cls, SEL original, SEL replacement) {
9
- Method replacementMethod = class_getInstanceMethod(cls, replacement);
9
+ Method replacementMethod = class_getInstanceMethod([NSObject class], replacement);
10
10
  if (!replacementMethod) return NO;
11
11
  Method originalMethod = class_getInstanceMethod(cls, original);
12
12
  if (!originalMethod) {
13
13
  class_addMethod(cls, original, method_getImplementation(replacementMethod), method_getTypeEncoding(replacementMethod));
14
14
  return NO;
15
15
  }
16
- method_exchangeImplementations(originalMethod, replacementMethod);
16
+
17
+ // Copy the original implementation onto the concrete app delegate class so
18
+ // `[self mach_application:...]` reliably reaches the app/Expo handler even
19
+ // when the original method is inherited from a Swift superclass.
20
+ class_addMethod(
21
+ cls,
22
+ replacement,
23
+ method_getImplementation(originalMethod),
24
+ method_getTypeEncoding(originalMethod)
25
+ );
26
+
27
+ Method concreteOriginal = class_getInstanceMethod(cls, original);
28
+ Method concreteReplacement = class_getInstanceMethod(cls, replacement);
29
+ if (!concreteOriginal || !concreteReplacement) return NO;
30
+ method_exchangeImplementations(concreteOriginal, concreteReplacement);
17
31
  return YES;
18
32
  }
19
33
 
20
34
  static BOOL MachPushHadRegisterHandler = NO;
21
35
  static BOOL MachPushHadFailureHandler = NO;
36
+ static Class MachPushInstalledDelegateClass = Nil;
37
+
38
+ static void MachPushInstallInterceptorIfNeeded(void) {
39
+ if (MachPushInstalledDelegateClass) return;
40
+
41
+ id<UIApplicationDelegate> delegate = UIApplication.sharedApplication.delegate;
42
+ Class appDelegateClass = delegate ? object_getClass(delegate) : NSClassFromString(@"AppDelegate");
43
+ if (!appDelegateClass) return;
44
+
45
+ MachPushHadRegisterHandler = MachPushSwizzle(
46
+ appDelegateClass,
47
+ @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:),
48
+ @selector(mach_application:didRegisterForRemoteNotificationsWithDeviceToken:)
49
+ );
50
+ MachPushHadFailureHandler = MachPushSwizzle(
51
+ appDelegateClass,
52
+ @selector(application:didFailToRegisterForRemoteNotificationsWithError:),
53
+ @selector(mach_application:didFailToRegisterForRemoteNotificationsWithError:)
54
+ );
55
+ MachPushInstalledDelegateClass = appDelegateClass;
56
+ }
22
57
 
23
58
  @interface NSObject (MachPushAppDelegateInterceptor)
24
59
  @end
@@ -27,18 +62,20 @@ static BOOL MachPushHadFailureHandler = NO;
27
62
 
28
63
  + (void)load {
29
64
  dispatch_async(dispatch_get_main_queue(), ^{
30
- Class appDelegateClass = NSClassFromString(@"AppDelegate");
31
- if (!appDelegateClass) return;
32
- MachPushHadRegisterHandler = MachPushSwizzle(
33
- appDelegateClass,
34
- @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:),
35
- @selector(mach_application:didRegisterForRemoteNotificationsWithDeviceToken:)
36
- );
37
- MachPushHadFailureHandler = MachPushSwizzle(
38
- appDelegateClass,
39
- @selector(application:didFailToRegisterForRemoteNotificationsWithError:),
40
- @selector(mach_application:didFailToRegisterForRemoteNotificationsWithError:)
41
- );
65
+ MachPushInstallInterceptorIfNeeded();
66
+ [[NSNotificationCenter defaultCenter]
67
+ addObserverForName:UIApplicationDidFinishLaunchingNotification
68
+ object:nil
69
+ queue:[NSOperationQueue mainQueue]
70
+ usingBlock:^(__unused NSNotification *note) {
71
+ MachPushInstallInterceptorIfNeeded();
72
+ }];
73
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
74
+ MachPushInstallInterceptorIfNeeded();
75
+ });
76
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
77
+ MachPushInstallInterceptorIfNeeded();
78
+ });
42
79
  });
43
80
  }
44
81
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radhya/mach-push-react-native",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "MACH Push for React Native and Expo development builds",
5
5
  "license": "MIT",
6
6
  "repository": {