I'm new with iOS development. I have to integrate Facebook login in my iOS app, as user logs in it fetches all the info and navigates it to home screen. I have done this via Facebook latest SDK but I want it to be done via social framework. I have gone through lots of online stuff and also have found examples where they are sharing(posting) content on Facebook page(Wall) via social framework but not Facebook login exactly. Anyone please help, let me know if Facebook login is possible via social framework or i have to stick to SDK. Thanks in Advance
6
-
@Facebbok SDK is the best solution for accessing all controls of Facebook. i will suggest this.– ChenSmileDec 26, 2013 at 7:14
-
1but isn't it bad practice ? like size of sdk is almost 10-11MB– ArunDec 26, 2013 at 7:19
-
but due to various security issues you could not use social framework as a complete Facebook SDK– ChenSmileDec 26, 2013 at 7:20
-
1If you want to fetch user's information, you need to use graph api in combination with social and accounts framework(for login) as explained below by @iSuman.– dispatchMainDec 26, 2013 at 7:38
-
2@ShehbazKhan pretty old stuff, with a lot of deprecated API. I wouldn't use. FB official SDK is the way to go, we can complain about it but it is the only way to havve a full integration with the graph API. Social iOS API requires that you have set an account in settings. The official SDK can work also asking username aand pwd on the fly.– AndreaDec 26, 2013 at 10:19
|
Show 1 more comment
3 Answers
Import Social and Acount Framwork
put this code in your .m file
-(void)facebook
{
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = @"451805654875339";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted)
{
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
self.facebookAccount = [accounts lastObject];
ACAccountCredential *facebookCredential = [self.facebookAccount credential];
NSString *accessToken = [facebookCredential oauthToken];
NSLog(@"Facebook Access Token: %@", accessToken);
NSLog(@"facebook account =%@",self.facebookAccount);
[self get];
[self getFBFriends];
isFacebookAvailable = 1;
} else
{
//Fail gracefully...
NSLog(@"error getting permission yupeeeeeee %@",e);
sleep(10);
NSLog(@"awake from sleep");
isFacebookAvailable = 0;
}
}];
}
-(void)checkfacebookstatus
{
if (isFacebookAvailable == 0)
{
[self checkFacebook];
isFacebookAvailable = 1;
}
else
{
printf("Get out from our game");
}
}
-(void)get
{
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
if(!error)
{
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Dictionary contains: %@", list );
self.globalmailID = [NSString stringWithFormat:@"%@",[list objectForKey:@"email"]];
NSLog(@"global mail ID : %@",globalmailID);
fbname = [NSString stringWithFormat:@"%@",[list objectForKey:@"name"]];
NSLog(@"faceboooookkkk name %@",fbname);
if([list objectForKey:@"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
});
}
else
{
//handle error gracefully
NSLog(@"error from get%@",error);
//attempt to revalidate credentials
}
}];
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = @"451805654875339";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"friends_videos"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {}];
}
-(void)accountChanged:(NSNotification *)notification
{
[self attemptRenewCredentials];
}
-(void)attemptRenewCredentials
{
[self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){
if(!error)
{
switch (renewResult) {
case ACAccountCredentialRenewResultRenewed:
NSLog(@"Good to go");
[self get];
break;
case ACAccountCredentialRenewResultRejected:
NSLog(@"User declined permission");
break;
case ACAccountCredentialRenewResultFailed:
NSLog(@"non-user-initiated cancel, you may attempt to retry");
break;
default:
break;
}
}
else{
//handle error gracefully
NSLog(@"error from renew credentials%@",error);
}
}];
}
Also logged in your native Facebook application in your device/Simulator
-
1For this you need to have a facebook app whose key you need to provide along with audience and permissions in login options dictionary. Dec 26, 2013 at 7:35
-
-
actually i did't implement this for twitter check this hope it will help you dev.twitter.com/docs/ios @AbhishekGupta Apr 18, 2014 at 5:33
-
Do you need to
requestAccessToAccountsWithType
every single time? Surely once the user has granted access, then you don't need to keep on checking it. Anyway around this? Nov 18, 2015 at 9:35 -
is there any issue with social login framework with earlier versions of 10.2 Aug 3, 2017 at 11:17
Swift 3 Version of solution @reena
@IBAction func loginButtonPressed(_ sender: UIButton) {
var facebookAccount: ACAccount?
let options:[String : Any] = [ACFacebookAppIdKey: "YOUR_FACEBOOK_API_KEY", ACFacebookPermissionsKey: ["email"],ACFacebookAudienceKey:ACFacebookAudienceFriends]
let accountStore = ACAccountStore()
let facebookAccountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook)
accountStore.requestAccessToAccounts(with: facebookAccountType, options: options) { (granted:Bool, error:Error?) in
if granted{
let accounts = accountStore.accounts(with: facebookAccountType)
facebookAccount = accounts?.last as? ACAccount
let dict:NSDictionary = facebookAccount!.dictionaryWithValues(forKeys: ["properties"]) as NSDictionary
let properties:NSDictionary = dict["properties"] as! NSDictionary
print("facebook Response is-->:%@",properties)
}
else {
if let err = error as? NSError, err.code == Int(ACErrorAccountNotFound.rawValue) {
DispatchQueue.main.async {
self.displayAlert("There is no Facebook accounts configured. you can add or created a Facebook account in your settings.")
}
} else {
DispatchQueue.main.async {
self.displayAlert("Permission not granted For Your Application")
}
}
}
}
}
Facebook login using Accounts and social framework (swift)
var facebookAccount: ACAccount?
let options: [NSObject : AnyObject] = [ACFacebookAppIdKey: "YOUR_FACEBOOK_API_KEY", ACFacebookPermissionsKey: ["publish_stream","publish_actions"],ACFacebookAudienceKey:ACFacebookAudienceFriends]
let accountStore = ACAccountStore()
let facebookAccountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook)
accountStore.requestAccessToAccountsWithType(facebookAccountType, options: options) { (granted, error) -> Void in
if granted
{
let accounts = accountStore.accountsWithAccountType(facebookAccountType)
self.facebookAccount = accounts.last as? ACAccount
let accountsArray=accountStore.accountsWithAccountType(facebookAccountType) as NSArray
self.facebookAccount=accountsArray.lastObject as? ACAccount
var dict:NSDictionary
dict=NSDictionary()
dict=self.facebookAccount!.dictionaryWithValuesForKeys(["properties"])
let properties:NSDictionary=dict["properties"] as! NSDictionary
print("facebook Response is-->:%@",properties)
}
else
{
if error.code == Int(ACErrorAccountNotFound.rawValue) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.displayAlert("There is no Facebook accounts configured. you can add or created a Facebook account in your settings.")
})
} else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.displayAlert("Permission not granted For Your Application")
})
}
}
}