Skip to content

Commit 021f274

Browse files
committed
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.
1 parent fd06483 commit 021f274

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,23 @@ @implementation DirectDownloadDelegate
7575
@synthesize error, response, done;
7676

7777
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
78-
[outputHandle writeData:data];
78+
NSError *fileHandleError;
79+
if (@available(iOS 13.0, *)) {
80+
[outputHandle writeData:data error:&fileHandleError];
81+
} else {
82+
@try {
83+
[outputHandle writeData:data];
84+
} @catch (NSException *e) {
85+
NSDictionary *userInfo = @{
86+
NSLocalizedDescriptionKey : @"Failed to write attachment data to filehandle",
87+
};
88+
fileHandleError = [NSError errorWithDomain:@"com.onesignal.download" code:0 userInfo:userInfo];
89+
}
90+
}
91+
92+
if (fileHandleError != nil) {
93+
[OneSignal onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"OneSignal Error encountered while downloading attachment: %@", fileHandleError.localizedDescription]];
94+
}
7995
}
8096

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

0 commit comments

Comments
 (0)