LinkedIn starter kit not sharing content to wall If add content dictionary? - iphone

I have written code to post content to linkedIn wall and it is working fine if I remove content dictionary from update dictionary. See the below code containing post data:
- (IBAction)postUpdate
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/shares"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
NSDictionary *content=[[NSDictionary alloc] initWithObjectsAndKeys:#"http://www.celebs101.com/wallpapers/Bruce_Lee/421101/Bruce_Lee_Wallpaper.jpg",#"submitted-image-url",#"http://www.youtube.com/watch?v=GoZ2Be2zLq8",#"submitted-url",#"Post Image and Video testing",#"title",#"Posted Description",#"description",nil];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc]
initWithObjectsAndKeys:
#"anyone",#"code",nil], #"visibility",
#"Test posting to linkedIn", #"comment",content,#"content",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:)];
}
Post data comes in didfinishSelector is :
data after posting is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
<status>401</status>
<timestamp>1356601471318</timestamp>
<request-id>MFW58DCKE9</request-id>
<error-code>0</error-code>
<message>[unauthorized]. OAU:0onill9cburx|3c05c306-aad8-4d07-a2a1-2430aa21b54a|*01|*01:1356601465:Ji7pimMqrXp3RHCNJLv8iKZsklk=</message>
</error>
I don't know why so? please help..
Thanks in Advance..

