@@ -46,7 +46,37 @@ @implementation DirectDownloadDelegate
4646@synthesize error, response, done;
4747
4848-(void )URLSession : (NSURLSession *)session dataTask : (NSURLSessionDataTask *)dataTask didReceiveData : (NSData *)data {
49- [outputHandle writeData: data];
49+ NSError * __autoreleasing fileHandleError;
50+ NSError * __autoreleasing *fileHandleErrorPointer = &fileHandleError;
51+ if (@available (iOS 13.0 , *)) {
52+ // We need to use NSInvocation for reflection because performSelector cannot take pointer parameters
53+ SEL writeDataSelector = NSSelectorFromString (@" writeData:error:" );
54+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: [NSFileHandle instanceMethodSignatureForSelector: writeDataSelector]];
55+ [invocation setTarget: outputHandle];
56+ [invocation setSelector: writeDataSelector];
57+ /*
58+ From Apple's Documentation on NSInvocation:
59+ Indices 0 and 1 indicate the hidden arguments self and _cmd, respectively;
60+ you should set these values directly with the target and selector properties.
61+ Use indices 2 and greater for the arguments normally passed in a message.
62+ */
63+ [invocation setArgument: &data atIndex: 2 ];
64+ [invocation setArgument: &fileHandleErrorPointer atIndex: 3 ];
65+ [invocation invoke ];
66+ } else {
67+ @try {
68+ [outputHandle writeData: data];
69+ } @catch (NSException *e) {
70+ NSDictionary *userInfo = @{
71+ NSLocalizedDescriptionKey : @" Failed to write attachment data to filehandle" ,
72+ };
73+ fileHandleError = [NSError errorWithDomain: @" com.onesignal.download" code: 0 userInfo: userInfo];
74+ }
75+ }
76+
77+ if (fileHandleError != nil ) {
78+ [OneSignalLog onesignalLog: ONE_S_LL_ERROR message: [NSString stringWithFormat: @" OneSignal Error encountered while downloading attachment: %@ " , fileHandleError.localizedDescription]];
79+ }
5080}
5181
5282- (void )URLSession : (NSURLSession *)session dataTask : (NSURLSessionDataTask *)dataTask didReceiveResponse : (NSURLResponse *)aResponse completionHandler : (void (^)(NSURLSessionResponseDisposition ))completionHandler {
0 commit comments