Skip to content

Commit

Permalink
feat: auto web js sdk bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Oct 12, 2024
1 parent 9b668ef commit 2632d41
Show file tree
Hide file tree
Showing 26 changed files with 102 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 54;
objects = {

/* Begin PBXAggregateTarget section */
Expand Down Expand Up @@ -4602,7 +4602,7 @@
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = SXBU677CPT;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
ENABLE_USER_SCRIPT_SANDBOXING = NO;
GCC_C_LANGUAGE_STANDARD = gnu17;
GCC_NO_COMMON_BLOCKS = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -4654,7 +4654,7 @@
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = SXBU677CPT;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
ENABLE_USER_SCRIPT_SANDBOXING = NO;
GCC_C_LANGUAGE_STANDARD = gnu17;
GCC_NO_COMMON_BLOCKS = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand Down
4 changes: 4 additions & 0 deletions Example/Example/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ - (void)SDK3rdStart {
configuration.abTestingRequestInterval = 5;
#endif

#if defined(SDKHybridMODULE)
GrowingHybridModule.sharedInstance.autoJavaScriptBridgeEnabled = YES;
#endif

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
[GrowingSDK startWithConfiguration:configuration launchOptions:nil];
Expand Down
6 changes: 6 additions & 0 deletions Example/Example/GrowingIO-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
#define GrowingSDKConfiguration GrowingTrackConfiguration
#endif

#if __has_include("GrowingHybridModule.h")
// Hybrid Module
#import "GrowingHybridModule.h"
#define SDKHybridMODULE
#endif

#if __has_include("GrowingAdvertising.h")
// Ads Module
#import "GrowingAdvertising.h"
Expand Down
1 change: 1 addition & 0 deletions Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

platform :ios, '12.0'
use_frameworks!
#use_modular_headers!

install!'cocoapods',:deterministic_uuids=>false, :warn_for_unused_master_specs_repo=>false

Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
GrowingAnalytics: d50200337f7dbad6d087705119c6f0605cdc91a2
GrowingAnalytics: e845c52ebff18c53659b5e37e75fb50e2097dc32
GrowingAPM: 3c4de0384935b654e6798b95606f47883a99418b
GrowingToolsKit: 53160d19690da0b78e04a9242abde7af86442922
GrowingUtils: 68aee2c96849bf9b674740503da30c2da468e79d
Expand All @@ -170,6 +170,6 @@ SPEC CHECKSUMS:
SDCycleScrollView: a0d74c3384caa72bdfc81470bdbc8c14b3e1fbcf
SDWebImage: dfe95b2466a9823cf9f0c6d01217c06550d7b29a

PODFILE CHECKSUM: 8ef4d85701caba0f5df403432dc182d34412ab4a
PODFILE CHECKSUM: dded21a1746985a3de3449c95f027e9071d8581a

COCOAPODS: 1.15.2
7 changes: 5 additions & 2 deletions GrowingAnalytics.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和
s.ios.framework = 'WebKit'
s.requires_arc = true
s.default_subspec = "Autotracker"
s.pod_target_xcconfig = { 'HEADER_SEARCH_PATHS' => '"${PODS_TARGET_SRCROOT}"' }
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'HEADER_SEARCH_PATHS' => '"${PODS_TARGET_SRCROOT}"' }

s.subspec 'Autotracker' do |autotracker|
autotracker.ios.deployment_target = '10.0'
Expand Down Expand Up @@ -47,7 +47,10 @@ GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和
trackerCore.dependency 'GrowingUtils/TrackerCore', '~> 1.2.3'
trackerCore.source_files = 'GrowingTrackerCore/**/*{.h,.m,.c,.cpp,.mm}'
trackerCore.public_header_files = 'GrowingTrackerCore/Public/*.h'
trackerCore.ios.resource_bundles = {'GrowingAnalytics' => ['Resources/iOS/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy']}
trackerCore.ios.resource_bundles = {'GrowingAnalytics' => [
'Resources/iOS/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy',
'Resources/iOS/GrowingAnalytics.bundle/gdp-full.js'
]}
trackerCore.osx.resource_bundles = {'GrowingAnalytics' => ['Resources/macOS/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy']}
trackerCore.watchos.resource_bundles = {'GrowingAnalytics' => ['Resources/watchOS/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy']}
trackerCore.tvos.resource_bundles = {'GrowingAnalytics' => ['Resources/tvOS/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy']}
Expand Down
1 change: 1 addition & 0 deletions Modules/Hybrid/GrowingHybridModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ + (instancetype)sharedInstance {
- (instancetype)init {
if (self = [super init]) {
_autoBridgeEnabled = YES;
_autoJavaScriptBridgeEnabled = NO;
_enableBridgeWebViews = [NSHashTable weakObjectsHashTable];
_disableBridgeWebViews = [NSHashTable weakObjectsHashTable];
}
Expand Down
26 changes: 26 additions & 0 deletions Modules/Hybrid/GrowingWKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#import "Modules/Hybrid/GrowingWebViewJavascriptBridgeConfiguration.h"
#import "Modules/Hybrid/Public/GrowingHybridModule.h"

#if SWIFT_PACKAGE
@import GrowingBundle;
#endif

static NSString *const kGrowingWKWebViewJavascriptBridge = @"GrowingWKWebViewJavascriptBridge";

@interface GrowingWKWebViewJavascriptBridge () <WKScriptMessageHandler>
Expand Down Expand Up @@ -110,11 +114,33 @@ + (void)addUserScripts:(WKUserContentController *)contentController {
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:NO];
[contentController addUserScript:userScript];

if (GrowingHybridModule.sharedInstance.autoJavaScriptBridgeEnabled) {
NSBundle *bundle = [self resourcesBundle];
NSString *filePath = [bundle pathForResource:@"gdp-full" ofType:@"js"];
NSString *webJSContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
webJSContent = [webJSContent stringByAppendingFormat:@"gdp('init', '%@', '%@');", accountId, dataSourceId];
WKUserScript *userScript2 = [[WKUserScript alloc] initWithSource:webJSContent
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:NO];
[contentController addUserScript:userScript2];
}
}
} @catch (NSException *exception) {
}
}

