Vote count:
0
Our application is written mostly in C++, the core of which is static library shared by multiple platforms (Win, Mac, Android, iOS).
We're adding iOS support, and have a series of functions that use libcurl that perform all of the HTTP get/post communication with our servers.
But for iOS, we're now implementing those calls using NSURLSession.
My question is pretty simple, how does the code look like to provide a C++ completionHandler to NSURLSessionDataTask?
Perhaps I'm thinking about this the wrong way, but that is the clearest I can think to make the question.
Using example code out in the world, I tried the following, to no avail. The Objective-C++ code inside the completion handler never gets called. It returns to the C++ code and never returns.
This is the objective-C++ function:
std::string get(std::string urlStr)
{
NSURLSession *defaultSession = [NSURLSession sharedSession];
NSURL * url = [NSURL URLWithString:[NSString stringWithUTF8String:urlStr.c_str()]];
__block std::string returnVal;
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
returnVal = std::string([text UTF8String]);
}
}];
[dataTask resume];
return returnVal;
}
And this is how I invoke that code in the C++:
response = get(url);
fprintf(stderr, "Data in C++: %s", response.c_str());
How can I pass a "C++ completion handler" to NSURLSessionDataTask to process the data it returned?
Aucun commentaire:
Enregistrer un commentaire