Skip to content

Commit

Permalink
Merge pull request #15 from tiktok/beta
Browse files Browse the repository at this point in the history
update to v1.3.6
  • Loading branch information
KirinelTT authored Aug 15, 2024
2 parents c5b8cf3 + 4c3de74 commit 0129378
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 86 deletions.
2 changes: 1 addition & 1 deletion TikTokBusinessSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'TikTokBusinessSDK'
s.version = '1.3.4'
s.version = '1.3.6'
s.summary = 'TikTok Business SDK for iOS'

s.description = <<-DESC
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "4A95C322-F14B-4014-8DEB-64668E504CAE"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "TikTokBusinessSDK/TikTokDeviceInfo.m"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "77"
endingLineNumber = "77"
landmarkName = "-collectUserAgentWithCompletion:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.SymbolicBreakpoint">
<BreakpointContent
Expand Down
6 changes: 3 additions & 3 deletions TikTokBusinessSDK/TikTokBusiness.m
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,10 @@ - (void)initializeSdk:(TikTokConfig *)tiktokConfig completionHandler:(void (^)(B
- (void)monitorInitialization:(NSNumber *)initStartTime andEndTime:(NSNumber *)initEndTime
{
NSDictionary *startMeta = @{
@"ts": initStartTime,
@"ts": [NSNumber numberWithDouble:[initStartTime doubleValue]],
};
NSDictionary *endMeta = @{
@"ts": initEndTime,
@"ts": [NSNumber numberWithDouble:[initEndTime doubleValue]],
@"latency": [NSNumber numberWithDouble:[initEndTime floatValue] - [initStartTime floatValue]],
};
NSDictionary *monitorInitStartProperties = @{
Expand Down Expand Up @@ -641,7 +641,7 @@ - (void)setTrackingEnabled:(BOOL)trackingEnabled

- (void)setCustomUserAgent:(NSString *)customUserAgent
{
[[TikTokUserAgentCollector singleton] setUserAgent:customUserAgent];
[[TikTokUserAgentCollector singleton] setCustomUserAgent:customUserAgent];
}

- (void)updateAccessToken:(nonnull NSString *)accessToken
Expand Down
17 changes: 16 additions & 1 deletion TikTokBusinessSDK/TikTokBusinessSDKMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#import <Foundation/Foundation.h>

#define SDK_VERSION @"1.3.5"
#define SDK_VERSION @"1.3.6"

#define TT_CONFIG_PATH @"/api/v1/app_sdk/config"
#define TT_BATCH_EVENT_PATH @"/api/v1/app_sdk/batch"
Expand All @@ -31,3 +31,18 @@ FOUNDATION_EXPORT NSString * const TTUserDefaultsKey_firstLaunchTime;
#define TTSafeString(__string) ((__string && [__string isKindOfClass:[NSString class]]) ? __string :@"")

#define TTSafeDictionary(__aDictionary) ((__aDictionary && [__aDictionary isKindOfClass:[NSDictionary class]]) ? __aDictionary :@{})

#ifndef tt_weakify
#if __has_feature(objc_arc)
#define tt_weakify(object) __weak __typeof__(object) weak##object = object;
#else
#define tt_weakify(object) __block __typeof__(object) block##object = object;
#endif
#endif
#ifndef tt_strongify
#if __has_feature(objc_arc)
#define tt_strongify(object) __typeof__(object) object = weak##object;
#else
#define tt_strongify(object) __typeof__(object) object = block##object;
#endif
#endif
2 changes: 1 addition & 1 deletion TikTokBusinessSDK/TikTokConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ - (void)enableLDUMode

- (void)setCustomUserAgent: (NSString *)customUserAgent
{
[[TikTokUserAgentCollector singleton] setUserAgent:customUserAgent];
[[TikTokUserAgentCollector singleton] setCustomUserAgent:customUserAgent];
[self.logger info:@"[TikTokConfig] User Agent set to: %@", customUserAgent];
}

Expand Down
21 changes: 0 additions & 21 deletions TikTokBusinessSDK/TikTokDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

@interface TikTokDeviceInfo()

@property (nonatomic, strong, readwrite) WKWebView *webView;

@end

@implementation TikTokDeviceInfo
Expand Down Expand Up @@ -62,25 +60,6 @@ - (NSString *)getUserAgent
return [TikTokUserAgentCollector singleton].userAgent;
}

- (void)collectUserAgentWithCompletion:(void (^)(NSString *userAgent))completion {
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.webView) {
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero];
}

[self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
if(completion){
if(response) {
self.webView = nil;
completion(response);
} else {
[self collectUserAgentWithCompletion:completion];
}
}
}];
});
}