I have solved it
working code to post on LinkedIn as follows:
- (IBAction)postUpdate
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/shares"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
NSMutableDictionary *contentDic=[[NSMutableDictionary alloc] init];
[contentDic setValue:#"http://www.celebs101.com/wallpapers/Bruce_Lee/421101/Bruce_Lee_Wallpaper.jpg" forKey:#"submitted-image-url"];
[contentDic setValue:#"http://www.linkedin.com" forKey:#"submitted-url"];
[contentDic setValue:[NSString stringWithFormat:#"A title for your share"] forKey:#"title"];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc]
initWithObjectsAndKeys:
#"anyone",#"code",nil], #"visibility",
#"Test posting to linkedIn",#"comment",contentDic,#"content",nil];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSString *updateString = [update JSONString];
NSLog(#"json string is %#",updateString);
[request setHTTPBodyWithString:updateString];
[request setHTTPMethod:#"POST"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(postUpdateApiCallResult:didFinish:)
didFailSelector:#selector(postUpdateApiCallResult:didFail:)];
}
- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data after posting is %#",myString);
}
- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(#"%#",[error description]);
}

If you are getting that type of error:
So it may be because of post url in "submitted-url" key.
So an easy way to do this first make the post_url for "submitted-url" short using this link( http://tinyurl.com/api-create.php?url=post_url). And then use this short post_url for "submitted-url" value. This can be a solution of your problem.
Sometimes long url for "submitted-url" key cause a that kind of problem.

Related

How to fetch LinkedIn connections/Friends/Contacts in iOS SDK?

I want to fetch all my friends from my LinkedIn profile. Please suggest me any tutorial.
My code is :
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/connections:(id,first-name,last-name,email-address,picture-url,Positions)"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:self.oAuthLoginView.consumer
token:self.oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(profileApiCallResult:didFinish:)
didFailSelector:#selector(profileApiCallResult:didFail:)];
You can use this for connections:For request part see this.
- (void)connectionsApiCallResult:(LOAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(#"connectionsApiCallResult====%#",responseBody);
[responseBody release];
}
- (void)connectionsApiCallResult:(LOAServiceTicket *)ticket didFail:(NSError *)error
{
NSLog(#"%#",[error description]);
}
-(void)GetConnectionsCall
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/connections:(headline,first-name,last-name,picture-url,id)"];
LOAMutableURLRequest *request =
[[LOAMutableURLRequest alloc] initWithURL:url
consumer:self.consumer
token:TestAccessToken
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
LOADataFetcher *fetcher = [[[LOADataFetcher alloc] init] autorelease];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(requestTokenResult:didFinish:)
didFailSelector:#selector(requestTokenResult:didFail:)];
}
Please try the following steps
first go to https://www.linkedin.com/secure/developer edit your App scope , make sure you have checked the r_network scope .
Then go to OAuthLoginView of you project , then go to -(void) requestTokenFromProvider method .
edit you scope in below lines .
OARequestParameter *nameParam = [[OARequestParameter alloc] initWithName:#"scope"
value:#"r_fullprofile+rw_nus+r_network"];
OARequestParameter * scopeParameter=[OARequestParameter requestParameter:#"scope"
value:#"r_fullprofile rw_nus r_network"];

LinkedIn Integration in iOS: Invitation API not working

I'm trying to integrate LinkedIn in my iOS application, where I need to Authenticate, then fetch profile details and post to LinkedIn from my app. I do all these things quite successfully using this sample project LinkedIn OAuth Sample Client as reference, I did some modifications and everything is working quite well. And I use the details from
LinnkedIn API as well to implement this.
Next, I need to send invitation to connect in LinkedIn from the same app. I tried using Invitataion API .
Here is my code snippet:
-(void)sendInvitationToconnectTo:(NSString *)emailAddress withMessage:(NSString *)text
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:[[OAHMAC_SHA1SignatureProvider alloc] init]];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSDictionary *temp=[[NSDictionary alloc]initWithObjectsAndKeys:#"/people/email=userName#gmail.com",#"_path",#"userName1",#"first-name",#"userName2",#"last-name", nil];
NSDictionary *temp2=[[NSDictionary alloc] initWithObjectsAndKeys:temp,#"person",nil];
NSArray *arr2=[[NSArray alloc]initWithObjects:temp2, nil];
NSDictionary *value=[[NSDictionary alloc]initWithObjectsAndKeys:arr2,#"values", nil];
NSDictionary *dict3=[[NSDictionary alloc]initWithObjectsAndKeys:#"friend",#"connect-type",nil];
NSDictionary *dict4=[[NSDictionary alloc] initWithObjectsAndKeys:dict3,#"invitation-request", nil];
NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:dict4,#"item-content",#"Say yes!",#"body",#"Invitation to connect.",#"subject",value,#"recipients", nil];
NSString *updateString = [dict JSONString];
NSLog(#"updateString : %#",updateString);
[request setHTTPBodyWithString:updateString];
[request setHTTPMethod:#"POST"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(sendRequestApiCallResult:didFinish:)
didFailSelector:#selector(sendRequestApiCallResult:didFail:)];
[request release];
}
- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSLog(#"invitation sent");
[self networkApiCall];
}
- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(#"%#",[error description]);
}
And my json string looks like this:
{"body":"Say yes!","subject":"Invitation to
connect.","recipients":{"values":[{"person":{"first-name":"userName1","last-name":"userName2","_path":"/people/email=userName#gmail.com"}}]},"item-content":{"invitation-request":{"connect-type":"friend"}}}
While sending the invitation I don't get any errors and when I debug, the sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data method gets called showing invitation sent text.
But ths issue is the target LinkedIn user didn't get any request!
Can anyone help me regarding this? Is there anything I'm doing wrong in my code?
NB I went through both the below mentioned links to get a solution, tried the solutions , but in vain.
1.Invitation API - Stumped by 401 (iOS and OAuthConsumer) - SOLVED
2.linkedin connect API gives error 401 - iphone
and the NSURLResponse statuscode I get is 401 !
EDIT:: Solved it myself. See my answer below.
Glad to inform all that after long 4 days of continuos research, I got this problem solved!
I got the solution from this Linkedin thread
You just need to make a few changes in the code:
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setHTTPMethod:#"POST"];
NSString *messageToPerson = #"/people/email=target#gmail.com";
NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:messageToPerson,#"_path",#"TargetFirstName",#"first-name",#"TargetSecondName",#"last-name",nil] autorelease], #"person",nil];
NSArray *valueArray = [[NSArray alloc] initWithObjects:person,nil];
NSDictionary *values = [[NSDictionary alloc] initWithObjectsAndKeys:valueArray,#"values", nil];
NSDictionary *ir = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:#"friend",#"connect-type",nil] autorelease], #"invitation-request",nil];
NSDictionary *ic = [[NSDictionary alloc] initWithObjectsAndKeys:ir,#"item-content", nil];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:values,#"recipients",#"Invitation",#"subject",#"ConnectWithMe",#"body", ir, #"item-content", nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSLog(#"%#",[update description]);
NSString *updateString = [update JSONString];
[request prepare];
[request setHTTPBodyWithString:updateString];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request delegate:self
 didFinishSelector:#selector(postUpdateApiCallResult:didFinish:)
 didFailSelector:#selector(postUpdateApiCallResult:didFail:) withId:1];
[request release];
and
in oAdata OADataFetcher.m
- (void)fetchDataWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector withId:(int)idtm
 {

 [request release];
request = [aRequest retain];

 delegate = aDelegate;
didFinishSelector = finishSelector;

 didFailSelector = failSelector;
//note this change

 if (idtm!=1) {

 [request prepare];
}
connection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];
}
Try this. Will work for sure. Hope this will help future visitors.
Regards

How to post an image to a LinkedIn account using Objective-C code on iOS?

I am able to post the comment to LinkedIn but not able to post the image. This is the code for posting the comment:
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!
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:-
Can't share using 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:-
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:-
Now you have to convert this above NSString to NSURL like
NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
and use this url that you want to share in to LinkedIn. hope that helps you.
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"];

LinkedIn Invitation API error 401 [unauthorized]-iPhone sdk

I am using OAuth-Sample-Client code to send invitation from email address via Linked Invitation API.
I have created the same body as described in developer site,but right now I am getting error of 401,I have searched a lot of on it but didn't get any solution which get me out of it from any site or forum.
My response is as below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
<status>401</status>
<timestamp>1351159386437</timestamp>
<request-id>YX4NRU3S7J</request-id>
<error-code>0</error-code>
<message>[unauthorized]. OAU:ron8e8nko3te|e0a96317-dfdc-44fb-b427-5655e02f97ed|*01|*01:1351159294:dRCDN6b3K9F/mwWAaByKZQPgeVw=</message>
</error>
Here my json string would be like this:
{"body":"Say yes!","subject":"Invitation to
connect.","recipients":{"values":[{"person":{"first-name":"test","last-name":"test","_path":"/people/email=test#test.com"}}]},"item-content":{"invitation-request":{"connect-type":"friend"}}}
I have used the code as below to send Invitation.
- (IBAction)postButton_TouchUp:(UIButton *)sender
{
[statusTextView resignFirstResponder];
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:[[OAHMAC_SHA1SignatureProvider alloc] init]];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSDictionary *temp=[[NSDictionary alloc]initWithObjectsAndKeys:#"/people/email=test#test.com",#"_path",#"test",#"first-name",#"test",#"last-name", nil];
NSDictionary *temp2=[[NSDictionary alloc] initWithObjectsAndKeys:temp,#"person",nil];
NSArray *arr2=[[NSArray alloc]initWithObjects:temp2, nil];
NSDictionary *value=[[NSDictionary alloc]initWithObjectsAndKeys:arr2,#"values", nil];
NSDictionary *dict3=[[NSDictionary alloc]initWithObjectsAndKeys:#"friend",#"connect-type",nil];
NSDictionary *dict4=[[NSDictionary alloc] initWithObjectsAndKeys:dict3,#"invitation-request", nil];
NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:dict4,#"item-content",#"Say yes!",#"body",#"Invitation to connect.",#"subject",value,#"recipients", nil];
NSLog(#"%#",[dict description]);
NSString *updateString = [dict 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:)];
[request release];
}
I got stuck at this point,Please get me out of this.
-Thanks in advance
call prepare method of OADataFetcher manually. Just put it before setting the body of the request.
[request prepare];
[request setHTTPBodyWithString:updateString];
This is working for me.
Also remove the prepare method from the OADataFetcher when you are working with Invitation API.
Apple's code works! Just one more thing, dont forget any header. I was still getting the same message, turns out I forgot to add x-li-format and Content-type headers.
I got the solution from this Linkedin thread
You just need to make a few changes in the code:
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setHTTPMethod:#"POST"];
NSString *messageToPerson = #"/people/email=target#gmail.com";
NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:messageToPerson,#"_path",#"TargetFirstName",#"first-name",#"TargetSecondName",#"last-name",nil] autorelease], #"person",nil];
NSArray *valueArray = [[NSArray alloc] initWithObjects:person,nil];
NSDictionary *values = [[NSDictionary alloc] initWithObjectsAndKeys:valueArray,#"values", nil];
NSDictionary *ir = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:#"friend",#"connect-type",nil] autorelease], #"invitation-request",nil];
NSDictionary *ic = [[NSDictionary alloc] initWithObjectsAndKeys:ir,#"item-content", nil];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:values,#"recipients",#"Invitation",#"subject",#"ConnectWithMe",#"body", ir, #"item-content", nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSLog(#"%#",[update description]);
NSString *updateString = [update JSONString];
[request prepare];
[request setHTTPBodyWithString:updateString];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request delegate:self
 didFinishSelector:#selector(postUpdateApiCallResult:didFinish:)
 didFailSelector:#selector(postUpdateApiCallResult:didFail:) withId:1];
[request release];
and in oAdata OADataFetcher.m
- (void)fetchDataWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector withId:(int)idtm
 {

 [request release];
request = [aRequest retain];

 delegate = aDelegate;
didFinishSelector = finishSelector;

 didFailSelector = failSelector;
//note this change

 if (idtm!=1) {

 [request prepare];
}
connection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];
}
Try this. Will work for sure. Hope this will help future visitors.
Regards

Capturing PHP Response Through NSJSONSerialization

Hi I have this code here.
NSString *name = [[NSString alloc] initWithFormat:[nameField text]];
NSString *street = [[NSString alloc] initWithFormat:[streetField text]];
NSString *city = [[NSString alloc] initWithFormat:[cityField text]];
NSString *state = [[NSString alloc] initWithFormat:[stateField text]];
NSString *zip = [[NSString alloc] initWithFormat:[zipField text]];
NSString *urlToAuthPage = [[NSString alloc] initWithFormat:#"&name=%#&street=%#&city=%#&state=%#&zip=%#&lat=%#&lon=%#", name, street, city, state, zip, str1, str2];
NSData *postData = [urlToAuthPage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:#"%d",[urlToAuthPage length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://yourlink.com/poop.php"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
NSError *error;
NSMutableArray *infoArray = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:postData options:kNilOptions error:&error];
NSLog(#"%#", [infoArray objectAtIndex:0]);
if (conn) {
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Your party has been successfully posted" delegate:nil cancelButtonTitle:#"Close" otherButtonTitles: nil];
[successAlert show];
//NSLog(conn);
nameField.text = #"";
streetField.text = #"";
cityField.text = #"";
stateField.text = #"";
zipField.text = #"";
[nameField resignFirstResponder];
[streetField resignFirstResponder];
[cityField resignFirstResponder];
[stateField resignFirstResponder];
[zipField resignFirstResponder];
}
What I'm trying to do is get the response the server gives out that is in NSJon. I'm trying capture what is below. I've tried using an NSMutableArray to get it but it didn't work. What am I doing wrong?
{"status":1}
The problem is that NSJSONSerialization answers witha NSDictionary, not an NSMutableArray. You can't force it to be an array, since it needs a value and a key (in this case the key is "status" and the value is "1")