【问题标题】:How to Post data in iOS?如何在 iOS 中发布数据?
【发布时间】:2014-02-10 06:44:27
【问题描述】:

我正在做一个 Instagram 应用程序。在那我需要在图片上发布。我无法发帖。我正在关注 instgram API。我们需要使用以下网址

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

我们需要像这样设置媒体ID

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

媒体 ID = 649114513807113248_1032025382

我要发布的代码:

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

但是点赞不是发帖。谁能帮我 ?

【问题讨论】:

  • 你实现了 NSURLConnection 委托吗?你得到回应了吗?它说什么?
  • 是的,我已经实现了 didReceiveData() 委托,但它显示了一些数字,没有有效数据
  • 你能把输出贴在这里吗?
  • 你能不能把数据转换成字符串并记录下来然后在这里显示 NSString *dataString = [[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding]; NSLog(@"%@",);
  • 这是一个 NSData 输出。将其转换为 NSString 或 NSDictionary。这里有几个链接:stackoverflow.com/questions/12603047/…stackoverflow.com/questions/2467844/…

标签: ios post


【解决方案1】:

您可以使用 AFNetworking 框架以更好的方式发布您的数据。 github上也提供了示例。

http://afnetworking.com/

【讨论】:

    【解决方案2】:

    MGInstagramDMActivityInstagram 是一个 iOS 实用程序,用于将图像从您的应用程序发布到 Instagram。

    // 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
    }
    

    来自https://github.com/crino/instagram-ios-sdk/issues/27的参考

    如果您将图像分享到社交网络,请使用下面的链接,它有带有教程的示例应用程序

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

    【讨论】:

      【解决方案3】:

      最好使用 AFNetworking 发帖,下面是示例,请看一下

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-22
        • 1970-01-01
        • 2020-09-23
        • 2021-01-04
        • 1970-01-01
        • 2013-05-04
        • 1970-01-01
        相关资源
        最近更新 更多