Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hybrid add enable/disable bridge APIs #272

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GrowingAnalytics.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和
s.subspec 'Hybrid' do |hybrid|
hybrid.ios.deployment_target = '9.0'
hybrid.source_files = 'Modules/Hybrid/**/*{.h,.m,.c,.cpp,.mm}'
hybrid.public_header_files = 'Modules/Hybrid/include/*.h'
hybrid.public_header_files = 'Modules/Hybrid/Public/*.h'
hybrid.dependency 'GrowingAnalytics/TrackerCore', s.version.to_s
end

Expand Down
71 changes: 70 additions & 1 deletion Modules/Hybrid/GrowingHybridModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,88 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "Modules/Hybrid/GrowingHybridModule.h"
#import "Modules/Hybrid/Public/GrowingHybridModule.h"
#import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h"
#import "GrowingULSwizzle.h"
#import "Modules/Hybrid/WKWebView+GrowingAutotracker.h"

GrowingMod(GrowingHybridModule)

@interface GrowingHybridModule ()

@property (nonatomic, strong) NSHashTable *enableBridgeWebViews;
@property (nonatomic, strong) NSHashTable *disableBridgeWebViews;

@end

@implementation GrowingHybridModule

#pragma mark - Init

+ (instancetype)sharedInstance {
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}

- (instancetype)init {
if (self = [super init]) {
_autoBridgeEnabled = YES;
_enableBridgeWebViews = [NSHashTable weakObjectsHashTable];
_disableBridgeWebViews = [NSHashTable weakObjectsHashTable];
}
return self;
}

#pragma mark - GrowingModuleProtocol

+ (BOOL)singleton {
return YES;
}

- (void)growingModInit:(GrowingContext *)context {
[self track];
}

#pragma mark - Public Methods

- (void)enableBridgeForWebView:(WKWebView *)webView {
if (![NSThread isMainThread]) {
GIOLogError(@"调用异常,请在主线程调用 %@", NSStringFromSelector(_cmd));

Check warning on line 70 in Modules/Hybrid/GrowingHybridModule.m

View check run for this annotation

Codecov / codecov/patch

Modules/Hybrid/GrowingHybridModule.m#L68-L70

Added lines #L68 - L70 were not covered by tests
}
[self.enableBridgeWebViews addObject:webView];

Check warning on line 72 in Modules/Hybrid/GrowingHybridModule.m

View check run for this annotation

Codecov / codecov/patch

Modules/Hybrid/GrowingHybridModule.m#L72

Added line #L72 was not covered by tests
}

- (void)disableBridgeForWebView:(WKWebView *)webView {
if (![NSThread isMainThread]) {
GIOLogError(@"调用异常,请在主线程调用 %@", NSStringFromSelector(_cmd));

Check warning on line 77 in Modules/Hybrid/GrowingHybridModule.m

View check run for this annotation

Codecov / codecov/patch

Modules/Hybrid/GrowingHybridModule.m#L75-L77

Added lines #L75 - L77 were not covered by tests
}
[self.disableBridgeWebViews addObject:webView];

Check warning on line 79 in Modules/Hybrid/GrowingHybridModule.m

View check run for this annotation

Codecov / codecov/patch

Modules/Hybrid/GrowingHybridModule.m#L79

Added line #L79 was not covered by tests
}

- (BOOL)isBridgeForWebViewEnabled:(WKWebView *)webView {
GrowingHybridModule *module = GrowingHybridModule.sharedInstance;
if (module.autoBridgeEnabled) {
return ![module.disableBridgeWebViews containsObject:webView];
} else {
return [module.enableBridgeWebViews containsObject:webView];

Check warning on line 87 in Modules/Hybrid/GrowingHybridModule.m

View check run for this annotation

Codecov / codecov/patch

Modules/Hybrid/GrowingHybridModule.m#L87

Added line #L87 was not covered by tests
}
}

- (void)resetBridgeSettings {
if (![NSThread isMainThread]) {
GIOLogError(@"调用异常,请在主线程调用 %@", NSStringFromSelector(_cmd));

Check warning on line 93 in Modules/Hybrid/GrowingHybridModule.m

View check run for this annotation

Codecov / codecov/patch

Modules/Hybrid/GrowingHybridModule.m#L91-L93

Added lines #L91 - L93 were not covered by tests
}
self.autoBridgeEnabled = YES;
self.enableBridgeWebViews = [NSHashTable weakObjectsHashTable];
self.disableBridgeWebViews = [NSHashTable weakObjectsHashTable];

Check warning on line 97 in Modules/Hybrid/GrowingHybridModule.m

View check run for this annotation

Codecov / codecov/patch

Modules/Hybrid/GrowingHybridModule.m#L95-L97

Added lines #L95 - L97 were not covered by tests
}

#pragma mark - Private Methods

