Troubles connecting with Obj C

Hey guys,

I am trying to develop a Halo 5 stats app for my iOS programming class and have had an issue connecting using the code given in the documentation. Specifically, the first thing I wish to retrieve is an emblem for my spartan and I am getting the same errors. Here are the errors i get:

NSErrorFailingURLStringKey=https://www.haloapi.com/profile/h5/profiles/ArsonHoliday/emblem, NSErrorFailingURLKey=https://www.haloapi.com/profile/h5/profiles/ArsonHoliday/emblem, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}
2016-04-06 15:21:09.413 h5stats[10991:936959] _BSMachError: (os/kern) invalid capability (20)
2016-04-06 15:21:09.413 h5stats[10991:936959] _BSMachError: (os/kern) invalid name (15)

I know my subscription key is valid, and when I use the try it button in the documentation, it seems like that url should be valid. Here is the code:

NSString* path = [NSString stringWithFormat:@“https://www.haloapi.com/profile/h5/profiles/%@/emblem”, item.gamertag];

//optional parameter for emblem size, not using
/*
NSArray* array = @[
// Request parameters
@“entities=true”,
@“size=95”
];
//@“size={number}”,
/
//NSString
string = [array componentsJoinedByString:@"&"];
//path = [path stringByAppendingFormat:@"?%@", string];
// NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSLog(@"%@", path);

NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
[_request setHTTPMethod:@“GET”];
// Request headers
[_request setValue:@"{sub key}" forHTTPHeaderField:@“Ocp-Apim-Subscription-Key”];
// Request body
[_request setHTTPBody:[@"{body}" dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse *response = nil;
NSError error = nil;
NSData
_connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];

What is {body} supposed to contain? and am I supposed to fill that? I also ran the python code and it worked in Terminal, but it didn’t seem to be doing anything with {body} either.

Followed by some error handling. If anyone has any iOS/Objective C experience and has the time to help my out, I’d be very grateful.

Thanks,
A fellow Spartan

Hi! I have was able to get a good response from the API without using setting the body property of the _request variable.

Below is my functional code for getting the spartanImage:

NSString* path = [NSString stringWithFormat:@“https://www.haloapi.com/profile/h5/profiles/%@/spartan”, gamertag];
NSArray* array = @[
// Request parameters
@“entities=true”,
[NSString stringWithFormat:@“size=%@”,EMBLEM_SIZE_190],
//@“crop=portrait”,
];

NSString* string = [array componentsJoinedByString:@"&"];
path = [path stringByAppendingFormat:@"?%@", string];

NSLog(@"%@", path);

NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
[_request setHTTPMethod:@“GET”];
// Request headers
[_request setValue:PRIMARY_SUBSCRIPTION_KEY forHTTPHeaderField:@“Ocp-Apim-Subscription-Key”];

NSURLResponse *response = nil;
NSError error = nil;
NSData
_connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];

if (nil != error)
{
NSLog(@“Error: %@”, error);
}
else
{
if (_connectionData != nil)
{
// UIImage *mirroredImage = [Util mirrorImageDiagonal1:[UIImage imageWithData:_connectionData]];
//
// [self.imageViewSpartanImage setImage:mirroredImage];
// self.imageViewSpartanImage.transform = CGAffineTransformMakeRotation(-M_PI/2);

[self.imageViewSpartanImage setImage:[UIImage imageWithData:_connectionData]];

}
}

Sorry about code identation.

Hope this helps you.

> 2533274958932941;2:
> Hi! I have was able to get a good response from the API without using setting the body property of the _request variable.
>
> Below is my functional code for getting the spartanImage:
>
> NSString* path = [NSString stringWithFormat:@“https://www.haloapi.com/profile/h5/profiles/%@/spartan”, gamertag];
> NSArray* array = @[
> // Request parameters
> @“entities=true”,
> [NSString stringWithFormat:@“size=%@”,EMBLEM_SIZE_190],
> //@“crop=portrait”,
> ];
>
> NSString* string = [array componentsJoinedByString:@"&"];
> path = [path stringByAppendingFormat:@"?%@", string];
>
> NSLog(@"%@", path);
>
> NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
> [_request setHTTPMethod:@“GET”];
> // Request headers
> [_request setValue:PRIMARY_SUBSCRIPTION_KEY forHTTPHeaderField:@“Ocp-Apim-Subscription-Key”];
>
> NSURLResponse *response = nil;
> NSError error = nil;
> NSData
_connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];
>
> if (nil != error)
> {
> NSLog(@“Error: %@”, error);
> }
> else
> {
> if (_connectionData != nil)
> {
> // UIImage *mirroredImage = [Util mirrorImageDiagonal1:[UIImage imageWithData:_connectionData]];
> //
> // [self.imageViewSpartanImage setImage:mirroredImage];
> // self.imageViewSpartanImage.transform = CGAffineTransformMakeRotation(-M_PI/2);
>
> [self.imageViewSpartanImage setImage:[UIImage imageWithData:_connectionData]];
>
> }
> }
>
> Sorry about code identation.
>
> Hope this helps you.

I had to alter a couple things, but this worked! Thanks so much for the help! I probably should have tried doing something a little easier for my first app, but here goes nothing. Cheers!