Making a HTTP request from objective C
I don’t normally write about objective C, since I’m not experienced in programming in that language, but I thought I’d post this really crucial, yet simple way to make a HTTP request in objective C, and get a string (NSString) back
NSString *dataUrl = @”https://yourAPI.com/something”;
NSURL *url = [NSURL URLWithString:dataUrl];
NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (error)
{
// report error.localizedDescription to the user.
return;
}
NSString *content = [[NSString alloc]
initWithData:data
encoding:NSASCIIStringEncoding];
// Now Content contains the returned data.
}];
[downloadTask resume];
And don’t forget to set ATS if you need to call a HTTP endpoint
NSAppTransportSecurity
NSAllowsArbitraryLoads
LikeLike