0

I am doing an instagram appliaction. In that I need to post like on Images. I am not able to post . I am following instgram API. We need to use following url

https://api.instagram.com/v1/media/{media-id}/likes

We need to set media id like

https://api.instagram.com/v1/media/649114513807113248_1032025382/likes

media id = 649114513807113248_1032025382

My code to post:

NSString *urlString = [NSString            stringWithFormat:@"https://api.instagram.com/v1/media/649114513807113248_1032025382/likes"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];


[request setHTTPMethod:@"POST"];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
                                                             delegate:self];
[connection start];

But like is not posting. Can anyone help me ?

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
deepti
  • 206
  • 2
  • 15
  • Did you implement the NSURLConnection delegates? Are you getting a response? What does it say? – Anil Feb 10 '14 at 06:58
  • Ya I have implemented didReceiveData() delegate but its showing some numbers , no valid data – deepti Feb 10 '14 at 07:00
  • Can you post the output here? – Anil Feb 10 '14 at 07:03
  • 1
    Can u please convert the data into string and log it then show here NSString *dataString = [[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding]; NSLog(@"%@",); – LML Feb 10 '14 at 07:08
  • 1
    That is a NSData output. Convert that to a NSString or NSDictionary. Here are a few links: http://stackoverflow.com/questions/12603047/how-to-convert-nsdata-to-nsdictionary http://stackoverflow.com/questions/2467844/convert-utf-8-encoded-nsdata-to-nsstring – Anil Feb 10 '14 at 07:10
  • @Anil It is giving null when I am converting NSData to NSDictionary. – deepti Feb 10 '14 at 07:17
  • you want to share the images to fb or tweet – codercat Feb 10 '14 at 07:19
  • @deepti convert it into string as I mentioned above.or your response is something other than string data? – LML Feb 10 '14 at 07:24
  • @Anil Thanks for your reply. Now I got the response . Actually AccessToken was missing on that. Now it is working fine. – deepti Feb 10 '14 at 08:47

3 Answers3

1

You can post your data in better way using AFNetworking framework. Samples are also provided on github.

http://afnetworking.com/

Vinay Bagale
  • 2,361
  • 4
  • 36
  • 45
1

MGInstagram and DMActivityInstagram is a iOS Utility for posting Images to Instagram from your app.

// Uses our instagram instance to do a request
- (void) postMessage:(NSString *)message mediaId:(NSString *)mediaId {
    NSString* methodName = [NSString stringWithFormat:@"media/%@/comments", mediaId];
    NSMutableDictionary* params = @{"text" : message};
    [m_instagram requestWithMethodName:methodName params:params httpMethod:@"POST" delegate:self];
}

// IGRequestDelegate method that is called once we get a response
- (void)request:(IGRequest *)request didLoad:(id)result {
  // post message was ok
}
- (void)request:(IGRequest *)request didFailWithError:(NSError *)error {
  // post message fails
}

Reference from https://github.com/crino/instagram-ios-sdk/issues/27

if you are share the images to social network use below link it have sample app with tutorial

share-an-image-on-instagram-in-ios/

codercat
  • 22,873
  • 9
  • 61
  • 85
1

better to use AFNetworking for post, bellow is a sample , please have a look

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock
{
    NSMutableURLRequest *apiRequest = 
        [self multipartFormRequestWithMethod:@"POST" 
                                        path:kAPIPath 
                                  parameters:params 
                   constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                       //TODO: attach file if needed
    }];

    AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //success!
        completionBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //failure :(
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    }];

    [operation start];
}
Arun Kumar
  • 154
  • 10