Create an Event on FB and invite friends through ios7 - ios

Hi I have create an event on Facebook through my iOS app by following code
-(void) createEvent:(NSString*)eventName Eventdesc:(NSString*)eventdesc EventDate: (NSString*)eventDate{
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me/events"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
eventName, #"name",
eventdesc, #"description",
[NSString stringWithFormat:#"%#",[NSDate date]], #"start_time",
nil];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:meurl
parameters:params];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"event %#", meDataString);
if (!error) {
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"" message:#"Thanks for your support. Event has been created on your facebook page." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alertView show];
}else{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"" message:#"Unable to create event on facebook." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alertView show];
}
}];
}
Now I want to invite friends to this created event. I search lot but I did not get any helpful information.
please help. Thank you in advance.

You can invite users to an event by issuing an HTTP POST to /EVENT_ID/invited/USER_ID. You can invite multiple users by issuing an HTTP POST to /EVENT_ID/invited?users=USER_ID1,USER_ID2,USER_ID3. Both of these require the create_event permission and return true if the invite is successful.

Solving the problem by using follwing code
pragma mark- Invite Friends Method
-(void)inviteFriendsToEvent:(NSString*)eventID UserID:(NSMutableArray*)userID{
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);
}];
}

Related

How can I share a file with iOS?

So I have my app create a file, in this case it's actually an animated .gif. This animated .gif is saved on the device and is all ready to go - what I want to do now is share this file when the user clicks the "Share" button.
Simply put, how can I achieve this?
Is there a way to just tell iOS to open up the share window and that I'm sharing an image? Right now I can't find a way to do that, so instead I'm creating a new UIImage with a path to this animated .gif on the device, but this ends up converting my animated file into a static image.
Again - I already have the animated .gif file created and saved on the user's device.
Any help appreciated.
Edit: To clarify, I'm used to Android, I just call up an intent to share a file and up pops a list of Gmail, Facebook, Twitter, etc, etc... I basically want the equivalent of that, if possible.
You may want to take a look at the UIActivityViewController document. This will open a sheet with all the ways available to share on the device. See here https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActivityViewController_Class/index.html#//apple_ref/doc/uid/TP40011976-CH1-SW2 . You may want to initialize the ActivityViewController with images or UIActivityItemSource
Facebook and twitter not support UIActivityViewController, still in this used only limited application. Now this code very usedfully.
-(void)uploadImageToTwitter {
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [account
accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
ACAccount *twitterAccount =
[arrayOfAccounts lastObject];
//
// NSURL *furl = [NSURL fileURLWithPath:NSTemporaryDirectory()];
// NSURL *fileURL = [furl URLByAppendingPathComponent:#"animation.gif"];
NSData *imageData = [NSData dataWithContentsOfURL:_GIFURL];
NSURL *requestURL = [NSURL URLWithString:#"https://upload.twitter.com/1.1/media/upload.json"];
SLRequest *postRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:requestURL parameters:nil];
postRequest.account = twitterAccount;
[postRequest addMultipartData:imageData
withName:#"media"
type:#"image/gif"
filename:#"test.gif"];
[postRequest
performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSString *mediaID = [json objectForKey:#"media_id_string"];
if (mediaID!=nil) {
NSURL *requestURL2 = [NSURL URLWithString:#"https://api.twitter.com/1.1/statuses/update.json"];
NSDictionary *message2 = #{#"status": #"Here is the image",
#"media_ids": mediaID };
SLRequest *postRequest2 = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:requestURL2 parameters:message2];
postRequest2.account = twitterAccount;
[postRequest2
performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
// DONE!!!
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Notification" message:#"Upload Twitter Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}];
}
}];
}
}
}];
}

Getting error while fetching picture of facebook friends with Graph API in iOS Social Framework

I'm using below code to get facebook friends list, and its working fine. But there is a need of friends profile picture too.
NSString *fbPermissions = #[ #"user_about_me",#"email",#"user_friends"];
NSString *apiIdKey = #"App_Key";
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
id options = #{
ACFacebookAppIdKey: apiIdKey,
ACFacebookPermissionsKey: fbPermissions,
ACFacebookAudienceKey: ACFacebookAudienceFriends,
};
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];
ACAccount *tempAccount = [arrayOfAccounts lastObject];
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends"];
NSURL *fbURL = [NSURL URLWithString:string];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:fbURL
parameters:nil];
[fbRequest setAccount:tempAccount];
[fbRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error)
{
NSError *jsonError = nil;
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSMutableArray *array = [data valueForKey:#"data"];
NSLog(#"Friends array:- %#",array);
}
else
{
[[[UIAlertView alloc]initWithTitle:#"Error"
message:[error description]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil] show];
}
}];
}
}];
To get profile picture I have changed request url with:-
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends?fields=picture,name"];
I got following response :-
{
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}
I'm not sure its access token expiration error because to revert back request Url to previous one, code works fine.
Finally it worked with:-
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends"];
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET URL:requestURL parameters:#{#"fields":#"id,name,picture,first_name,last_name,gender"}];

Can't able to fetch the User Profile photo in ios

