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

[SDK-2458] Implement Consumer Protection Preferences #1407

Merged
merged 20 commits into from
Nov 18, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ fastlane/test_output
# Ruby stuff
vendor
.bundle
DeepLinkDemo/DeepLinkDemo.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings
21 changes: 21 additions & 0 deletions Branch-TestBed/Branch-SDK-Tests/BranchClassTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,25 @@ - (void)testSetDMAParamsForEEA {
[[BNCPreferenceHelper sharedInstance] writeObjectToDefaults:@"bnc_dma_ad_user_data" value:nil];
}

- (void)testSetConsumerProtectionAttributionLevel {
// Set to Reduced and check
Branch *branch = [Branch getInstance];
[branch setConsumerProtectionAttributionLevel:BranchAttributionLevelReduced];
XCTAssertEqual([BNCPreferenceHelper sharedInstance].attributionLevel, BranchAttributionLevelReduced);

// Set to Minimal and check
[branch setConsumerProtectionAttributionLevel:BranchAttributionLevelMinimal];
XCTAssertEqual([BNCPreferenceHelper sharedInstance].attributionLevel, BranchAttributionLevelMinimal);

// Set to None and check
[branch setConsumerProtectionAttributionLevel:BranchAttributionLevelNone];
XCTAssertEqual([BNCPreferenceHelper sharedInstance].attributionLevel, BranchAttributionLevelNone);

// Set to Full and check
[branch setConsumerProtectionAttributionLevel:BranchAttributionLevelFull];
XCTAssertEqual([BNCPreferenceHelper sharedInstance].attributionLevel, BranchAttributionLevelFull);

}


@end
41 changes: 33 additions & 8 deletions Branch-TestBed/Branch-TestBed/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ @implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[self setBranchLogFile];

appDelegate = self;

/*
Expand All @@ -32,20 +34,28 @@ - (BOOL)application:(UIApplication *)application
// Branch.useTestBranchKey = YES; // Make sure to comment this line out for production apps!!!
Branch *branch = [Branch getInstance];


// Change the Branch base API URL
//[Branch setAPIUrl:@"https://api3.branch.io"];

// test pre init support
//[self testDispatchToIsolationQueue:branch]
[branch enableLoggingAtLevel:BranchLogLevelVerbose withCallback:^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {


[Branch enableLoggingAtLevel:BranchLogLevelVerbose withCallback:^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
// Handle the log message and error here. For example, printing to the console:
if (error) {
NSLog(@"[BranchLog] Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription);
} else {
NSLog(@"[BranchLog] Level: %lu, Message: %@", (unsigned long)logLevel, message);
}

NSString *logEntry = error ? [NSString stringWithFormat:@"Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription]
: [NSString stringWithFormat:@"Level: %lu, Message: %@", (unsigned long)logLevel, message];
APPLogHookFunction([NSDate date], logLevel, logEntry);
}];


// Comment out in production. Un-comment to test your Branch SDK Integration:
//[branch validateSDKIntegration];

Expand All @@ -58,14 +68,16 @@ - (BOOL)application:(UIApplication *)application
* Required: Initialize Branch, passing a deep link handler block:
*/

[self setLogFile:@"OpenNInstall"];
//[self setLogFile:@"OpenNInstall"];

[branch setIdentity:@"Bobby Branch"];

//[[Branch getInstance] setConsumerProtectionAttributionLevel:BranchAttributionLevelReduced];

[branch initSessionWithLaunchOptions:launchOptions andRegisterDeepLinkHandlerUsingBranchUniversalObject:
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {

[self setLogFile:nil];
//[self setLogFile:nil];
[self handleDeepLinkObject:universalObject linkProperties:linkProperties error:error];
}];

Expand Down Expand Up @@ -134,7 +146,7 @@ - (void) handleDeepLinkObject:(BranchUniversalObject*)object
[storyboard instantiateViewControllerWithIdentifier:@"LogOutputViewController"];
[navigationController pushViewController:logOutputViewController animated:YES];
NSString *logOutput =
[NSString stringWithFormat:@"Successfully Deeplinked:\n\n%@\nSession Details:\n\n%@",
[NSString stringWithFormat:@"Successfully Deeplinked!\n\nCustom Metadata Deeplink Text: %@\n\nSession Details:\n\n%@",
deeplinkText, [[[Branch getInstance] getLatestReferringParams] description]];
logOutputViewController.logOutput = logOutput;
}
Expand Down Expand Up @@ -175,6 +187,19 @@ - (BOOL)application:(UIApplication *)application
return YES;
}

- (void)setBranchLogFile {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:@"branchlogs.txt"];

// If the log file already exists, remove it to start fresh
if ([[NSFileManager defaultManager] fileExistsAtPath:logFilePath]) {
[[NSFileManager defaultManager] removeItemAtPath:logFilePath error:nil];
}

self.logFileName = logFilePath;
}


#pragma mark - Push Notifications (Optional)
/*
// Helper method
Expand Down Expand Up @@ -213,10 +238,11 @@ -(void)application:(UIApplication *)application

// hook Function for SDK - Its for taking control of Logging messages.
void APPLogHookFunction(NSDate*_Nonnull timestamp, BranchLogLevel level, NSString*_Nullable message) {
[appDelegate processLogMessage:message];
NSString *formattedMessage = [NSString stringWithFormat:@"%@ [%lu] %@", timestamp, (unsigned long)level, message];
[appDelegate processLogMessage:formattedMessage];
}

// Writes message to log File.
// Writes message to Log File.
- (void) processLogMessage:(NSString *)message {

if (!self.logFileName)
Expand All @@ -228,13 +254,12 @@ - (void) processLogMessage:(NSString *)message {
[fileHandle seekToEndOfFile];
[fileHandle writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
} else { // Create file if it doesnt exist
} else {
[message writeToFile:self.logFileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}
NSLog(@"%@", message); // Log mmessages to console - remove if required.
}
}

Expand Down
25 changes: 25 additions & 0 deletions Branch-TestBed/Branch-TestBed/LogOutputViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,36 @@ @implementation LogOutputViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.logOutputTextView.text = _logOutput;

UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear Logs"
style:UIBarButtonItemStylePlain
target:self
action:@selector(clearLogs)];
self.navigationItem.rightBarButtonItem = clearButton;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)clearLogs {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:@"branchlogs.txt"];

NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:logFilePath error:&error];

if (error) {
NSLog(@"Error clearing log file: %@", error.localizedDescription);
} else {
self.logOutputTextView.text = @"Logs cleared.";
NSLog(@"Log file cleared successfully.");

[self.navigationController popViewControllerAnimated:YES];

}
}


@end
Loading
Loading