@@ -21,6 +21,8 @@ @implementation AppDelegate
21
21
- (BOOL )application : (UIApplication *)application
22
22
didFinishLaunchingWithOptions : (NSDictionary *)launchOptions {
23
23
24
+ [self setBranchLogFile ];
25
+
24
26
appDelegate = self;
25
27
26
28
/*
@@ -32,20 +34,28 @@ - (BOOL)application:(UIApplication *)application
32
34
// Branch.useTestBranchKey = YES; // Make sure to comment this line out for production apps!!!
33
35
Branch *branch = [Branch getInstance ];
34
36
37
+
35
38
// Change the Branch base API URL
36
39
// [Branch setAPIUrl:@"https://api3.branch.io"];
37
40
38
41
// test pre init support
39
42
// [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) {
41
46
// Handle the log message and error here. For example, printing to the console:
42
47
if (error) {
43
48
NSLog (@" [BranchLog] Level: %lu , Message: %@ , Error: %@ " , (unsigned long )logLevel, message, error.localizedDescription );
44
49
} else {
45
50
NSLog (@" [BranchLog] Level: %lu , Message: %@ " , (unsigned long )logLevel, message);
46
51
}
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);
47
56
}];
48
57
58
+
49
59
// Comment out in production. Un-comment to test your Branch SDK Integration:
50
60
// [branch validateSDKIntegration];
51
61
@@ -58,14 +68,16 @@ - (BOOL)application:(UIApplication *)application
58
68
* Required: Initialize Branch, passing a deep link handler block:
59
69
*/
60
70
61
- [self setLogFile: @" OpenNInstall" ];
71
+ // [self setLogFile:@"OpenNInstall"];
62
72
63
73
[branch setIdentity: @" Bobby Branch" ];
64
74
75
+ // [[Branch getInstance] setConsumerProtectionAttributionLevel:BranchAttributionLevelReduced];
76
+
65
77
[branch initSessionWithLaunchOptions: launchOptions andRegisterDeepLinkHandlerUsingBranchUniversalObject:
66
78
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
67
79
68
- [self setLogFile: nil ];
80
+ // [self setLogFile:nil];
69
81
[self handleDeepLinkObject: universalObject linkProperties: linkProperties error: error];
70
82
}];
71
83
@@ -134,7 +146,7 @@ - (void) handleDeepLinkObject:(BranchUniversalObject*)object
134
146
[storyboard instantiateViewControllerWithIdentifier: @" LogOutputViewController" ];
135
147
[navigationController pushViewController: logOutputViewController animated: YES ];
136
148
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 %@ " ,
138
150
deeplinkText, [[[Branch getInstance ] getLatestReferringParams ] description ]];
139
151
logOutputViewController.logOutput = logOutput;
140
152
}
@@ -175,6 +187,19 @@ - (BOOL)application:(UIApplication *)application
175
187
return YES ;
176
188
}
177
189
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
+
178
203
#pragma mark - Push Notifications (Optional)
179
204
/*
180
205
// Helper method
@@ -213,10 +238,11 @@ -(void)application:(UIApplication *)application
213
238
214
239
// hook Function for SDK - Its for taking control of Logging messages.
215
240
void 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];
217
243
}
218
244
219
- // Writes message to log File.
245
+ // Writes message to Log File.
220
246
- (void ) processLogMessage : (NSString *)message {
221
247
222
248
if (!self.logFileName )
@@ -228,13 +254,12 @@ - (void) processLogMessage:(NSString *)message {
228
254
[fileHandle seekToEndOfFile ];
229
255
[fileHandle writeData: [message dataUsingEncoding: NSUTF8StringEncoding]];
230
256
[fileHandle closeFile ];
231
- } else { // Create file if it doesnt exist
257
+ } else {
232
258
[message writeToFile: self .logFileName
233
259
atomically: NO
234
260
encoding: NSStringEncodingConversionAllowLossy
235
261
error: nil ];
236
262
}
237
- NSLog (@" %@ " , message); // Log mmessages to console - remove if required.
238
263
}
239
264
}
240
265
0 commit comments