- (void)track {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand Down
6 changes: 6 additions & 0 deletions Modules/Hybrid/GrowingWKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#import "Modules/Hybrid/GrowingHybridBridgeProvider.h"
#import "Modules/Hybrid/GrowingWKWebViewJavascriptBridge_JS.h"
#import "Modules/Hybrid/GrowingWebViewJavascriptBridgeConfiguration.h"
#import "Modules/Hybrid/Public/GrowingHybridModule.h"

static NSString *const kGrowingWKWebViewJavascriptBridge = @"GrowingWKWebViewJavascriptBridge";

Expand Down Expand Up @@ -63,6 +64,11 @@
return;
}

if (![GrowingHybridModule.sharedInstance isBridgeForWebViewEnabled:webView]) {
GIOLogDebug(@"WKWebview Bridge %@ is disabled", webView);
return;

Check warning on line 69 in Modules/Hybrid/GrowingWKWebViewJavascriptBridge.m

View check run for this annotation

Codecov / codecov/patch

Modules/Hybrid/GrowingWKWebViewJavascriptBridge.m#L68-L69

Added lines #L68 - L69 were not covered by tests
}

WKUserContentController *contentController = webView.configuration.userContentController;
[self addScriptMessageHandler:contentController];
[self addUserScripts:contentController];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,25 @@

NS_ASSUME_NONNULL_BEGIN

@class WKWebView;

@interface GrowingHybridModule : NSObject <GrowingModuleProtocol>

// 是否对所有webView自动注入Hybrid SDK,默认为YES
@property (nonatomic, assign) BOOL autoBridgeEnabled;

// 在autoBridgeEnabled为NO时,对单个webView启用Hybrid注入,请在主线程调用
- (void)enableBridgeForWebView:(WKWebView *)webView;

// 在autoBridgeEnabled为YES时,对单个webView关闭Hybrid注入,请在主线程调用
- (void)disableBridgeForWebView:(WKWebView *)webView;

// 判断当前配置下,webView是否可注入
- (BOOL)isBridgeForWebViewEnabled:(WKWebView *)webView;

// 重置Hybrid注入配置,请在主线程调用
- (void)resetBridgeSettings;

@end

NS_ASSUME_NONNULL_END
21 changes: 0 additions & 21 deletions Modules/Hybrid/include/Dummy-GrowingModule-Hybrid.h

This file was deleted.