static NSString * getIDFA(void) {
NSNumber *idfaStartTime = [TikTokAppEventUtility getCurrentTimestampAsNumber];
Expand Down
11 changes: 10 additions & 1 deletion TikTokBusinessSDK/TikTokErrorHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,16 @@ + (void)clearCrashReportFiles

+ (nullable NSData *)_loadCrashLog:(NSString *)crashLog
{
return [NSData dataWithContentsOfFile:[directoryPath stringByAppendingPathComponent:crashLog] options:NSDataReadingMappedIfSafe error:nil];
NSData *resultData = nil;
NSFileManager *fm = [NSFileManager defaultManager];
NSString *path = [directoryPath stringByAppendingPathComponent:crashLog];
BOOL exists = [fm fileExistsAtPath:path];
if (exists) {
resultData = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
} else {
resultData = nil;
}
return resultData;
}

+ (NSArray<NSString *> *)_getCrashLogFileNames:(NSArray<NSString *> *)files
Expand Down
11 changes: 8 additions & 3 deletions TikTokBusinessSDK/TikTokRequestHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ - (void)getRemoteSwitch:(TikTokConfig *)config
if(self.session == nil) {
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}

__block NSNumber *networkStartTime = [TikTokAppEventUtility getCurrentTimestampAsNumber];
tt_weakify(self)
[[self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
tt_strongify(self)
BOOL isSwitchOn = nil;
// handle basic connectivity issues
if(error) {
Expand Down Expand Up @@ -181,7 +184,7 @@ - (void)getRemoteSwitch:(TikTokConfig *)config
@"ts": configMonitorStartTime,
@"latency": [NSNumber numberWithInt:[configMonitorEndTime intValue] - [configMonitorStartTime intValue]],
@"success": [NSNumber numberWithBool:true],
@"log_id": [dataDictionary objectForKey:@"request_id"],
@"log_id": TTSafeString([dataDictionary objectForKey:@"request_id"]),
};
NSDictionary *monitorUserAgentStartProperties = @{
@"monitor_type": @"metric",
Expand Down Expand Up @@ -308,8 +311,9 @@ - (void)sendBatchRequest:(NSArray *)eventsToBeFlushed
}

__block NSNumber *networkStartTime = [TikTokAppEventUtility getCurrentTimestampAsNumber];
tt_weakify(self)
[[self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

tt_strongify(self)
// handle basic connectivity issues
if(error) {
[self.logger error:@"[TikTokRequestHandler] error in connection: %@", error];
Expand Down Expand Up @@ -505,8 +509,9 @@ - (void)sendMonitorRequest:(NSArray *)eventsToBeFlushed
if(self.session == nil) {
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}
tt_weakify(self)
[[self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

tt_strongify(self)
// handle basic connectivity issues
if(error) {
[self.logger error:@"[TikTokRequestHandler] error in connection: %@", error];
Expand Down
3 changes: 1 addition & 2 deletions TikTokBusinessSDK/TikTokUserAgentCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
* @brief Handles asynchronous Javascript call to the WebKit browser
*/
- (void)loadUserAgentWithCompletion:(void(^)(NSString * _Nullable userAgent))completion;
+ (void)setUserAgent:(NSString *)userAgent;
- (void)setUserAgent:(NSString *)userAgent;
- (void)setCustomUserAgent:(NSString *)userAgent;

@end

Expand Down
87 changes: 50 additions & 37 deletions TikTokBusinessSDK/TikTokUserAgentCollector.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
//

#import "TikTokUserAgentCollector.h"
#import <WebKit/WKWebView.h>
#import "TikTokBusinessSDKMacros.h"
#import "TikTokTypeUtility.h"

static NSString *TT_UserAgent = @"TT_UserAgent";

@interface TikTokUserAgentCollector()

@property (nonatomic, strong, readwrite) WKWebView *webView;
@property (nonatomic, assign) BOOL updatedUa;

@end

Expand All @@ -29,57 +33,66 @@ + (TikTokUserAgentCollector *)singleton
- (instancetype)init
{
self = [super init];
if(self == nil){
return nil;
if(self) {
self.userAgent = [[NSUserDefaults standardUserDefaults] objectForKey:TT_UserAgent];
self.updatedUa = NO;
}
return self;
}

- (void)loadUserAgentWithCompletion:(void (^)(NSString * _Nullable))completion
{
[self collectUserAgentWithCompletion: ^(NSString * _Nullable userAgent) {
if(self.userAgent == nil){
self.userAgent = userAgent;
}
if(completion){
completion(userAgent);
}
}];
}

- (void)collectUserAgentWithCompletion:(void (^)(NSString *userAgent))completion
{
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.webView) {
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero];
}

[self.webView evaluateJavaScript:@"navigator.userAgent;" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
if (completion) {
if (response) {
// release the webview
self.webView = nil;

completion(response);
} else {
// retry if we failed to obtain user agent. This occasionally occurs on simulator.
[self collectUserAgentWithCompletion:completion];
if (!self.updatedUa && !TTCheckValidString(self.userAgent)) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.webView) {
@try {
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero];
} @catch (NSException *exception) {
} @finally {
}
}
}];
});
[self _update];
});
}
if (completion) {
completion(self.userAgent);
}
}

+ (void)setUserAgent:(NSString *)userAgent
{
@synchronized (self) {
[[TikTokUserAgentCollector singleton] setUserAgent:userAgent];
- (void)_update {
if (!self.webView) {
return;
}
tt_weakify(self)
[self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
tt_strongify(self)
if (!TTCheckValidString(result)) {
// Don't replace the existing value if fetched nil.
return;
}
self.userAgent = result;
self.updatedUa = YES;
if (TTCheckValidString(result)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:result forKey:TT_UserAgent];
[userDefaults synchronize];
});
}
}];
}

- (void)setUserAgent:(NSString *)userAgent
- (void)setCustomUserAgent:(NSString *)userAgent
{
_userAgent = userAgent;
self.updatedUa = YES;
if (TTCheckValidString(userAgent)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:userAgent forKey:TT_UserAgent];
[userDefaults synchronize];
});
}
}

@end

0 comments on commit 0129378

Please sign in to comment.