diff --git a/RNSensorsAnalyticsModule.podspec b/RNSensorsAnalyticsModule.podspec index 506e20d..547f85d 100644 --- a/RNSensorsAnalyticsModule.podspec +++ b/RNSensorsAnalyticsModule.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "RNSensorsAnalyticsModule" - s.version = "2.1.3" + s.version = "2.1.4" s.summary = "The official React Native SDK of Sensors Analytics." s.description = <<-DESC 神策分析 RN 组件 diff --git a/android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java b/android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java index ca652d8..b0e2dde 100644 --- a/android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java +++ b/android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java @@ -28,7 +28,7 @@ import java.util.List; public class RNSensorsAnalyticsPackage implements ReactPackage { - public static final String VERSION = "2.1.3"; + public static final String VERSION = "2.1.4"; @Override public List createNativeModules(ReactApplicationContext reactContext) { List modules = new ArrayList<>(); diff --git a/ios/RNSensorsAnalyticsModule.m b/ios/RNSensorsAnalyticsModule.m index 1f902b3..0857b02 100644 --- a/ios/RNSensorsAnalyticsModule.m +++ b/ios/RNSensorsAnalyticsModule.m @@ -32,7 +32,7 @@ #import "SAReactNativeManager.h" #import "SAReactNativeEventProperty.h" -NSString *const kSAReactNativePluginVersion = @"react_native:2.1.3"; +NSString *const kSAReactNativePluginVersion = @"react_native:2.1.4"; @implementation RNSensorsAnalyticsModule diff --git a/ios/SAReactNativeManager.m b/ios/SAReactNativeManager.m index 7e514f4..24ae6e7 100644 --- a/ios/SAReactNativeManager.m +++ b/ios/SAReactNativeManager.m @@ -225,6 +225,15 @@ - (RCTRootView *)rootView { current = current.presentingViewController; rootView = [self rootViewWithCurrentView:current.view]; } + + if (!rootView) { + // 当 rootViewController 为普通 UIViewController,且添加了 childController 时无法获取到 RCTRootView + // 此时直接通过 rootViewController 的 subview 获取 RCTRootView + // 这里是通过遍历所有的 subviews 查找,作为补充逻辑存在 + UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController; + rootView = [self rootViewWithCurrentView:root.view]; + } + return rootView; } diff --git a/ios/SAReactNativeswizzler.h b/ios/SAReactNativeswizzler.h deleted file mode 100644 index 9424788..0000000 --- a/ios/SAReactNativeswizzler.h +++ /dev/null @@ -1,33 +0,0 @@ -// -// SAReactNativeSwizzler.h -// RNSensorsAnalyticsModule -// -// Created by 彭远洋 on 2020/5/14. -// Copyright © 2020-2021 Sensors Data Co., Ltd. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface NSObject (SAReactNativeSwizzler) - -+ (BOOL)sa_reactnative_swizzleMethod:(SEL)origSel_ withMethod:(SEL)altSel_ error:(NSError **)error_; -+ (BOOL)sa_reactnative_swizzleClassMethod:(SEL)origSel_ withClassMethod:(SEL)altSel_ error:(NSError **)error_; - -@end - -NS_ASSUME_NONNULL_END diff --git a/ios/SAReactNativeswizzler.m b/ios/SAReactNativeswizzler.m deleted file mode 100644 index f231f04..0000000 --- a/ios/SAReactNativeswizzler.m +++ /dev/null @@ -1,154 +0,0 @@ -// -// SAReactNativeSwizzler.m -// RNSensorsAnalyticsModule -// -// Created by 彭远洋 on 2020/5/14. -// Copyright © 2020-2021 Sensors Data Co., Ltd. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#if ! __has_feature(objc_arc) -#error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag on this file. -#endif - - -#import "SAReactNativeswizzler.h" - -#if TARGET_OS_IPHONE - #import - #import -#else - #import -#endif - -#define SetNSErrorFor(FUNC, ERROR_VAR, FORMAT,...) \ - if (ERROR_VAR) { \ - NSString *errStr = [NSString stringWithFormat:@"%s: " FORMAT,FUNC,##__VA_ARGS__]; \ - *ERROR_VAR = [NSError errorWithDomain:@"NSCocoaErrorDomain" \ - code:-1 \ - userInfo:[NSDictionary dictionaryWithObject:errStr forKey:NSLocalizedDescriptionKey]]; \ - } -#define SetNSError(ERROR_VAR, FORMAT,...) SetNSErrorFor(__func__, ERROR_VAR, FORMAT, ##__VA_ARGS__) - -#if OBJC_API_VERSION >= 2 -#define GetClass(obj) object_getClass(obj) -#else -#define GetClass(obj) (obj ? obj->isa : Nil) -#endif - -@implementation NSObject (SAReactNativeSwizzler) - -+ (BOOL)sa_reactnative_swizzleMethod:(SEL)origSel_ withMethod:(SEL)altSel_ error:(NSError **)error_ { -#if OBJC_API_VERSION >= 2 - Method origMethod = class_getInstanceMethod(self, origSel_); - if (!origMethod) { -#if TARGET_OS_IPHONE - SetNSError(error_, @"original method %@ not found for class %@", NSStringFromSelector(origSel_), [self class]); -#else - SetNSError(error_, @"original method %@ not found for class %@", NSStringFromSelector(origSel_), [self className]); -#endif - return NO; - } - - Method altMethod = class_getInstanceMethod(self, altSel_); - if (!altMethod) { -#if TARGET_OS_IPHONE - SetNSError(error_, @"alternate method %@ not found for class %@", NSStringFromSelector(altSel_), [self class]); -#else - SetNSError(error_, @"alternate method %@ not found for class %@", NSStringFromSelector(altSel_), [self className]); -#endif - return NO; - } - - class_addMethod(self, - origSel_, - class_getMethodImplementation(self, origSel_), - method_getTypeEncoding(origMethod)); - class_addMethod(self, - altSel_, - class_getMethodImplementation(self, altSel_), - method_getTypeEncoding(altMethod)); - - method_exchangeImplementations(class_getInstanceMethod(self, origSel_), class_getInstanceMethod(self, altSel_)); - return YES; -#else - // Scan for non-inherited methods. - Method directOriginalMethod = NULL, directAlternateMethod = NULL; - - void *iterator = NULL; - struct objc_method_list *mlist = class_nextMethodList(self, &iterator); - while (mlist) { - int method_index = 0; - for (; method_index < mlist->method_count; method_index++) { - if (mlist->method_list[method_index].method_name == origSel_) { - assert(!directOriginalMethod); - directOriginalMethod = &mlist->method_list[method_index]; - } - if (mlist->method_list[method_index].method_name == altSel_) { - assert(!directAlternateMethod); - directAlternateMethod = &mlist->method_list[method_index]; - } - } - mlist = class_nextMethodList(self, &iterator); - } - - // If either method is inherited, copy it up to the target class to make it non-inherited. - if (!directOriginalMethod || !directAlternateMethod) { - Method inheritedOriginalMethod = NULL, inheritedAlternateMethod = NULL; - if (!directOriginalMethod) { - inheritedOriginalMethod = class_getInstanceMethod(self, origSel_); - if (!inheritedOriginalMethod) { - SetNSError(error_, @"original method %@ not found for class %@", NSStringFromSelector(origSel_), [self className]); - return NO; - } - } - if (!directAlternateMethod) { - inheritedAlternateMethod = class_getInstanceMethod(self, altSel_); - if (!inheritedAlternateMethod) { - SetNSError(error_, @"alternate method %@ not found for class %@", NSStringFromSelector(altSel_), [self className]); - return NO; - } - } - - int hoisted_method_count = !directOriginalMethod && !directAlternateMethod ? 2 : 1; - struct objc_method_list *hoisted_method_list = malloc(sizeof(struct objc_method_list) + (sizeof(struct objc_method)*(hoisted_method_count-1))); - hoisted_method_list->obsolete = NULL; // soothe valgrind - apparently ObjC runtime accesses this value and it shows as uninitialized in valgrind - hoisted_method_list->method_count = hoisted_method_count; - Method hoisted_method = hoisted_method_list->method_list; - - if (!directOriginalMethod) { - bcopy(inheritedOriginalMethod, hoisted_method, sizeof(struct objc_method)); - directOriginalMethod = hoisted_method++; - } - if (!directAlternateMethod) { - bcopy(inheritedAlternateMethod, hoisted_method, sizeof(struct objc_method)); - directAlternateMethod = hoisted_method; - } - class_addMethods(self, hoisted_method_list); - } - - // Swizzle. - IMP temp = directOriginalMethod->method_imp; - directOriginalMethod->method_imp = directAlternateMethod->method_imp; - directAlternateMethod->method_imp = temp; - - return YES; -#endif -} - -+ (BOOL)sa_reactnative_swizzleClassMethod:(SEL)origSel_ withClassMethod:(SEL)altSel_ error:(NSError **)error_ { - return [GetClass((id)self) sa_reactnative_swizzleMethod:origSel_ withMethod:altSel_ error:error_]; -} - -@end diff --git a/package.json b/package.json index 4629430..ebe1a25 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sensorsdata-analytics-react-native", - "version": "2.1.3", + "version": "2.1.4", "private": false, "description": "神策分析 RN 组件", "main": "index.js",