如何使用iOS上的Objective-C代码将图像发布到LinkedIn帐户?

时间:2022-09-07 09:38:18

I am able to post the comment to LinkedIn but not able to post the image. This is the code for posting the comment:

我可以将评论发布到LinkedIn,但无法发布图片。这是发布评论的代码:

  NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
  OAMutableURLRequest *request = 
        [[OAMutableURLRequest alloc] initWithURL:url
                                        consumer:self.consumer
                                           token:self.accessToken
                                        callback:nil
                               signatureProvider:nil];
  NSString *postedStr = self.textView.text;
  NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
                                [[NSDictionary alloc] 
                                 initWithObjectsAndKeys:
                                 @"anyone",@"code",nil], @"visibility", 
                                postedStr, @"comment", nil];
  [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  NSString *updateString = [update JSONString];
  [request setHTTPBodyWithString:updateString];
  [request setHTTPMethod:@"POST"];
  OADataFetcher *fetcher = [[OADataFetcher alloc] init];
  [fetcher fetchDataWithRequest:request
                        delegate:self
               didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
                 didFailSelector:@selector(postUpdateApiCallResult:didFail:)];    
    // [self.view addSubview:linkedinView];
   [request release];

Any suggestion really appreciated!

任何建议真的很感激!

2 个解决方案

#1


4  

try this bellow code may be its help you:-

试试这个波纹管代码可能会帮助你: -

-(void)postUpdateHERE
{
  NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
  OAMutableURLRequest *request =
  [[OAMutableURLRequest alloc] initWithURL:url
                            consumer:[self getConsumer]
                               token:self.accesstoken
                            callback:nil
                   signatureProvider:nil];

  NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:

                    [[NSDictionary alloc]
                     initWithObjectsAndKeys:
                     @"anyone",@"code",nil], @"visibility",

                    @"comment goes here", @"comment",
                    [[NSDictionary alloc]
                     initWithObjectsAndKeys:
                    @"description goes here",@"description",
                    @"www.google.com",@"submittedUrl",
                      @"title goes here",@"title",
                    @"http://economy.blog.ocregister.com/files/2009/01/linkedin-logo.jpg",@"submittedImageUrl",nil],@"content",
                    nil];
  [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
  [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  NSString *updateString = [update JSONString];
  [request setHTTPBodyWithString:updateString];
  [request setHTTPMethod:@"POST"];
  OADataFetcher *fetcher = [[OADataFetcher alloc] init];
  [fetcher fetchDataWithRequest:request
                 delegate:self
        didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
          didFailSelector:@selector(postUpdateApiCallResult:didFail:)];

}

i found this from bellow stackover flow answer:-

我发现这是从波纹管stackover流程回答: -

Can't share using OAuth Starter Kit for LinkedIn

无法使用OAuth Starter Kit for LinkedIn进行分享

EDIT:-

For TamilKing comment reply. you can get image URL of you currunt Location using Google API. First you need to get you currunt Location Image Like:-

对于TamilKing的评论回复。您可以使用Google API获取您的图片网址。首先,你需要得到你的currunt位置图像像: -

NSString *staticMapUrl = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?markers=color:red|%f,%f&%@&sensor=true",lat, log,@"zoom=12&size=114x116"];    //That above staticMapURl NSlog is :http://maps.google.com/maps/api/staticmap?markers=color:red|1.282130,103.803131&zoom=12&size=114x116&sensor=true

That given you current Location image for example:-

这给你当前的位置图像例如: -

如何使用iOS上的Objective-C代码将图像发布到LinkedIn帐户?

Now you have to convert this above NSString to NSURL like

现在你必须将上面的NSString转换为NSURL之类的

NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

and use this url that you want to share in to LinkedIn. hope that helps you.

并使用您想要分享到LinkedIn的这个网址。希望对你有所帮助。

#2


3  

You may need to do more, but for sure you'll need to set the length...

你可能需要做更多,但肯定你需要设置长度......

[request setValue:[NSString stringWithFormat:@"%d", updateString.length] forHTTPHeaderField:@"Content-Length"];

#1


4  

try this bellow code may be its help you:-

试试这个波纹管代码可能会帮助你: -

-(void)postUpdateHERE
{
  NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
  OAMutableURLRequest *request =
  [[OAMutableURLRequest alloc] initWithURL:url
                            consumer:[self getConsumer]
                               token:self.accesstoken
                            callback:nil
                   signatureProvider:nil];

  NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:

                    [[NSDictionary alloc]
                     initWithObjectsAndKeys:
                     @"anyone",@"code",nil], @"visibility",

                    @"comment goes here", @"comment",
                    [[NSDictionary alloc]
                     initWithObjectsAndKeys:
                    @"description goes here",@"description",
                    @"www.google.com",@"submittedUrl",
                      @"title goes here",@"title",
                    @"http://economy.blog.ocregister.com/files/2009/01/linkedin-logo.jpg",@"submittedImageUrl",nil],@"content",
                    nil];
  [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
  [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  NSString *updateString = [update JSONString];
  [request setHTTPBodyWithString:updateString];
  [request setHTTPMethod:@"POST"];
  OADataFetcher *fetcher = [[OADataFetcher alloc] init];
  [fetcher fetchDataWithRequest:request
                 delegate:self
        didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
          didFailSelector:@selector(postUpdateApiCallResult:didFail:)];

}

i found this from bellow stackover flow answer:-

我发现这是从波纹管stackover流程回答: -

Can't share using OAuth Starter Kit for LinkedIn

无法使用OAuth Starter Kit for LinkedIn进行分享

EDIT:-

For TamilKing comment reply. you can get image URL of you currunt Location using Google API. First you need to get you currunt Location Image Like:-

对于TamilKing的评论回复。您可以使用Google API获取您的图片网址。首先,你需要得到你的currunt位置图像像: -

NSString *staticMapUrl = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?markers=color:red|%f,%f&%@&sensor=true",lat, log,@"zoom=12&size=114x116"];    //That above staticMapURl NSlog is :http://maps.google.com/maps/api/staticmap?markers=color:red|1.282130,103.803131&zoom=12&size=114x116&sensor=true

That given you current Location image for example:-

这给你当前的位置图像例如: -

如何使用iOS上的Objective-C代码将图像发布到LinkedIn帐户?

Now you have to convert this above NSString to NSURL like

现在你必须将上面的NSString转换为NSURL之类的

NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

and use this url that you want to share in to LinkedIn. hope that helps you.

并使用您想要分享到LinkedIn的这个网址。希望对你有所帮助。

#2


3  

You may need to do more, but for sure you'll need to set the length...

你可能需要做更多,但肯定你需要设置长度......

[request setValue:[NSString stringWithFormat:@"%d", updateString.length] forHTTPHeaderField:@"Content-Length"];