+ (NSBundle *)resourcesBundle {
#if SWIFT_PACKAGE
return GrowingBundleWrapper_SWIFTPM_MODULE_BUNDLE();
#else
NSBundle *bundle = [NSBundle bundleForClass:self.class];
NSString *bundleName = @"GrowingAnalytics.bundle";
NSString *path = [bundle.resourcePath stringByAppendingPathComponent:bundleName];
return [NSBundle bundleWithPath:path];
#endif
}

- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
if ([GrowingWKWebViewJavascriptBridge webViewDontTrackCheck:message.webView]) {
Expand Down
3 changes: 3 additions & 0 deletions Modules/Hybrid/Public/GrowingHybridModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ NS_SWIFT_NAME(Hybrid)
// 是否对所有webView自动注入Hybrid SDK,默认为YES
@property (nonatomic, assign) BOOL autoBridgeEnabled;

// 是否对所有webView自动注入Web JS SDK,默认为NO
@property (nonatomic, assign) BOOL autoJavaScriptBridgeEnabled;

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

Expand Down
77 changes: 40 additions & 37 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ let package = Package(

// MARK: - Resources

.Resources.privacy,
.Resources.privacy_macOS,
.Resources.privacy_tvOS,
.Resources.privacy_visionOS,
.Resources.privacy_watchOS,
.Resources.bundle,
.Resources.bundle_macOS,
.Resources.bundle_tvOS,
.Resources.bundle_visionOS,
.Resources.bundle_watchOS,

// MARK: - Core

Expand Down Expand Up @@ -149,24 +149,27 @@ extension Target {
cSettings: [.hspFor(.Path.tracker_objc)])

enum Resources {
static let privacy = target(name: .privacy,
path: .Path.privacy,
resources: [.copy("Resources/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy")])

static let privacy_macOS = target(name: .privacy_macOS,
path: .Path.privacy_macOS,
static let bundle = target(name: .bundle,
path: .Path.bundle,
resources: [
.copy("Resources/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy"),
.copy("Resources/GrowingAnalytics.bundle/gdp-full.js")
])

static let bundle_macOS = target(name: .bundle_macOS,
path: .Path.bundle_macOS,
resources: [.copy("Resources/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy")])

static let privacy_tvOS = target(name: .privacy_tvOS,
path: .Path.privacy_tvOS,
static let bundle_tvOS = target(name: .bundle_tvOS,
path: .Path.bundle_tvOS,
resources: [.copy("Resources/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy")])

static let privacy_visionOS = target(name: .privacy_visionOS,
path: .Path.privacy_visionOS,
static let bundle_visionOS = target(name: .bundle_visionOS,
path: .Path.bundle_visionOS,
resources: [.copy("Resources/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy")])

static let privacy_watchOS = target(name: .privacy_watchOS,
path: .Path.privacy_watchOS,
static let bundle_watchOS = target(name: .bundle_watchOS,
path: .Path.bundle_watchOS,
resources: [.copy("Resources/GrowingAnalytics.bundle/PrivacyInfo.xcprivacy")])
}

Expand All @@ -183,11 +186,11 @@ extension Target {
static let trackerCore = target(name: .trackerCore,
dependencies: [
.trackerUtils,
.Resources.privacy,
.Resources.privacy_macOS,
.Resources.privacy_tvOS,
.Resources.privacy_visionOS,
.Resources.privacy_watchOS,
.Resources.bundle,
.Resources.bundle_macOS,
.Resources.bundle_tvOS,
.Resources.bundle_visionOS,
.Resources.bundle_watchOS,
],
path: .Path.trackerCore,
publicHeadersPath: .Path.publicHeaders,
Expand Down Expand Up @@ -329,11 +332,11 @@ extension Target.Dependency {
static let protobuf = product(name: "SwiftProtobuf", package: "swift-protobuf")

enum Resources {
static let privacy = byName(name: .privacy, condition: .when(platforms: [.iOS, .macCatalyst]))
static let privacy_macOS = byName(name: .privacy_macOS, condition: .when(platforms: [.macOS]))
static let privacy_tvOS = byName(name: .privacy_tvOS, condition: .when(platforms: [.tvOS]))
static let privacy_visionOS = byName(name: .privacy_visionOS, condition: .when(platforms: [.visionOS]))
static let privacy_watchOS = byName(name: .privacy_watchOS, condition: .when(platforms: [.watchOS]))
static let bundle = byName(name: .bundle, condition: .when(platforms: [.iOS, .macCatalyst]))
static let bundle_macOS = byName(name: .bundle_macOS, condition: .when(platforms: [.macOS]))
static let bundle_tvOS = byName(name: .bundle_tvOS, condition: .when(platforms: [.tvOS]))
static let bundle_visionOS = byName(name: .bundle_visionOS, condition: .when(platforms: [.visionOS]))
static let bundle_watchOS = byName(name: .bundle_watchOS, condition: .when(platforms: [.watchOS]))
}

enum Core {
Expand Down Expand Up @@ -381,11 +384,11 @@ extension String {
static let tracker_objc = "GrowingTracker_Objc"

// Resources
static let privacy = "GrowingPrivacy"
static let privacy_macOS = "GrowingPrivacy_macOS"
static let privacy_tvOS = "GrowingPrivacy_tvOS"
static let privacy_visionOS = "GrowingPrivacy_visionOS"
static let privacy_watchOS = "GrowingPrivacy_watchOS"
static let bundle = "GrowingBundle"
static let bundle_macOS = "GrowingBundle_macOS"
static let bundle_tvOS = "GrowingBundle_tvOS"
static let bundle_visionOS = "GrowingBundle_visionOS"
static let bundle_watchOS = "GrowingBundle_watchOS"

// Core
static let autotrackerCore = "GrowingAutotrackerCore"
Expand Down Expand Up @@ -420,11 +423,11 @@ extension String {
static let tracker_objc = "GrowingTracker"

// Resources
static let privacy = "SwiftPM-Wrap/GrowingPrivacy-Wrapper"
static let privacy_macOS = "SwiftPM-Wrap/GrowingPrivacy-macOS-Wrapper"
static let privacy_tvOS = "SwiftPM-Wrap/GrowingPrivacy-tvOS-Wrapper"
static let privacy_visionOS = "SwiftPM-Wrap/GrowingPrivacy-visionOS-Wrapper"
static let privacy_watchOS = "SwiftPM-Wrap/GrowingPrivacy-watchOS-Wrapper"
static let bundle = "SwiftPM-Wrap/GrowingBundle-Wrapper"
static let bundle_macOS = "SwiftPM-Wrap/GrowingBundle-macOS-Wrapper"
static let bundle_tvOS = "SwiftPM-Wrap/GrowingBundle-tvOS-Wrapper"
static let bundle_visionOS = "SwiftPM-Wrap/GrowingBundle-visionOS-Wrapper"
static let bundle_watchOS = "SwiftPM-Wrap/GrowingBundle-watchOS-Wrapper"

// Core
static let autotrackerCore = "GrowingAutotrackerCore"
Expand Down
1 change: 1 addition & 0 deletions Resources/iOS/GrowingAnalytics.bundle/gdp-full.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@
// 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 <Foundation/Foundation.h>

NSBundle* GrowingBundleWrapper_SWIFTPM_MODULE_BUNDLE() {
return SWIFTPM_MODULE_BUNDLE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@
// limitations under the License.

// Prevent a missing umbrella header warning.

#import <Foundation/Foundation.h>

NSBundle* GrowingBundleWrapper_SWIFTPM_MODULE_BUNDLE(void);
File renamed without changes.

0 comments on commit 2632d41

Please sign in to comment.