Twitter integration and iOS5: semantic and parsing issues - objective-c

I was using some of Apple's example code to write the Twitter integration for my app.
However, I get a whopping amount of errors (mostly being Semantic and parse errors).
How can this be solved?
-(IBAction)TWButton:(id)sender {
ACAccountStore *accountstore = [[ACAccountStore alloc] init];
//Make sure to retrive twitter accounts
ACAccountType *accountType = [accountstore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountstore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted) [{
NSArray *accountsArray = [accountstore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:[#"Tweeted from iBrowser" forKey:#"status"] requestMethod:TWRequestMethodPOST];
[postRequest setAccount:twitterAccount];
[postRequest preformRequestWithHandeler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
[self preformSelectorOnMainThread:#selector(displaytext:) withObject:output waitUntilDone:NO];
}];
}
}];
}
//Now lets see if we can actually tweet
-(void)canTweetStatus {
if ([TWTweetComposeViewController canSendTweet]) {
self.TWButton.enabled = YES
self.TWButton.alpha = 1.0f;
}else{
self.TWButton.enabled = NO
self.TWButton.alpha = 0.5f;
}
}

The first error I see is easy to get rid of.
Objective C convention is to make the first letters of each method name lower case.
Use makeKeyAndVisible in your AppDelegate.m
The other errors we'd probably need to see where (in your code) the errors are being thrown, not just what kind of errors.

Related

Twitter Streaming Endpoint not working with SLRequest object

I am trying to pull data from the Twitter Streaming API using the SLRequest class.
When I use the endpoint and parameters documented in the code below the program "hangs"
and no JSON data is printed. I am using an endpoint based on an example from the twitter dev
website https://dev.twitter.com/docs/streaming-apis/parameters#with I am requesting
tweets at a certain location.
When I use this code to query my timeline using the REST API (the code and request is included but
commented out) the program does not hang and I get a valid response.
Is there something else in the code that I need to implement to access the data using the streaming API? What additional modifications or changes need to be made?
ACAccountStore * accountStore = [[ACAccountStore alloc] init];
ACAccountType * twitterAccountType =
[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Ask the user permission to access his account
[accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == NO) {
NSLog(#"-- error: %#", [error localizedDescription]);
}
if (granted == YES){
/***************** Create request using REST API*********************
***************** This URL is functional and returns valid data *****
NSURL * url = [NSURL URLWithString:#"https://userstream.twitter.com/1.1/user.json"];
SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:#{#"screen_name": #"your_twitter_id"}];
***************************************************************/
// Create request using Streaming API Endpoint
NSURL * url = [NSURL URLWithString:#"https://stream.twitter.com/1.1/statuses/filter.json"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:#"track" forKey:#"twitter&"];
[params setObject:#"locations" forKey:#"-122.75,36.8,-121.75,37.8"];
SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:params];
NSArray * twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] == 0) {
(NSLog(#"-- no accounts available"));
} else if ([twitterAccounts count] >0){
[request setAccount:[twitterAccounts lastObject]];
NSLog([request.account description]);
NSLog(#"Twitter handler of user is %#", request.account.username);
// Execute the request
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSError * jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// NSLog(#"-- json Data is %#", json);
NSLog([json description]);
}];
}];
}
}
}];
SLRequest doesn't play well with the streaming API.
Here is how to do with STTwitter:
self.twitter = [STTwitterAPI twitterAPIOSWithAccount:account];
[_twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
NSLog(#"-- access granted for %#", username);
[_twitter postStatusesFilterUserIDs:nil
keywordsToTrack:#[#"twitter"]
locationBoundingBoxes:#[#"-122.75,36.8,-121.75,37.8"]
delimited:nil
stallWarnings:nil
progressBlock:^(id response) {
NSLog(#"-- %#", response);
} stallWarningBlock:^(NSString *code, NSString *message, NSUInteger percentFull) {
NSLog(#"-- stall warning");
} errorBlock:^(NSError *error) {
NSLog(#"-- %#", [error localizedDescription]);
}];
} errorBlock:^(NSError *error) {
NSLog(#"-- %#", [error localizedDescription]);
}];
Internally, STTwitter builds a NSURLConnection instance with the request from -[SLRequest preparedURLRequest]. You can replicate this trick in your code if you wish.

SLRequest gives Parse Error: Expected ]

I'm writing a Facebook Parser for iOS6. Yesterday, I made a test project with only the parser. This worked fine. When I want to implement the parser in my bigger project (with exactly the same method code), a "Parse Error: Expected ]" is given. The imports for Social and Accounts are set.
This is the line that gives the error:
SLRequest *retrieveWall = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:newsFeed parameters:nil];
This is the full method:
-(void)fetchFacebookFrom:(NSString*)userID withAppKey:(NSString*)appKey
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{
#"ACFacebookAppIdKey" : appKey,
#"ACFacebookPermissionsKey" : #[#"email"],
#"ACFacebookAudienceKey" : ACFacebookAudienceEveryone
};
[accountStore requestAccessToAccountsWithType:accountType
options:options completion:^(BOOL granted, NSError *error) {
if(granted)
{
NSArray *arrayOfAccounts = [accountStore
accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
ACAccount *facebookAccount = [arrayOfAccounts lastObject];
NSURL *newsFeed = [NSURL URLWithString:#"https://graph.facebook.com/12345678901/feed"];
SLRequest *retrieveWall = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:newsFeed parameters:nil];
retrieveWall.account = facebookAccount;
[retrieveWall performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (error)
{
NSLog(#"Error: %#", [error localizedDescription]);
}
else
{
// The output of the request is placed in the log.
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
NSArray *statuses = [jsonResponse objectForKey:#"data"];
}
}];
}
}
else
{
NSLog(#"Not granted");
}
}];
}
The error specificly points to "URL" in the method call..
This stupid error is driving me crazy for 3 hours already. I restarted XCode, cleaned the project and rebooted my Mac. Who sees the error?
EDIT: It seems that this particular code is not the problem. I added core data too, which has some functions with URL in method names.
Every function with URL in the method name now gives a Parse Error and points to "URL". Getting closer to the problem, but still not there!
We defined URL somewhere in a header file. Removing this fixed it. Thanks for thinking about a solution!

Facebook in iOS6.0 use SLRequest to upload a photo failed anyway

Here Comes my Objc code:
ACAccountStore *facebookaccount = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{ ACFacebookAppIdKey: #"1234567899876543", ACFacebookPermissionsKey: #[#"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [facebookaccount accountsWithAccountType:facebookaccountType];
if ([accountsArray count] > 0) {
ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
NSString *sendmessage = #"Face";
NSData *myImageData = UIImagePNGRepresentation(imageSource);
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"https://graph.facebook.com/me/photos"] parameters:nil];
[facebookRequest addMultipartData:myImageData withName:#"source" type:#"multipart/form-data" filename:nil];
[facebookRequest addMultipartData:[sendmessage dataUsingEncoding:NSUTF8StringEncoding] withName:#"message" type:#"multipart/form-data" filename:nil];
[facebookRequest setAccount:facebookAccount];
[facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
if (error == nil) {
NSLog(#"responedata:%#", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}else{
NSLog(#"%#",error.description);
}
}
}
else
{
NSLog(#"error description : %#",[NSString stringWithFormat:#"%#", error.localizedDescription]);
}
}];
Finally I get these respone data:
responedata:{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}
Help me please!!!
I can successfully upload a photo by including a file name in addMultipartData and by passing the message as part of the SLRequest options.
code:
NSDictionary *parameters = #{#"message": sendmessage};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/photos"]
parameters:parameters];
[facebookRequest addMultipartData: myImageData
withName:#"source"
type:#"multipart/form-data"
filename:#"TestImage"];
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
// Log the result
}];

can not get user info after doing authentication,facebookSDK, ios

I am trying to authenticate using facebook account by following
- (IBAction)logInByFaceBook:(id)sender {
NSLog(#"appDelegate.session.isOpen = %#",appDelegate.session.isOpen == 0 ? #"NO" : #"YES");
FBSessionLoginBehavior behavior = FBSessionLoginBehaviorForcingWebView;
appDelegate.session = [[FBSession alloc] init];
[appDelegate.session openWithBehavior:behavior
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[NSNumber numberWithBool:YES] forKey:#"didLogIn"];
data = [[SavedData alloc] initLogInInfo:dict];
[data saveLogInInfoToDisk];
if (session.isOpen) {
FBRequest *me = [FBRequest requestForMe];
NSLog(#"me is %#",[me description]);
[me startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *my, NSError *error) {
NSLog(#"my is %#",[my description]);
}
];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
];
}
However, i cant not get my username back and the console is displaying
Error: HTTP status code: 400
my is (null)
By looking up this documentation, 400 means Invalid email address..... and my is null so that I can no extract the username at all.....
I dont know why this happens...I try with the same email address with facebookSDK sample code and it works properly...
Getting lost now....Can somebody point out what I am doing wrong or missing something.

Objective-C Twitter API 150 Requests

I am trying to fetch tweets from twitter using an NSURLConnection, and I keep hitting the maximum number of requests per hour (150). I switched to using the authenticated way of fetching tweets, like below, but still hit the maximum API calls - 150. How am I able to make more than 150 twitter requests per device?
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType =
[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType
withCompletionHandler:^(BOOL granted, NSError *error) {
if (!granted) {
// The user rejected your request
NSLog(#"User rejected access to the account.");
}
else {
// Grab the available accounts
NSArray *twitterAccounts =
[store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
// Use the first account for simplicity
ACAccount *account = [twitterAccounts objectAtIndex:0];
// Now make an authenticated request to our endpoint
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:#"1" forKey:#"include_entities"];
// The endpoint that we wish to call
NSURL *url =
[NSURL
URLWithString:#"https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=%#&count=5",someTwitterUserName];
// Build the request with our parameter
TWRequest *request =
[[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodGET];
// Attach the account object to this request
[request setAccount:account];
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!responseData) {
// inspect the contents of error
NSLog(#"%#", error);
}
else {
NSError *jsonError;
NSArray *timeline =
[NSJSONSerialization
JSONObjectWithData:responseData
options:NSJSONReadingMutableLeaves
error:&jsonError];
if (timeline) {
// at this point, we have an object that we can parse and grab tweet information from
NSLog(#"%#", timeline);
}
else {
// inspect the contents of jsonError
NSLog(#"%#", jsonError);
}
}
}];
} // if ([twitterAccounts count] > 0)
} // if (granted)
}];
Try see at this post
solution
Problem in your twitter app, who have a limit at default