@@ -21,6 +21,8 @@ @implementation AppDelegate
2121- (BOOL )application : (UIApplication *)application
2222didFinishLaunchingWithOptions : (NSDictionary *)launchOptions {
2323
24+ [self setBranchLogFile ];
25+
2426 appDelegate = self;
2527
2628 /*
@@ -32,20 +34,28 @@ - (BOOL)application:(UIApplication *)application
3234 // Branch.useTestBranchKey = YES; // Make sure to comment this line out for production apps!!!
3335 Branch *branch = [Branch getInstance ];
3436
37+
3538 // Change the Branch base API URL
3639 // [Branch setAPIUrl:@"https://api3.branch.io"];
3740
3841 // test pre init support
3942 // [self testDispatchToIsolationQueue:branch]
40- [branch enableLoggingAtLevel: BranchLogLevelVerbose withCallback: ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
43+
44+
45+ [Branch enableLoggingAtLevel: BranchLogLevelVerbose withCallback: ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
4146 // Handle the log message and error here. For example, printing to the console:
4247 if (error) {
4348 NSLog (@" [BranchLog] Level: %lu , Message: %@ , Error: %@ " , (unsigned long )logLevel, message, error.localizedDescription );
4449 } else {
4550 NSLog (@" [BranchLog] Level: %lu , Message: %@ " , (unsigned long )logLevel, message);
4651 }
52+
53+ NSString *logEntry = error ? [NSString stringWithFormat: @" Level: %lu , Message: %@ , Error: %@ " , (unsigned long )logLevel, message, error.localizedDescription]
54+ : [NSString stringWithFormat: @" Level: %lu , Message: %@ " , (unsigned long )logLevel, message];
55+ APPLogHookFunction ([NSDate date ], logLevel, logEntry);
4756 }];
4857
58+
4959 // Comment out in production. Un-comment to test your Branch SDK Integration:
5060 // [branch validateSDKIntegration];
5161
@@ -58,14 +68,16 @@ - (BOOL)application:(UIApplication *)application
5868 * Required: Initialize Branch, passing a deep link handler block:
5969 */
6070
61- [self setLogFile: @" OpenNInstall" ];
71+ // [self setLogFile:@"OpenNInstall"];
6272
6373 [branch setIdentity: @" Bobby Branch" ];
6474
75+ // [[Branch getInstance] setConsumerProtectionAttributionLevel:BranchAttributionLevelReduced];
76+
6577 [branch initSessionWithLaunchOptions: launchOptions andRegisterDeepLinkHandlerUsingBranchUniversalObject:
6678 ^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
6779
68- [self setLogFile: nil ];
80+ // [self setLogFile:nil];
6981 [self handleDeepLinkObject: universalObject linkProperties: linkProperties error: error];
7082 }];
7183
@@ -134,7 +146,7 @@ - (void) handleDeepLinkObject:(BranchUniversalObject*)object
134146 [storyboard instantiateViewControllerWithIdentifier: @" LogOutputViewController" ];
135147 [navigationController pushViewController: logOutputViewController animated: YES ];
136148 NSString *logOutput =
137- [NSString stringWithFormat: @" Successfully Deeplinked: \n\n %@ \n Session Details:\n\n %@ " ,
149+ [NSString stringWithFormat: @" Successfully Deeplinked! \n\n Custom Metadata Deeplink Text: %@ \n \n Session Details:\n\n %@ " ,
138150 deeplinkText, [[[Branch getInstance ] getLatestReferringParams ] description ]];
139151 logOutputViewController.logOutput = logOutput;
140152 }
@@ -175,6 +187,19 @@ - (BOOL)application:(UIApplication *)application
175187 return YES ;
176188}
177189
190+ - (void )setBranchLogFile {
191+ NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) firstObject ];
192+ NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent: @" branchlogs.txt" ];
193+
194+ // If the log file already exists, remove it to start fresh
195+ if ([[NSFileManager defaultManager ] fileExistsAtPath: logFilePath]) {
196+ [[NSFileManager defaultManager ] removeItemAtPath: logFilePath error: nil ];
197+ }
198+
199+ self.logFileName = logFilePath;
200+ }
201+
202+
178203#pragma mark - Push Notifications (Optional)
179204/*
180205// Helper method
@@ -213,10 +238,11 @@ -(void)application:(UIApplication *)application
213238
214239// hook Function for SDK - Its for taking control of Logging messages.
215240void APPLogHookFunction (NSDate *_Nonnull timestamp, BranchLogLevel level, NSString *_Nullable message) {
216- [appDelegate processLogMessage: message];
241+ NSString *formattedMessage = [NSString stringWithFormat: @" %@ [%lu ] %@ " , timestamp, (unsigned long )level, message];
242+ [appDelegate processLogMessage: formattedMessage];
217243}
218244
219- // Writes message to log File.
245+ // Writes message to Log File.
220246- (void ) processLogMessage : (NSString *)message {
221247
222248 if (!self.logFileName )
@@ -228,13 +254,12 @@ - (void) processLogMessage:(NSString *)message {
228254 [fileHandle seekToEndOfFile ];
229255 [fileHandle writeData: [message dataUsingEncoding: NSUTF8StringEncoding]];
230256 [fileHandle closeFile ];
231- } else { // Create file if it doesnt exist
257+ } else {
232258 [message writeToFile: self .logFileName
233259 atomically: NO
234260 encoding: NSStringEncodingConversionAllowLossy
235261 error: nil ];
236262 }
237- NSLog (@" %@ " , message); // Log mmessages to console - remove if required.
238263 }
239264}
240265
0 commit comments