iOS 프로젝트에서 Facebook SDK를 구현하는 데 문제가 있습니다. UIActionSheet를 통해 내 응용 프로그램의 특정 섹션에서 호출하려고합니다. Facebook에 로그인 할 수 있지만 일단 내 앱으로 리디렉션되면 앱이 로그인되어 있지 않아 공유 기능을 실행할 수 없습니다.앱으로 돌아 가면 Facebook 로그인이 제대로 인식되지 않습니다.
나는 프로젝트 정보에서 "fbMY_APP_ID"을 추가 포함 https://developers.facebook.com/docs/mobile/ios/build/에서 페이스 북의 아이폰 OS 튜토리얼을 따라했지만 그것은 내가 로그인하고 있음을 인식하지 않습니다 이유를 알아낼 수 없습니다. 여기
가 관련 헤더 인 및 방법을 사용하고 있습니다.//Header
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import "MWFeedItem.h"
#import "FBConnect.h"
@interface DetailTableViewController : UITableViewController <UIAlertViewDelegate, UIActionSheetDelegate, UIApplicationDelegate, FBSessionDelegate>{
Facebook *facebook;
BOOL login;
}
@property (nonatomic, strong) Facebook *facebook;
@end
및 방법 : 편의를 위해 페이스 북의 구현과 관련이없는 항목을 삭제 한
// Method
#import "DetailTableViewController.h"
#import "NSString+HTML.h"
#import <Twitter/Twitter.h>
@implementation DetailTableViewController
@synthesize facebook;
//Navigation button on detail view does THIS:
- (void)navAction {
NSLog(@"Action sheet activated.");
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Like this story?"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Email link"
otherButtonTitles:@"Share on Facebook", @"Share on Twitter", @"Open in Safari", nil];
[actionSheet showInView:[self.view window]];
}
-(void)actionSheet: (UIActionSheet *) actionSheet willDismissWithButtonIndex:(NSInteger) buttonIndex{
///////THIS SECTION DEFINES THE EMAIL SHARE FUNCTION/////
if (buttonIndex == 0) {
// MFMailComposeViewController implementation
}
///////THIS SECTION DEFINES THE FACEBOOK SHARE FUNCTION/////
else {
if (buttonIndex == 1) {
if (login == NO){
NSLog(@"No login detected");
UIAlertView *infoAlert = [[UIAlertView alloc]
initWithTitle:@"Login necessary"
message:@"You must first login to your Facebook account to use this feature."
delegate:self
cancelButtonTitle:@"Login"
otherButtonTitles:@"Cancel", nil];
[infoAlert show];
}else {
if (login == YES){
NSLog(@"Login detected.");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationKey"];
[defaults synchronize];
//APPLY LOGIN ACTION HERE. Waiting until I can make the system run the NSLog above. So far, the login is not being correctly recorded.
}
}
}
///////THIS SECTION DEFINES THE TWITTER SHARE FUNCTION/////
else {
if (buttonIndex == 2) {
//Twitter implementation here
}
///////THIS SECTION DEFINES THE SAFARI FUNCTION/////
else{
if (buttonIndex == 3){
//Safari implementation here
}
}
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
// CALL TO FACEBOOK HERE
facebook = [[Facebook alloc] initWithAppId:@"MY_APP_ID" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]){
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]){
NSLog(@"Reached isSessionValid evaluation.");
[facebook authorize:nil];
}
}
else if (buttonIndex == 1)
{
NSLog(@"Pressed cancel button");
}
}
// THIS METHOD SUPPORTS IOS PRE 4.2
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [facebook handleOpenURL:url];
}
// THIS METHOD SUPPORTS IOS AFTER 4.2
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [facebook handleOpenURL:url];
}
////////////////////////////////////////////
- (void)fbDidNotLogin:(BOOL)cancelled{
}
- (void)fbDidExtendToken:(NSString*)accessToken
expiresAt:(NSDate*)expiresAt{
}
- (void)fbDidLogout{
}
- (void)fbSessionInvalidated{
}
- (void) fbDidLogin {
NSLog(@"Logged in with Facebook credentials");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationKey"];
[defaults synchronize];
//APPLY LOGIN ACTION HERE. Waiting until I can make the system run the NSLog above. So far, the login is not being correctly recorded.
}
추가 정보 :
나는 또한 주에의 OpenURL 부분을 추가하려고했습니다 응용 프로그램 위임 파일이지만 동일한 결과가있었습니다.
공유 방법은 어디에 있습니까? 어디에서 불러? –
당분간 제거했습니다. 현재 내가하려고하는 것은 앱이 NSLog 활동을 시청하여 로그인했음을 인식하도록하는 것입니다. 필자는 로그인 한 후 fbDidLogin 메서드가 호출되어야하는 앱으로 리디렉션 된 것으로 생각했습니다. 나는 틀린가? –
매번 fbDidLogin : 메서드를 호출하지 않습니다. 이 메소드는 사용자 기본값에 토큰 및 만료 날짜가 없거나 사용자 토큰에 토큰 및 만기 날짜가 유효하지 않은 경우에만 호출됩니다. –