アプリへの認証に FBSession をネイティブに使用しましたが、これは正常に機能しており、アクセストークンも取得しています。
しかし、私の問題は、特定の URl を渡して webView にクライアント facebook ページを読み込んでいるときに、ページが正常に表示されているのにログインしていないことです。それでも、上部に [参加してログイン] ボタンが表示されます。
私はすでにログインしているので、なぜ再度ログインする必要があるのですか。
友達がこれを解決する方法を教えてくれます。
これがコードです。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self updateView];
SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (!appDelegate.session.isOpen) {
// create a fresh session object
appDelegate.session = [[FBSession alloc] init];
// 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, and so
// we check here to make sure we have a token before calling open
if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
// even though we had a cached token, we need to login to make the session usable
[appDelegate.session openWithBehavior:FBSessionLoginBehaviorForcingWebView
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// this handler is called back whether the login succeeds or fails; in the
// success case it will also be called back upon each state transition between
// session-open and session-close
[self updateView];
}];
NSArray *permissions1 = @[@"email"];
[appDelegate.session requestNewReadPermissions:permissions1
completionHandler:^(FBSession *session,
NSError *error)
{
// Handle new permissions callback
}];
}
}
}
// FBSample logic
// main helper method to update the UI to reflect the current state of the session.
- (void)updateView {
// get the app delegate, so that we can reference the session property
SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (appDelegate.session.isOpen) {
// valid account UI is shown whenever the session is open
[self.buttonLoginLogout setTitle:@"Log out" forState:UIControlStateNormal];
[self.textNoteOrLink setText:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",
appDelegate.session.accessTokenData.accessToken]];
NSLog(@"Print %@",self.textNoteOrLink.text);
// NSLog(@"Print the user name is %@",appDelegate.session.accessTokenData)
} else {
// login-needed account UI is shown whenever the session is closed
[self.buttonLoginLogout setTitle:@"Log in" forState:UIControlStateNormal];
[self.textNoteOrLink setText:@"Login to create a link to fetch account data"];
}
}
// FBSample logic
// handler for button click, logs sessions in or out
- (IBAction)buttonClickHandler:(id)sender {
// get the app delegate so that we can access the session property
SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
// this button's job is to flip-flop the session from open to closed
if (appDelegate.session.isOpen) {
// if a user logs out explicitly, we delete any cached token information, and next
// time they run the applicaiton they will be presented with log in UX again; most
// users will simply close the app or switch away, without logging out; this will
// cause the implicit cached-token login to occur on next launch of the application
[appDelegate.session closeAndClearTokenInformation];
} else {
if (appDelegate.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
appDelegate.session = [[FBSession alloc] init];
}
// if the session isn't open, let's open it now and present the login UX to the user
[appDelegate.session openWithBehavior:FBSessionLoginBehaviorForcingWebView
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// this handler is called back whether the login succeeds or fails; in the
// success case it will also be called back upon each state transition between
// session-open and session-close
[self updateView];
}];
}
}
そして、別のView Controllerで、次のようにクライアントページを開いています:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.facebook.com/abc"]]];