Home > Uncategorized > Making a HTTP request from objective C

Making a HTTP request from objective C

f58bb86caadcc1cda081a893f5c35b6e

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];

Advertisements
Categories: Uncategorized
  1. February 12, 2017 at 12:22 am

    And don’t forget to set ATS if you need to call a HTTP endpoint

    NSAppTransportSecurity

    NSAllowsArbitraryLoads

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: