I am making my first attempts on FB integration with the iOS SDK.
I have succeeded in establishing a connection with my fb app over the FBLoginView
which works so far.
Here are my corresponding calls:
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
NSLog(@"logged in");
}
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
NSLog(@"logged out");
[FBSettings setLoggingBehavior:[NSSet setWithObjects:FBLoggingBehaviorFBRequests, nil]];
if (FBSession.activeSession.isOpen) {
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,id<FBGraphUser> user,NSError *error) {
if (!error) {
NSString *fbID = user.id;
NSLog(@"UserID: %@",fbID);
NSLog(@"TESTING: %@",user.name);
}
}];
}
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
NSLog(@"Hello %@!", user.first_name);
}
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
NSLog(@"FBLoginView encountered an error=%@", error);
}
i have 1 small and 1 big (topic) question.
The small : The request gets fullfilled allright and i get a nice log of it:
Response Body:
(
{
body = {
gender = male;
id = ###;
...
};
code = 200;
}
)
But somehow my 2 testlogs dont show, any ideas?
The big (topic): I establish that connection in a designated controller in my app. my problem is that on every restart of the app the connection somehow is lost (session not active anymore).. Is there a way to establish an infinite connection using the FBLoginView
?
I have added the following calls & methods to my appdelegate:
//upon start:
if (![FBSession activeSession].isOpen) {
[self connectWithFacebook];
}
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
//..
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
if (error) {
NSLog (@"Handle error %@", error.localizedDescription);
} else {
[FBSession setActiveSession:session];
[self checkSessionState:state];
}
}];
}
- (void) connectWithFacebook {
[self openSessionWithAllowLoginUI:YES];
}
- (void) checkSessionState:(FBSessionState)state {
switch (state) {
case FBSessionStateOpen:
break;
case FBSessionStateCreated:
break;
case FBSessionStateCreatedOpening:
break;
case FBSessionStateCreatedTokenLoaded:
break;
case FBSessionStateOpenTokenExtended:
// I think this is the state that is calling
break;
case FBSessionStateClosed:
break;
case FBSessionStateClosedLoginFailed:
break;
default:
break;
}
}
this allows relogin, but every time the app is opened the login screen shows again which is not very userfriendly. Is there a solution to this, and if yes, what am i missing or in what direction could i proceed?
This is my implementation, keeps the session open for 60 days (maximum allowed time by FB SDK 3.2.1)
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
NSArray *readPermissions = @[@"email",@"friends_birthday",@"friends_likes",@"friends_interests",@"user_birthday",@"user_interests",@"user_likes",@"user_location"];
return [FBSession openActiveSessionWithReadPermissions:readPermissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState)state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
// We have a valid session
NSLog(@"User session found");
if (FBSession.activeSession.isOpen) {
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection,
id<FBGraphUser> user,
NSError *error) {
if (!error) {
NSLog(@"accessToken: %@ userID: %@",[FBSession activeSession].accessTokenData.accessToken,user.id);
self.userID = user.id;
[[NSUserDefaults standardUserDefaults] setValue:user.first_name forKey:@"first_name"];
[[NSUserDefaults standardUserDefaults] setValue:user.last_name forKey:@"last_name"];
NSString *accessToken = [FBSession activeSession].accessTokenData.accessToken;
if (accessToken) {
[[NSNotificationCenter defaultCenter] postNotificationName:FacebookLoginInProgressNotification object:nil];
[self submitFacebookUserID:user.id andAccessToken:accessToken];
}
else {
NSLog(@"no access token for userID: %@",user.id);
[[NSNotificationCenter defaultCenter] postNotificationName:FacebookLoginFailureNotification object:nil];
}
}
else {
//handle error retrieving User ID
NSLog(@"error retrieving User ID [%@]",[error localizedDescription]);
[[NSNotificationCenter defaultCenter] postNotificationName:FacebookLoginFailureNotification object:nil];
}
}];
} else {
[FBSession setActiveSession:session];
}
// Pre-fetch and cache the friends for the friend picker as soon as possible to improve
// responsiveness when the user tags their friends.
FBCacheDescriptor *cacheDescriptor = [FBFriendPickerViewController cacheDescriptor];
[cacheDescriptor prefetchAndCacheForSession:session];
}
break;
case FBSessionStateClosed: {
[FBSession.activeSession closeAndClearTokenInformation];
}
break;
case FBSessionStateClosedLoginFailed: {
[FBSession.activeSession closeAndClearTokenInformation];
}
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
NSLog(@"Facebook Error %@", error);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.