1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ let package = Package(
name: "GrowingModule_Hybrid",
dependencies: ["GrowingTrackerCore"],
path: "Modules/Hybrid",
publicHeadersPath: "Public",
cSettings: [
.headerSearchPath("../..")
],
Expand Down
3 changes: 3 additions & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ end

post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
end
if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
target.build_configurations.each do |config|
config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
Expand Down
130 changes: 65 additions & 65 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
PODS:
- GrowingAnalytics-cdp/Autotracker (3.4.8-hotfix.1):
- GrowingAnalytics/AutotrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/DefaultServices (= 3.4.8-hotfix.1)
- GrowingAnalytics/Hybrid (= 3.4.8-hotfix.1)
- GrowingAnalytics/MobileDebugger (= 3.4.8-hotfix.1)
- GrowingAnalytics/WebCircle (= 3.4.8-hotfix.1)
- GrowingAnalytics-cdp/Tracker (3.4.8-hotfix.1):
- GrowingAnalytics/DefaultServices (= 3.4.8-hotfix.1)
- GrowingAnalytics/MobileDebugger (= 3.4.8-hotfix.1)
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Advert (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/APM (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics-cdp/Autotracker (3.5.0):
- GrowingAnalytics/AutotrackerCore (= 3.5.0)
- GrowingAnalytics/DefaultServices (= 3.5.0)
- GrowingAnalytics/Hybrid (= 3.5.0)
- GrowingAnalytics/MobileDebugger (= 3.5.0)
- GrowingAnalytics/WebCircle (= 3.5.0)
- GrowingAnalytics-cdp/Tracker (3.5.0):
- GrowingAnalytics/DefaultServices (= 3.5.0)
- GrowingAnalytics/MobileDebugger (= 3.5.0)
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/Advert (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/APM (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAPM/Core
- GrowingAnalytics/Autotracker (3.4.8-hotfix.1):
- GrowingAnalytics/AutotrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/DefaultServices (= 3.4.8-hotfix.1)
- GrowingAnalytics/Hybrid (= 3.4.8-hotfix.1)
- GrowingAnalytics/MobileDebugger (= 3.4.8-hotfix.1)
- GrowingAnalytics/WebCircle (= 3.4.8-hotfix.1)
- GrowingAnalytics/AutotrackerCore (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Autotracker (3.5.0):
- GrowingAnalytics/AutotrackerCore (= 3.5.0)
- GrowingAnalytics/DefaultServices (= 3.5.0)
- GrowingAnalytics/Hybrid (= 3.5.0)
- GrowingAnalytics/MobileDebugger (= 3.5.0)
- GrowingAnalytics/WebCircle (= 3.5.0)
- GrowingAnalytics/AutotrackerCore (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingUtils/AutotrackerCore (= 0.0.5)
- GrowingAnalytics/Compression (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Database (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/DefaultServices (3.4.8-hotfix.1):
- GrowingAnalytics/Compression (= 3.4.8-hotfix.1)
- GrowingAnalytics/Database (= 3.4.8-hotfix.1)
- GrowingAnalytics/Encryption (= 3.4.8-hotfix.1)
- GrowingAnalytics/Network (= 3.4.8-hotfix.1)
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Encryption (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Hybrid (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/MobileDebugger (3.4.8-hotfix.1):
- GrowingAnalytics/Screenshot (= 3.4.8-hotfix.1)
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/WebSocket (= 3.4.8-hotfix.1)
- GrowingAnalytics/Network (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Protobuf (3.4.8-hotfix.1):
- GrowingAnalytics/Database (= 3.4.8-hotfix.1)
- GrowingAnalytics/Protobuf/Proto (= 3.4.8-hotfix.1)
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Protobuf/Proto (3.4.8-hotfix.1):
- GrowingAnalytics/Database (= 3.4.8-hotfix.1)
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Compression (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/Database (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/DefaultServices (3.5.0):
- GrowingAnalytics/Compression (= 3.5.0)
- GrowingAnalytics/Database (= 3.5.0)
- GrowingAnalytics/Encryption (= 3.5.0)
- GrowingAnalytics/Network (= 3.5.0)
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/Encryption (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/Hybrid (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/MobileDebugger (3.5.0):
- GrowingAnalytics/Screenshot (= 3.5.0)
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/WebSocket (= 3.5.0)
- GrowingAnalytics/Network (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/Protobuf (3.5.0):
- GrowingAnalytics/Database (= 3.5.0)
- GrowingAnalytics/Protobuf/Proto (= 3.5.0)
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/Protobuf/Proto (3.5.0):
- GrowingAnalytics/Database (= 3.5.0)
- GrowingAnalytics/TrackerCore (= 3.5.0)
- Protobuf
- GrowingAnalytics/Screenshot (3.4.8-hotfix.1):
- GrowingAnalytics/Screenshot (3.5.0):
- GrowingAnalytics/TrackerCore
- GrowingAnalytics/Tracker (3.4.8-hotfix.1):
- GrowingAnalytics/DefaultServices (= 3.4.8-hotfix.1)
- GrowingAnalytics/MobileDebugger (= 3.4.8-hotfix.1)
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/TrackerCore (3.4.8-hotfix.1):
- GrowingAnalytics/Tracker (3.5.0):
- GrowingAnalytics/DefaultServices (= 3.5.0)
- GrowingAnalytics/MobileDebugger (= 3.5.0)
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAnalytics/TrackerCore (3.5.0):
- GrowingUtils/TrackerCore (= 0.0.5)
- GrowingAnalytics/WebCircle (3.4.8-hotfix.1):
- GrowingAnalytics/AutotrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/Hybrid (= 3.4.8-hotfix.1)
- GrowingAnalytics/Screenshot (= 3.4.8-hotfix.1)
- GrowingAnalytics/WebSocket (= 3.4.8-hotfix.1)
- GrowingAnalytics/WebSocket (3.4.8-hotfix.1):
- GrowingAnalytics/TrackerCore (= 3.4.8-hotfix.1)
- GrowingAnalytics/WebCircle (3.5.0):
- GrowingAnalytics/AutotrackerCore (= 3.5.0)
- GrowingAnalytics/Hybrid (= 3.5.0)
- GrowingAnalytics/Screenshot (= 3.5.0)
- GrowingAnalytics/WebSocket (= 3.5.0)
- GrowingAnalytics/WebSocket (3.5.0):
- GrowingAnalytics/TrackerCore (= 3.5.0)
- GrowingAPM (0.0.14):
- GrowingAPM/Core (= 0.0.14)
- GrowingAPM/CrashMonitor (= 0.0.14)
Expand Down Expand Up @@ -161,8 +161,8 @@ EXTERNAL SOURCES:
:path: "./"

SPEC CHECKSUMS:
GrowingAnalytics: 9c4c3c7e33523f3daba012a06f0f419b77e02544
GrowingAnalytics-cdp: 273939219903d141466237a703700cfb6bb0b689
GrowingAnalytics: d1d706fe8205b8139b62a5717053a03ee33962a0
GrowingAnalytics-cdp: 0c3605344c9870ac9982cb0315c498f7a12298c3
GrowingAPM: 79fe4f4a12d94432fa4a552d56c41940ea13961a
GrowingToolsKit: 88b144e858f8895f1d84c518642ce34ce0f5aa07
GrowingUtils: 5212c5c0501ea0c3863a29c33ccefbde5be77353
Expand All @@ -172,6 +172,6 @@ SPEC CHECKSUMS:
SDCycleScrollView: a0d74c3384caa72bdfc81470bdbc8c14b3e1fbcf
SDWebImage: 2aea163b50bfcb569a2726b6a754c54a4506fcf6

PODFILE CHECKSUM: 82c0924b772d0db457a4d610670c1ac33475aeae
PODFILE CHECKSUM: cbf7540faaee5eacde16e6a695caa559cb26e455

COCOAPODS: 1.12.1