From 021f274a3ec1e6207a598c4db5d1effe5b30d94d Mon Sep 17 00:00:00 2001 From: emawby Date: Tue, 14 Dec 2021 09:47:25 -0800 Subject: [PATCH] Putting try catch around fileHandle writeData 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. --- iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m b/iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m index f773afa61..589d75bc5 100644 --- a/iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m +++ b/iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m @@ -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: %@", fileHandleError.localizedDescription]]; + } } - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)aResponse completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {