What could this error mean?
[__NSCFNumber length]: unrecognized selector sent to instance 0x6d21350
Here is my code:
NSString *urlString = #"http://api.twitter.com/1/statuses/update.json";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:status forKey:#"status"];
[params setObject:replyToID forKey:#"in_reply_to_status_id"];
[params setObject:#"1" forKey:#"include_entities"];
// Build the request with our parameter
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodPOST];
// Attach the account object to this request
[request setAccount:twitterAccount];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!responseData) {
// inspect the contents of error
NSLog(#"%#", [error localizedDescription]);
self.alert = [[UIAlertView alloc] initWithTitle:#"HTTP error" message:#"I could not connect to the Twitter API." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[self.alert show];
[self.replyDelegate replyRequestSuccessful:NO];
}
else {
/*NSString *responseDataAsString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(responseDataAsString);*/
NSError *error;
NSArray *replyResponse = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
if (!replyResponse) {
NSLog(#"%#", [error localizedDescription]);
self.alert = [[UIAlertView alloc] initWithTitle:#"JSON error" message:#"I could not parse the JSON response from the Twitter API." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[self.alert show];
[self.replyDelegate replyRequestSuccessful:NO];
}
else {
[self.replyDelegate replyRequestSuccessful:YES];
}
}
}];
I tried debuggin, and it dies once it enters the performRequestWithHandler. It goes the else block and dies with the error above.
It means that you are passing an NSNumber where the called code expects an NSString or some other object that has a length method. You can tell Xcode to break on exceptions so that you see where exactly the length method gets called.
Specifically this line of code:
[params setObject:replyToID forKey:#"in_reply_to_status_id"];
the replyToID is not a String or has a method length. If you don't have this, or if you convert the replyToID to a String before setting it in params it should work.
Related
So I'm working on creating a login page. But I'm not sure what part I'm missing here! Every time I try to login it says the credentials are invalid.... I'm betting I got confused with POST and GET methods.
Any help would be appreciated! Thanks.
My Code is below:
- (IBAction)login:(id)sender {
if ([userName.text isEqualToString:#""] || [password.text isEqualToString:#""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oops!" message:#"Please fill in all the fields!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
NSURL *url = [NSURL URLWithString:#"http://WEBSITEHERE/api/users/AuthenticateUser"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSDictionary *params = #{#"userName": userName.text, #"password": password.text};
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
NSLog(#"PARAMS = %#", params);
[request setHTTPMethod:#"GET"];
[request setValue:#"text/plain" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json;charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:data];
NSURLResponse *response = nil;
NSData *dataURL = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *result = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSString *responseString = [[NSString alloc]initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(#"RESULT = %#", responseString);
if ([result isEqualToString:#"1"])
{
UIStoryboard *mainStoryboard=[UIStoryboard
storyboardWithName:#"MainStoryboard" bundle:nil];
Home *mainView=[mainStoryboard
instantiateViewControllerWithIdentifier:#"mainView"];
mainView.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentViewController:mainView animated:YES completion:nil];
}else
{
// invalid information
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oops!" message:#"You must have entered something wrong! Try again!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
I'm not sure how your server is setup but this is how I setup a request for authentication in one of my recent apps:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#", kBaseServerURL, sAuthMethod]]];
[request setHTTPMethod:#"GET"];
[request setValue:_username forHTTPHeaderField:#"j_username"];
[request setValue:_password forHTTPHeaderField:#"j_password"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
Can somebody help me with this? I am using an NSURL connection for GET and POST methods. The reddit api is found here: http://www.reddit.com/dev/api#POST_api_subscribe
I am specifically confused with what the ('sr', 'sr_name') parameter is. Is it an array of some sort? Or do I have to pass either of 'sr' or 'sr_name'?
My current code:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.reddit.com/api/subscribe"]]];
[request setHTTPMethod:#"POST"];
request.HTTPBody = [[NSString stringWithFormat:#"api_type=json&action=%#&uh=%#&sr=%#", #"sub", modhashString, SubredditTitle] dataUsingEncoding:NSASCIIStringEncoding];
NSURLResponse *response;
NSError *error;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:POSTReply
options:NSJSONReadingMutableContainers
error:nil];
NSLog(#"%#", json);
NSMutableArray *temp = [[json objectForKey:#"json"] objectForKey:#"errors"];
if ([temp count] > 0){
for (int i = 0; i<[temp count]; i++) {
NSArray *temp2 = [[NSArray alloc] init];
temp2 = [temp objectAtIndex:i];
NSLog(#"Contains errors %#", temp2);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[temp2 objectAtIndex:1] message:nil delegate:self cancelButtonTitle:#":/" otherButtonTitles:nil, nil];
[alert show];
}
}
else{
}
I'm creating an application that has integration with Twitter .. and wanted to know what have to do to create a button to add an image of the device in the tweet.and also wanted to know which framework to use it and also where the code because my app should be compatible with ios4 too.
First of all is it possible to send image on twitter before ios5?
Yes you can upload image to twitter.
Have a look at this blog article on integrating twitter into your applications; and
This one about adding twitpic to your application to upload images
use sharekit. Refer ShareKit link.
You can use Oauth for this sending.it is compatible with ios4.acct123 is Oauth account
- (IBAction)composeTweet:(id)sender
{
// Build a twitter request
NSString *resultprofileUrl= [NSString stringWithFormat:#"https://api.twitter.com/1/statuses/update.json"];
tweets = [NSMutableArray array];
// Posting image and text to twitpic............
ASIFormDataRequest *req = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:#"http://api.twitpic.com/2/upload.json"]];
[req addRequestHeader:#"X-Auth-Service-Provider" value:#"https://api.twitter.com/1/account/verify_credentials.json"];
[req addRequestHeader:#"X-Verify-Credentials-Authorization"
value:[acct123 oAuthHeaderForMethod:#"GET"
andUrl:#"https://api.twitter.com/1/account/verify_credentials.json"
andParams:nil]];
[req setData:UIImageJPEGRepresentation(imageview3.image, 0.8) forKey:#"media"];
[req setPostValue:#"YourKey" forKey:#"key"];
[req setPostValue:tweetPost.text forKey:#"message"];
[req startSynchronous];
NSLog(#"Got HTTP status code from TwitPic: %d", [req responseStatusCode]);
NSLog(#"Response string: %#", [req responseString]);
NSDictionary *twitpicResponse = [[req responseString] JSONValue];
NSLog(#"image url: %#", twitpicResponse);
url1 = [twitpicResponse valueForKey:#"url"];
NSLog(#"url=%#",url1);
if (url1 == nil) {
url1 = #"";
}
[req release];
NSString *resultUrl = [[NSString alloc]init];
if(reply_tweet_flag != 1)
{
resultUrl = [NSString stringWithFormat:#"%# %#",tweetPost.text,url1];
}
else
{
resultUrl= [NSString stringWithFormat:#"%#",tweetPost.text];
}
NSLog(#"URL>>>>%#",resultUrl);
NSString *postUrl = #"https://api.twitter.com/1/statuses/update.json";
ASIFormDataRequest *request = [[ASIFormDataRequest alloc]
initWithURL:[NSURL URLWithString:postUrl]];
NSMutableDictionary *postInfo = [NSMutableDictionary
dictionaryWithObject:resultUrl
forKey:#"status"];
for (NSString *key in [postInfo allKeys]) {
[request setPostValue:[postInfo objectForKey:key] forKey:key];
}
[request addRequestHeader:#"Authorization"
value:[acct123 oAuthHeaderForMethod:#"POST"
andUrl:postUrl
andParams:postInfo]];
[request startSynchronous];
NSLog(#"Status posted. HTTP result code: %d", request.responseStatusCode);
if (reply_tweet_flag == 1) {
reply_tweet_flag = 0;
[self refresh];
}
if([tweetPost.text isEqualToString:#""])
{
UIAlertView *message1 = [[UIAlertView alloc] initWithTitle:#""
message:#"Check the message"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[message1 show];
message1.tag=1;
}
else{
[tweetPost setText:#""];
imageview3.image = [UIImage imageNamed:#""];
UIAlertView *message1 = [[UIAlertView alloc] initWithTitle:#""
message:#"Post was send succesfully"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[message1 show];
message1.tag=1;
}
}
I want to know that is there any way I can post picture in twitter with some text, some one has suggested to use "http://tinyurl.com/".I don't know where to start, in my previous application I twit successfully but that only contains text.
A proper direction to proceed would be a great help.
There are a couple of ways. I would suggest you use ShareKit. It does most of the work for you.
To post image to twitter we have to first post the image to twit pick and then we have to send that url to twitter that is the process to send image to twitter this is the sample code to send image to twitter..........
NSString *postUrl = #"https://api.twitter.com/1/statuses/update.json";
ASIFormDataRequest *request = [[ASIFormDataRequest alloc]
initWithURL:[NSURL URLWithString:postUrl]];
NSMutableDictionary *postInfo = [NSMutableDictionary
dictionaryWithObject:statusText.text
forKey:#"status"];
NSLog(#"%#",postInfo);
NSString *str1= statusText.text;
NSLog(#"Status posted. HTTP result code: %d", request.responseStatusCode);
[request release];
[statusText resignFirstResponder];
ASIFormDataRequest *req = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:#"http://api.twitpic.com/2/upload.json"]];
[req addRequestHeader:#"X-Auth-Service-Provider" value:#"https://api.twitter.com/1/account/verify_credentials.json"];
[req addRequestHeader:#"X-Verify-Credentials-Authorization"
value:[oAuth oAuthHeaderForMethod:#"GET"
andUrl:#"https://api.twitter.com/1/account/verify_credentials.json"
andParams:nil]];
[req setData:UIImageJPEGRepresentation(imageView.image, 0.8) forKey:#"media"];
// Define this somewhere or replace with your own key inline right here.
[req setPostValue:#"74734e805f2ad85afae441ca12c16087" forKey:#"key"];
[req startSynchronous];
NSLog(#"Got HTTP status code from TwitPic: %d", [req responseStatusCode]);
NSLog(#"Response string: %#", [req responseString]);
NSDictionary *twitpicResponse = [[req responseString] JSONValue];
NSLog(#"%#",[[req responseString] JSONValue]);
textView.text = [NSString stringWithFormat:#"Posted image URL: %#", [twitpicResponse valueForKey:#"url"]];
NSString *str=[NSString stringWithFormat:#" %#",[twitpicResponse valueForKey:#"url"]];
NSLog(#"%#",str);
if([str isEqualToString:#" (null)"])
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Message" message:#"Could not authenticate you(header rejected by twitter)" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
}
else
{
NSString *postUrl = #"https://api.twitter.com/1/statuses/update.json";
ASIFormDataRequest *request = [[ASIFormDataRequest alloc]
initWithURL:[NSURL URLWithString:postUrl]];
NSString *txtmessage=[str1 stringByAppendingString:str];
/*NSMutableDictionary *postInfo = [NSMutableDictionary
dictionaryWithObject:[twitpicResponse valueForKey:#"url"]
forKey:#"status"];*/
NSMutableDictionary *postInfo = [NSMutableDictionary
dictionaryWithObject:txtmessage
forKey:#"status"];
for (NSString *key in [postInfo allKeys]) {
[request setPostValue:[postInfo objectForKey:key] forKey:key];
}
[request addRequestHeader:#"Authorization"
value:[oAuth oAuthHeaderForMethod:#"POST"
andUrl:postUrl
andParams:postInfo]];
[request startSynchronous];
if(request.responseStatusCode!=200)
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Message" message:#"Already posted" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Message" message:#"sucess" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
}
NSLog(#"Status posted. HTTP result code: %d", request.responseStatusCode);
statusText.text = #"";
[request release];
[statusText resignFirstResponder];
}
Trying to alert user when internet is unavailable (and retry when they dismiss message). The screen slightly dims (in preparation for alert), but the alert never displays.
Is the while loop interfering with the alert?
-(NSArray*)getResponse:(NSString*)page {
NSError *error;
NSURLResponse *response;
NSData *dataReply;
NSString *stringReply;
NSString *legalAddressURL;
NSArray *separatedData;
legalAddressURL = [NSString stringWithFormat:#"%#%#", SERVER,
[page stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: legalAddressURL]];
[request setHTTPMethod: #"GET"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
while ([error code]){
if (isNetAvailable){
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Internet Connection"
message:#"Server is down" delegate:self cancelButtonTitle:#"Try again"
otherButtonTitles:nil] autorelease];
[alert show];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
} else {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Internet Connection"
message:#"No access to net" delegate:self cancelButtonTitle:#"Try again"
otherButtonTitles:nil] autorelease];
[alert show];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
}
}
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
separatedData = [stringReply componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#","]];
return separatedData;
}
After showing the alert using [alert show], you should exit the getResponse method, because the internal message loop should continue.
You can't simply continue looping and showing alerts.
I could be wrong, but I'm pretty sure that's the reason.