Using Twitter API's upload_with_media with iOS SDK? - iphone

I've been looking for a Objective-C version of this PHP code sample on how to use Twitter's upload_with_media for an iPhone application.
I have been able to find one. Could you point me to ane example or explain how I could translate this code to Objective-C?
Thanks

You can Do like this for upload_with_media,
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST];
UIImage * image = [UIImage imageNamed:#"myImage.png"];
//add text
[postRequest addMultiPartData:[#"I just found the secret level!" dataUsingEncoding:NSUTF8StringEncoding] withName:#"status" type:#"multipart/form-data"];
//add image
[postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:#"media" type:#"multipart/form-data"];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
[self performSelectorOnMainThread:#selector(displayText:) withObject:output waitUntilDone:NO];
}];

Related

how to invite facebook friend in ios7?

am new in ios development
when i click "find your facebook friend" button then invitaion goes to this my salected friend
how can i code for this.am using facebook sdk framework.
NSLog(#"ID=%#",[_IDArray lastObject]);
NSLog(#"Event ID:%#",_eventID);
NSData *data = [_eventID dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"Event ID:%#",[json objectForKey:#"id"]);
NSURL *meurl = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/invited/%#",[json objectForKey:#"id"],[_IDArray lastObject]]];
//#"https://graph.facebook.com/EVENT_ID/invited?users=USER_ID1,USER_ID2,USER_ID3"
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:meurl
parameters:nil];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Data=%#",meDataString);
}];
}
}
but i want to use facebook sdk
please help out
Facebook have shared the way to send request and invitation as:Facebook tutorial friend requests

Quering Facebook notifications using Social framework in iOS 6

I've looked around the Social Framework, but it's difficult to navigate. I figured out how to get the latest #mentions on twitter, which I do like so:
NSURL *mentionsURL = [NSURL URLWithString:#"http://api.twitter.com/1.1/statuses/mentions_timeline.json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:#"3" forKey:#"count"];
[parameters setObject:#"0" forKey:#"include_entities"];
SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:mentionsURL
parameters:parameters];
[twitterInfoRequest setAccount:twitterAccount];
[twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
}];
I was wanting to adapt this to get the latest notifications from Facebook, but I'm unsure what to put in the URL and parameters to make this work.

Iphone - AFNetworking Login Form always returns 404

I'm having troubles with AFNetworking, decided to use it since ASIHTTP is long deprecated, and I'm trying to use it in a login form request, but the response code is always -1011.
here is my code :
NSString *urltest = [NSString stringWithFormat:#"%#%#/", SERVER_ADDRESS, API_ADDRESS];
NSURL *url = [NSURL URLWithString:urltest];
NSLog(#"url address : %#",url);
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.allowsInvalidSSLCertificate = TRUE;
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
_email.text, #"email",
_password.text, #"password",
nil];
// Here I try doing my login request using AFHTTPRequestOperation
NSMutableURLRequest *request = [httpClient requestWithMethod:#"POST" path:#"/auth/login" parameters:params];
[request setValue:#"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:#"Content-Type"];
//Notice the different method here!
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Response: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(#"Error: %#", error);
}];
//Enqueue it instead of just starting it.
[httpClient enqueueHTTPRequestOperation:operation];
// Here I try doing my login request using the postPath:parameters: method
[httpClient postPath:#"/auth/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self performSegueWithIdentifier:#"AuthOK" sender:self];
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"Request Successful, response '%#'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Request failed with code %d for %# for request : %#",error.code , error.localizedDescription,httpClient.baseURL);
}];
And none of these two methods are working, I'm guessing I'm doing something wrong somewhere
btw, the server I login to, has an unsigned ssl certificate, but I handled the error by adding
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 1
in AFURLConnectionOperation.h
I use Charles to monitor http requests, and it gives me a "SSLHandshake: Remote host closed connection during handshake"
My app runs with iOS 5.
Anyone has insights on this?
Leo
Well my solution was to initialize my request with an empty url and then in the postPath, put the whole address.
You need to install Charles' SSL Certificate on your device to decrypt SSL traffic
http://www.charlesproxy.com/documentation/faqs/ssl-connections-from-within-iphone-applications/

How to synchronize variables with TWRequest performRequestWithHandler in iOS5?

I really couldn't figure out how to deal with iOS5 Twitter API TWRequest performRequestWithHandler. I declared an instance variable of NSMutableArray *parsedTimeLine in my .h file and a method signature of -(void) fetchWebData: (NSString *) screenName;
The said method implementation is here:
-(void) fetchWebData: (NSString *) screenName {
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:screenName forKey:#"screen_name"];
[params setObject:#"f" forKey:#"include_entities"];
[params setObject:#"f" forKey:#"include_rts"];
[params setObject:#"10" forKey:#"count"];
[params setObject:#"t" forKey:#"trim_user"];
[params setObject:#"f" forKey:#"contributor_details"];
[params setObject:#"t" forKey:#"exclude_replies"];
NSURL *url = [NSURL URLWithString:#"http://api.twitter.com/1/statuses/user_timeline.json"];
TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodGET];
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
NSError *jsonError;
parsedUserTimeLine =[NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableLeaves
error:&jsonError];
NSLog(#"jsonArray result count inside request: %d",parsedUserTimeLine.count);
NSLog(#"jsonArray values inside request: %#", parsedUserTimeLine);
}
}];
NSLog(#"jsonArray result count outside request: %d",parsedUserTimeLine.count);
NSLog(#"jsonArray values outside request: %#", parsedUserTimeLine);
}
This works fine, but there's a weird thing I really couldn't figure out. My 2 NSLogs INSIDE the request method shows the accurate count value and array contents of the parsedUSerTimeline. But my 2 NSLogs OUTSIDE THE REQUEST method shows
-jsonArray result count outside request: 0
-jsonArray values outside request: (null)
also, I checked the value of parsedUserTimeLine at my viewDidLoad after calling the fetchWebData method shows count = 0 and value = null.
Kindly help me with this guys. I need to access the value of parsedUserTimeLine globally. Thanks in advance.
I don't know the Twitter-Framework, but from the code you've posted i can tell you that this is going to perform asynchron. The NSLogs are both valid. The inner NSLogs are called when the request has finished, the outer are called even if the request has not been started.
Simply call a method to inform the others if the request has been finished.
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
NSError *jsonError;
NSDictionary *parsedUserTimeLine =[NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableLeaves
error:&jsonError];
NSLog(#"jsonArray result count inside request: %d",parsedUserTimeLine.count);
NSLog(#"jsonArray values inside request: %#", parsedUserTimeLine);
// The request will be perform on another thread, so call the method on the main
// thread to avoid crossing operations
[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self requestHasFinishedWithData:parsedUserTimeLine];
}]
}
}];

How to get the most updated tweet?

I am trying to post a tweet and then getting the latest tweets from the timeline and get the url of the image i just posted.
But somehow its not showing me the recent one. This is the code i am using:
-(void)shareButtonClicked:(id)sender
{
if ([TWTweetComposeViewController canSendTweet])
{
// Create account store, followed by a twitter account identifier
// At this point, twitter is the only account type available
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
// Did user allow us access?
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
// Sanity check
if ([arrayOfAccounts count] > 0)
{
// Keep it simple, use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
// Build a twitter request
UIImage * image = [UIImage imageNamed:#"welcomescreen-header.png"];
NSString * status = #"This is welcome screen.";
NSString * completeHandle = [NSString stringWithFormat:#"%##%#",status,twitterHandle.text];
NSData * data=[completeHandle dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#"Text:%#",completeHandle);
NSData * imageData = (UIImageJPEGRepresentation(photo.image, 90));
TWRequest *postRequest = [[TWRequest alloc] initWithURL:
[NSURL URLWithString:#"https://upload.twitter.com/1/statuses/update_with_media.json"]
parameters:nil requestMethod:TWRequestMethodPOST];
[postRequest addMultiPartData:imageData withName:#"media" type:#"image/jpeg"];
[postRequest addMultiPartData:data withName:#"status" type:#"text"];
// Post the request
[postRequest setAccount:acct];
// Block handler to manage the response
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(#"Twitter Response, HTTP response: %i", [urlResponse statusCode]);
if ([urlResponse statusCode] == 200) {
[self getTheUserTimeLine];
}
}];
}
}
}];
}
}
-(void)getTheUserTimeLine
{
TWRequest *postRequest = [[TWRequest alloc] initWithURL:
[NSURL URLWithString:#"https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=ashu1702&count=1"]
parameters:nil requestMethod:TWRequestMethodGET];
// Block handler to manage the response
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ([urlResponse statusCode] == 200)
{
// The response from Twitter is in JSON format
// Move the response into a dictionary and print
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
NSLog(#"Twitter response: %#", [dict description]);
}
else
NSLog(#"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];
}
Try using http://twitter.com/statuses/user_timeline/ashu1702.json?count=1 in your browser and see what happens. I misread your code earlier but trying this now it seems okay to me.