Skip to content

Commit

Permalink
Putting try catch around fileHandle writeData
Browse files Browse the repository at this point in the history
For iOS 13+ devices we will now use writeData:error: which returns a boolean and populates the NSError parameter with the failure reason if it  fails.

For < iOS 13 we will place a try catch around writeData: to avoid crashes.

In both cases we will log the failure reason. Note that this is logged on the NotificationServiceExtension process not the main app process.
  • Loading branch information
emawby committed Dec 14, 2021
1 parent fd06483 commit fdd50d2
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,23 @@ @implementation DirectDownloadDelegate
@synthesize error, response, done;

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
[outputHandle writeData:data];
NSError *fileHandleError;
if (@available(iOS 13.0, *)) {
[outputHandle writeData:data error:&fileHandleError];
} else {
@try {
[outputHandle writeData:data];
} @catch (NSException *e) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey : @"Failed to write attachment data to filehandle",
};
fileHandleError = [NSError errorWithDomain:@"com.onesignal.download" code:0 userInfo:userInfo];
}
}

if (fileHandleError != nil) {
[OneSignal onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"OneSignal Error encountered while downloading attachment: %@", error.localizedDescription]];
}
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)aResponse completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
Expand Down

0 comments on commit fdd50d2

Please sign in to comment.