Tôi bao gồm lớp tiện ích Facebook tôi đã viết giúp hiểu trạng thái đăng nhập, vì tôi hiển thị thông báo "đăng nhập"/"chưa đăng nhập" cho người dùng trong Giao diện người dùng cài đặt của riêng mình, ngoài việc sử dụng thành phần 'FBLoginView' thực tế khi đến lúc cho phép người dùng chuyển đổi trạng thái ủy quyền.
Mã bên dưới cũng có sẵn qua số gist này.
Tôi có thể không giải thích tất cả các loại FBSessionState
một cách chính xác trong báo cáo chuyển đổi của mình, nhưng cho đến nay, nó đã phục vụ tốt cho tôi trong các trường hợp tôi đã thử nghiệm (tôi vừa mới kết hợp).
Điều quan trọng cần lưu ý là đôi khi bạn có mã thông báo ủy quyền được lưu trong bộ nhớ cache mà bạn không thể sử dụng ngay lập tức, nhưng nếu thực hiện cuộc gọi trên Facebook open
, bạn có thể làm cho nó trở thành có thể tái sử dụng (được làm mới). Cuộc gọi mở này hoạt động sau hậu trường, không kích hoạt bất kỳ chuyển đổi cửa sổ/ứng dụng OAuth UI/jarring nào (nếu bạn có mã thông báo được lưu trong bộ nhớ cache).
Xem nhận xét của tôi theo phương pháp isLoggedInAfterOpenAttempt
. Lưu ý cách tôi kiểm tra trạng thái là FBSessionStateCreatedTokenLoaded
và chỉ sau đó, thực hiện cuộc gọi đến
-openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error)
.
mẩu tin khác về lớp này:
- tôi có một tài sản vào đây để bộ nhớ cache đăng nhập của người dùng, các loại phù hợp với giao thức
FBGraphUser
. Tuy nhiên, nó không được sử dụng trong bất kỳ phương thức đăng nhập nào được minh họa ở đây.
- Mã mẫu Facebook SDK 3.0 cho iOS đề xuất xây dựng lớp học của riêng bạn để giữ và quản lý các loại hoạt động này, nếu bạn có bất cứ điều gì nhiều hơn một ứng dụng tầm thường. Lớp này của tôi dưới đây là sự khởi đầu của ý tưởng đó cho ứng dụng của tôi.
- Bạn có thể thay thế macro 'log4Info' và 'log4Debug' bằng NSLog để làm việc này.
#import "UWFacebookService.h"
@implementation UWFacebookService
// Static
static const int ddLogLevel = LOG_LEVEL_DEBUG;
// Strong
@synthesize facebookGraphUser = _facebookGraphUser;
#pragma mark - Inquiries
- (BOOL)isSessionStateEffectivelyLoggedIn:(FBSessionState)state {
BOOL effectivelyLoggedIn;
switch (state) {
case FBSessionStateOpen:
log4Info(@"Facebook session state: FBSessionStateOpen");
effectivelyLoggedIn = YES;
break;
case FBSessionStateCreatedTokenLoaded:
log4Info(@"Facebook session state: FBSessionStateCreatedTokenLoaded");
effectivelyLoggedIn = YES;
break;
case FBSessionStateOpenTokenExtended:
log4Info(@"Facebook session state: FBSessionStateOpenTokenExtended");
effectivelyLoggedIn = YES;
break;
default:
log4Info(@"Facebook session state: not of one of the open or openable types.");
effectivelyLoggedIn = NO;
break;
}
return effectivelyLoggedIn;
}
/**
* Determines if the Facebook session has an authorized state. It might still need to be opened if it is a cached
* token, but the purpose of this call is to determine if the user is authorized at least that they will not be
* explicitly asked anything.
*/
- (BOOL)isLoggedIn {
FBSession *activeSession = [FBSession activeSession];
FBSessionState state = activeSession.state;
BOOL isLoggedIn = activeSession && [self isSessionStateEffectivelyLoggedIn:state];
log4Info(@"Facebook active session state: %d; logged in conclusion: %@", state, (isLoggedIn ? @"YES" : @"NO"));
return isLoggedIn;
}
/**
* Attempts to silently open the Facebook session if we have a valid token loaded (that perhaps needs a behind the scenes refresh).
* After that attempt, we defer to the basic concept of the session being in one of the valid authorized states.
*/
- (BOOL)isLoggedInAfterOpenAttempt {
log4Debug(@"FBSession.activeSession: %@", FBSession.activeSession);
// If we don't have a cached token, a call to open here would cause UX for login to
// occur; we don't want that to happen unless the user clicks the login button over in Settings, and so
// we check here to make sure we have a token before calling open
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
log4Info(@"We have a cached token, so we're going to re-establish the login for the user.");
// Even though we had a cached token, we need to login to make the session usable:
[FBSession.activeSession openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
log4Info(@"Finished opening login session, with state: %d", status);
}];
}
else {
log4Info(@"Active session wasn't in state 'FBSessionStateCreatedTokenLoaded'. It has state: %d", FBSession.activeSession.state);
}
return [self isLoggedIn];
}
@end
Bạn cần phải mở lại phiên của bạn trên mỗi mắt ứng dụng. –
Làm thế nào để làm điều đó? Tôi đang thiếu phần đó. sẽ '[FBSession sessionOpenWithPermissions: ...]' làm các trick mà không hiển thị hộp thoại auth mỗi lần? –
điều này: '+ (BOOL) openActiveSessionWithAllowLoginUI: (BOOL) allowLoginUI' và google FBSession - nghĩa đen là lần truy cập đầu tiên. –