1. Adjuntamos el framework Accounts a nuestro proyecto.
2. Importamos las cabeceras a nuestro archivo de cabecera.
#import <Accounts/Accounts.h>
3. Añadimos el siguiente código en el método donde queramos implementar la función (en este caso se toma como ejemplo el "follow" de Twitter).
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error)
{
if(granted)
{
NSArray *accountsArray = [accountStore accountsWithAccountType:twitterAccountType];
// En caso de tener más de una cuenta, aquí se debería integrar el código
if ([accountsArray count] > 0)
{
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *twitterApiDictionary = [[NSMutableDictionary alloc] init];
[twitterApiDictionary setValue:@"OhMyAppss" forKey:@"screen_name"];
[twitterApiDictionary setValue:@"true" forKey:@"follow"];
SLRequest *twitterRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"http://api.twitter.com/1/friendships/create.json"] parameters:twitterApiDictionary];
[twitterRequest setAccount:twitterAccount];
[twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(@"%@",[NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]]);
//Aquí se deben estudiar los posibles casos de error
}
}
}];
}
Para conocer todas las funciones que nos ofrecen Twitter y Facebook deberemos consultar su documentación:
- Facebook https://developers.facebook.com/docs/
- Twiter https://dev.twitter.com/docs
No hay comentarios:
Publicar un comentario