How to make a POST call and send parameters with objective-c in iOS

ios, networking, oauth, objective-c, post

Solution

To post a json data you can try this

-(void)PostJson {

__block NSMutableDictionary *resultsDictionary;

NSDictionary *userDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"first title", @"title",@"1",@"blog_id", nil];//if your json structure is something like {"title":"first title","blog_id":"1"}
if ([NSJSONSerialization isValidJSONObject:userDictionary]) {//validate it
NSError* error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:userDictionary options:NSJSONWritingPrettyPrinted error: &error];
NSURL* url = [NSURL URLWithString:@"www.google.com"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];//use POST 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[jsonData length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:jsonData];//set data
 __block NSError *error1 = [[NSError alloc] init];

 //use async way to connect network
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse* response,NSData* data,NSError* error)
{
    if ([data length]>0 && error == nil) {
        resultsDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
        NSLog(@"resultsDictionary is %@",resultsDictionary);

    } else if ([data length]==0 && error ==nil) {
        NSLog(@" download data is null");
    } else if( error!=nil) {
        NSLog(@" error is %@",error);
    }
}];
    }
}

Problem

I'm adding the ability to share an article on LinkedIn in an iOS 7 app using oauth2. I've gotten thru the authentication and have the access token. The documentation seems to be pretty clear about that, but it's odd, to actually post, things get pretty vague and poorly documented. I know I post here: http://api.linkedin.com/v1/people/~/shares appending the token. iOS being a huge platform and linkedin being very popular, i assumed i was missing something obvious, but a lot of googling has revealed the same old references to old projects. One example uses oauth2 and does get me through authentication but i can't make any api calls. Am i missing a page from linkedin itself? I've read the share api page, but when it comes to the api calls, theres not a single example on using objective-c. i'm not looking to not do anything, but I'm just kinda amazed at the lack of info. Every example just has the same code using OAMutableRequest, building the dictionary,etc. but they never explain what that is, how to incorporate that library or anything, its just strange. Is this the accepted best practice, the library hasn't been updated in 3 years so it has errors for arc and other things. All the code examples mention the same "consumer" property with no discussion of how or why that's needed. I can't seem to find how you build the post request with the parameters linkedin needs to post something on the site. Is OAMutableRequest the only way? If so, how have people updated it to work? If not, how can you build the request otherwise using NSURLRequest or something simpler. Thanks so much!

Original source