I have a MacCatalyst app that can work in two ways:
- as a classic app with a GUI
- as a command line app without a GUI
I need to make an HTTP request to an API when the app is launched in command line. However this does not work and I don't know why.
Here is a simplified example of my case (my app is written in Objective C)
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
if(argc >= 2 && [condition on argv[1]]){
@try {
NSURL* requestUrl = [NSURL URLWithString:@"https://google.com"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:requestUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"GET"];
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest: request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// never called
fprintf(stdout, "%s", [@"Request Complete" UTF8String]);
if (error != nil) {
// return error
return;
}
// use data
}];
[dataTask resume];
}@catch(NSException* e) {
fprintf(stdout, "%s", [@"Exception" UTF8String]); // never called
}@catch(NSError*e) {
fprintf(stdout, "%s", [@"Error" UTF8String]); // never called
}
} else {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
}
In the above case, the dataTask completionHandler is never called, no exception or error is thrown, I get no error in the console, the script just stops after [dataTask resume].
I also tried using delegates and using background sessions with upload or download tasks but none of them worked nor thrown any error or provided any information as to what's not working or what's not allowed. When using delegates, none of the delegate callbacks is ever called.
Does anyone know what's going on ?