I am trying to get the facebook account details using SLRequest in ios.
I got all the details But Can not able to get the Profile Photo link of the User.
My code is as follow:
NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
#"xxxxxxxxxxxxxx", ACFacebookAppIdKey,
[NSArray arrayWithObjects:#"email", nil] , ACFacebookPermissionsKey,
nil];
[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:options
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
NSLog(#"Success");
[self me];
}else{
// ouch
NSLog(#"Fail");
NSLog(#"Error: %#", error);
}
}];
- (void)me{
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#", meDataString);
NSJSONSerialization *js=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
NSLog(#"%#",js);
}];
}
Help me to solve this.
You can get the user's ID from the returned response, then use the below to get the profile picture:
http://graph.facebook.com/User_ID/picture
I use SDWebImage for images:
NSString *urlstr = #"http://graph.facebook.com/100001393655684/picture";
[imageView setImageWithURL:[NSURL URLWithString:urlstr]
placeholderImage:nil];
For Twitter, please make sure you authenticate your request, check this link for more details
Note: You may need to update TWRequest to SLRequest if you aren't deploying for iOS5

ACAccountStore - trying to follow on Twitter - 410 error

Here is my code for "follow on twitter" (iOS6+):
-(IBAction)twitterFollow
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(#"twitter accounts: %#", accountsArray);
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"myapp" forKey:#"screen_name"];
[tempDict setValue:#"true" forKey:#"follow"];
//requestForServiceType
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"https://api.twitter.com/1/friendships/create.json"] parameters:tempDict];
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i Error %d", [urlResponse statusCode],error.code];
NSLog(#"%#error %#", output,error.description);
NSString *twitterErrorMessage = [error localizedDescription];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:kDefaultErrorText message:twitterErrorMessage delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show)
withObject:nil
waitUntilDone:NO];
}];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:kDefaultErrorText message:kTextMessageNoTwitter delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show)
withObject:nil
waitUntilDone:NO];
}
}
}];
}
This is what I get:
HTTP response status: 410 Error 0error (null)
What am I doing wrong?
The screen name and account are valid:
twitter accounts: (
"type:com.apple.twitter\nidentifier: 2ABCDEC6-9D6B-1234-9AB7-847EB167B123\naccountDescription: #theuser\nusername: theuser\nobjectID: x-coredata://ABD5C87A-FB5D-464E-AF18-2C1123451123/Account/p13\nenabledDataclasses: {(\n)}\nenableAndSyncableDataclasses: {(\n)}\nproperties: {\n fullName = theuser;\n \"user_id\" = 147712345;\n}\nparentAccount: (null)\nowningBundleID:(null)"
)
Well, I got rid of the 410 error by using the 1.1 API:
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"https://api.twitter.com/1.1/friendships/create.json"] parameters:tempDict];
Now I get a 400 error:
HTTP response status: 400 Error 0error (null)
Which suggested invalid authentication, even though the code above looks exactly like the code I see on every tutorial/example I find. I guess I'll create another question...

Twitter SLRequest performRequestWithHandler - Could not prepare the URL request

I am trying to implement "Follow Us On Twitter" in my iOS app. Here is my code. But it give error "Could not prepare the URL request". Please help!
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore
requestAccessToAccountsWithType:accountType
options:NULL
completion:^(BOOL granted, NSError *error) {
if (granted) {
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"a4arpan" forKey:#"screen_name"];
[tempDict setValue:#"true" forKey:#"follow"];
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://api.twitter.com/1.1/friendships/create.json"]
parameters:tempDict];
ACAccount * twitterAccount = [[ACAccount alloc] initWithAccountType:accountType];
twitterAccount.username = twitterUsername;
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
NSError *jsonError;
NSDictionary *timelineData =
[NSJSONSerialization
JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments error:&jsonError];
if (timelineData) {
NSLog(#"Timeline Response: %#\n", timelineData);
}
else {
// Our JSON deserialization went awry
NSLog(#"JSON Error: %#", [jsonError localizedDescription]);
}
if ([urlResponse statusCode] == 200) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Follow us successfull" message:nil delegate:nil cancelButtonTitle:#"Thanx" otherButtonTitles:nil, nil];
[alert show];
}
else {
if ([tpAppMode isEqualToString:#"sandbox"])
NSLog(#"%#", [error localizedDescription]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Follow us Failed" message:nil delegate:nil cancelButtonTitle:#"Thanx" otherButtonTitles:nil, nil];
[alert show];
}
}
else {
// The server did not respond successfully... were we rate-limited?
NSLog(#"The response status code is %d", urlResponse.statusCode);
}
}
else {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %# %#", [error localizedDescription], [error localizedFailureReason]];
NSLog(#"%#", output);
}
}];
}
else {
if ([tpAppMode isEqualToString:#"sandbox"])
NSLog(#"%#", [error localizedDescription]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Follow us Failed" message:nil delegate:nil cancelButtonTitle:#"Thanx" otherButtonTitles:nil, nil];
[alert show];
}
}];
I have followed all the steps mentioned on Twitter Dev
That is how I've done it!
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"https://api.twitter.com/1.1/friendships/create.json"] parameters:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"AnatoliyGatt", #"true", nil] forKeys:[NSArray arrayWithObjects:#"screen_name", #"follow", nil]]];
[request setAccount:[[self twitterAccounts] lastObject]];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if(responseData) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
if(responseDictionary) {
// Probably everything gone fine
}
} else {
// responseDictionary is nil
}
}];

Resources