0

How to do Facebook login in iOS without Facebook sdk(i am supporting iOS6 and iOS7). My app is using Facebook login and user can share post on Facebook. Also i need to send the Facebook user id and auth token to my server.

Earlier I was supporting iOS 5 but now my iOS target >= iOS6 with Xcode 5.

For this i used Facebook SDK 3.11. From researching, i come to know that we can use SLComposeViewController, UIActivityViewController or SLRequest to share post. This will solve my sharing issue but how to do Facebook login and get auth token?

I tried SLRequest for Facebook login and SLComposeViewController for sharing, then is this good solution? (Here, i want to show Facebook native share dialog. So i haven't use SLRequest to share post because we have to make view for it.)

I referred this link. Is this the best solution to go forward?

miken32
  • 42,008
  • 16
  • 111
  • 154
Kirti Nikam
  • 2,166
  • 2
  • 22
  • 43

1 Answers1

0

only -if you have deployment target 6 -you have set your bundle id in setting, develelopers.facebook.com -you have set fb_app_id in your plist then

do like this without facebook sdk,

#import <Social/Social.h>
#import <Accounts/Accounts.h>
#define FB_APP_ID @"45666675444" // ur id here
@interface OGShareViewController ()
@property (strong, nonatomic) ACAccountStore *accountStore;
@property (strong, nonatomic) ACAccount *facebookAccount;
@property (strong, nonatomic) ACAccountType *facebookAccountType;
@end

-(void)viewDidLoad
{
[self getMyDetails];
}
- (void) getMyDetails {
    if (! _accountStore) {
        _accountStore = [[ACAccountStore alloc] init];
    }

    if (! _facebookAccountType) {
        _facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    }

    NSDictionary *options = @{ ACFacebookAppIdKey: FB_APP_ID };

    [_accountStore requestAccessToAccountsWithType: _facebookAccountType
                                           options: options
                                        completion: ^(BOOL granted, NSError *error) {
                                            if (granted) {
                                                NSArray *accounts = [_accountStore accountsWithAccountType:_facebookAccountType];
                                                _facebookAccount = [accounts lastObject];

                                                NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"];

                                                SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                                                        requestMethod:SLRequestMethodGET
                                                                                                  URL:url
                                                                                           parameters:nil];
                                                request.account = _facebookAccount;

                                                [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                                                    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData
                                                                                                                       options:NSJSONReadingMutableContainers
                                                                                                                         error:nil];
                                                    NSLog(@"id: %@", responseDictionary[@"id"]);
                                                    NSLog(@"first_name: %@", responseDictionary[@"first_name"]);
                                                    NSLog(@"last_name: %@", responseDictionary[@"last_name"]);
                                                    NSLog(@"gender: %@", responseDictionary[@"gender"]);
                                                    NSLog(@"city: %@", responseDictionary[@"location"][@"name"]);
                                                    NSLog(@"user name: %@", responseDictionary[@"username"]);

//                                                    http://graph.facebook.com/facebook/picture?type=normal
                                                    NSString *userPic  = [NSString stringWithFormat:@" http://graph.facebook.com/%@/picture?type=normal", responseDictionary[@"username"]
                                                                          ];
                                                      NSLog(@"profile pic link: %@", userPic);


                                                }];
                                            } else {
                                                [self showAlert:@"Facebook access for this app has been denied. Please edit Facebook permissions in Settings."];
                                            }
                                        }];
}

- (void) showAlert:(NSString*) msg {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"WARNING"
                                  message:msg
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    });
}
Noor
  • 2,071
  • 19
  • 28