0

I want to send the messages and want to post on facebook and twitter in background is there any possible framework is there to do this.Can any one share the idea please.Thanks in advance .

sairam
  • 299
  • 1
  • 6
  • 17

2 Answers2

1

To post on Twitter in background use this library https://github.com/nst/STTwitter

You will need to register your app on Twitter to receive consumer key and consumer secret. After you have it just use the code represented here Accessing Twitter Direct Messages using SLRequest iOS. This code uses system Twitter credentials, user have to set these credentials in iPhone / iPad settings.

Regarding to post on Facebook: I found no wrapper for iOS which can provide possibility to post in background. I use official Facebook iOS SDK is here https://github.com/facebook/facebook-ios-sdk. But post functionality works only with user's participation.

Community
  • 1
  • 1
Igor Matyushkin
  • 778
  • 4
  • 4
  • to post on facebook on the background, only the "request publish permissions" requires the user participation, if your app already has this, you can post on the background. its explained on the facebook sdk. – Pochi Aug 20 '14 at 04:46
1

For facebook make sure you have the user permission or sdk setup properly. I would recommend to go through this tutorial for details. http://m-farhan.com/2014/03/ios-facebook-sdk-tutorial/

    - (void)requestPermissionAndPost {
        [FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObjects:@"publish_actions", @"publish_checkins",nil]
                                              defaultAudience:FBSessionDefaultAudienceEveryone
                                            completionHandler:^(FBSession *session, NSError *error) {
                                                if (!error) {
                                                    // Now have the permission
                                                    [self postOpenGraphAction];
                                                } else {
                                                    // Facebook SDK * error handling *
                                                    // if the operation is not user cancelled
                                                    if (error.fberrorCategory != FBErrorCategoryUserCancelled) {
                                                        [self presentAlertForError:error];
                                                    }
                                                }
                                            }];
    }
    - (void)postOpenGraphAction
    {
        FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
        FBRequestHandler handler =
        ^(FBRequestConnection *connection, id result, NSError *error) {
            // output the results of the request
            [self requestCompleted:connection forFbID:@"me" result:result error:error];
        };

        UIImage *img = imageView.image;
        NSString *message = @"Your Message";
        FBRequest *request=[[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:@"me/photos" parameters:[NSDictionary dictionaryWithObjectsAndKeys:UIImageJPEGRepresentation(img, 0.7),@"source",message,@"message",@"{'value':'EVERYONE'}",@"privacy", nil] HTTPMethod:@"POST"];


        [newConnection addRequest:request completionHandler:handler];
        [self.requestConnection cancel];
         self.requestConnection = newConnection;
        [newConnection start];
    }
// FBSample logic
// Report any results.  Invoked once for each request we make.
- (void)requestCompleted:(FBRequestConnection *)connection
                 forFbID:fbID
                  result:(id)result
                   error:(NSError *)error
{
    NSLog(@"request completed");

    // not the completion we were looking for...
    if (self.requestConnection &&
        connection != self.requestConnection)
    {
        NSLog(@"    not the completion we are looking for");
        return;
    }

    // clean this up, for posterity
    self.requestConnection = nil;

    if (error)
    {
        NSLog(@"    error");
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        // error contains details about why the request failed
        [alert show];
    }
    else
    {
        NSLog(@"   ok");
        NSLog(@"%@",result);
        /*[[[UIAlertView alloc] initWithTitle:@"Result"
                                    message:[NSString stringWithFormat:@"Posted Open Graph action, id: %@",
                                             [result objectForKey:@"id"]]
                                   delegate:nil
                          cancelButtonTitle:@"Thanks!"
                          otherButtonTitles:nil]
         show];*/
        [self doCheckIn];

    };
}

fro Twitter its simple and easy

#import <Twitter/Twitter.h>

 - (void)TWPostImage:(UIImage *)image withStatus:(NSString *)status
    {
        ACAccountStore *accountStore = [[ACAccountStore alloc] init];

        //ACAccountStore *accountStore = [[ACAccountStore alloc] init];

        // Create an account type that ensures Twitter accounts are retrieved.
        ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

        [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
            ACAccount *ac;
            if(granted) {
                NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
                int i=0;
                for (ACAccount *account in accountsArray ) {
                    i++;
                    NSLog(@"Account name: %@", account.username);
                    ac=account;
                }
                if (i==0) {
                    [[[UIAlertView alloc] initWithTitle:@"Wait"
                                                message:@"Please setup Twitter Account Settigns > Twitter > Sign In "
                                               delegate:nil
                                      cancelButtonTitle:@"Thanks!"
                                      otherButtonTitles:nil]
                     show];
                    return ;

                }

                ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];;

                //SLRequestHandler requestHandler;

                SLRequestHandler requestHandler =
                ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                    if (responseData) {
                        NSInteger statusCode = urlResponse.statusCode;
                        if (statusCode >= 200 && statusCode < 300) {
                            NSDictionary *postResponseData =
                            [NSJSONSerialization JSONObjectWithData:responseData
                                                            options:NSJSONReadingMutableContainers
                                                              error:NULL];
                            NSLog(@"[SUCCESS!] Created Tweet with ID: %@", postResponseData[@"id_str"]);
                            ESAppDelegate* d =[[UIApplication sharedApplication] delegate];
                            NSString* link = [NSString stringWithFormat:@"Your Message",twitterAccStore,hashtagFromStore];
                            [d linkSelected:link PointerToSelf:self];
                            /*[[[UIAlertView alloc] initWithTitle:@"Result"
                                                        message:[NSString stringWithFormat:@"[SUCCESS!] Created Tweet with ID: %@", postResponseData[@"id_str"]]
                                                       delegate:nil
                                              cancelButtonTitle:@"Thanks!"
                                              otherButtonTitles:nil]
                             show];*/

                        }
                        else {
                            NSLog(@"[ERROR] Server responded: status code %d %@", statusCode,
                                  [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
                        }
                    }
                    else {
                        NSLog(@"[ERROR] An error occurred while posting: %@", [error localizedDescription]);
                    }
                    });
                };
                //});
                ACAccountStoreRequestAccessCompletionHandler accountStoreHandler =
                ^(BOOL granted, NSError *error) {
                    if (granted) {
                        NSArray *accounts = [accountStore accountsWithAccountType:twitterType];
                        NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
                                      @"/1.1/statuses/update_with_media.json"];
                        NSDictionary *params = @{@"status" : status};
                        SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                                requestMethod:SLRequestMethodPOST
                                                                          URL:url
                                                                   parameters:params];
                        NSData *imageData = UIImageJPEGRepresentation(image, 1.f);
                        [request addMultipartData:imageData
                                         withName:@"media[]"
                                             type:@"image/jpeg"
                                         filename:@"image.jpg"];
                        [request setAccount:[accounts lastObject]];

                        [request performRequestWithHandler:requestHandler];
                        //});
                    }
                    else {
                        NSLog(@"[ERROR] An error occurred while asking for user authorization: %@",
                              [error localizedDescription]);
                    }
                };

                [accountStore requestAccessToAccountsWithType:twitterType
                                                      options:NULL
                                                   completion:accountStoreHandler];
            }else
            {
                [[[UIAlertView alloc] initWithTitle:@"Wait"
                                            message:@"Please Settigns > Twitter > In bottom Enable DealsHype to post"
                                           delegate:nil
                                  cancelButtonTitle:@"Thanks!"
                                  otherButtonTitles:nil]
                                    show];
            }
        }];
    }
m-farhan
  • 1,436
  • 13
  • 14
  • will it check my login on FB and twitter in my device settings – sairam Mar 23 '14 at 09:09
  • @sairam if you are using tutorial i have gave you it tells you all option FB SDK take care of all options and twitter will ask in device settings – m-farhan Mar 23 '14 at 09:49