2012-02-17 72 views
0

我必須說我現在感覺像個白癡。 :)我一直在Facebook,谷歌和StackOverflow上下,仍然無法得到我做錯了什麼的答案! :)我看了兩個Facebook示例:HackbookWishListWishlist顯然是應該告訴我該怎麼做的人,但是全部我見過的例子有OBJECT部分作爲URL。我不一定希望這樣,因爲我只想讓帖子說(此用戶)正在玩[MyGame]FBConnect iOS SDK publish_action問題

好的,這是我的目標。我有一款iPhone遊戲。我想要做什麼,當你聽一首歌,時間軸和股票的帖子。我也想用它來將玩家的得分發布在用戶的時間軸和代碼中。

我使用名爲Play的動作和名爲Game的對象及其聚集器來設置Open Graph。我想我也需要一個叫做Score的動作?

無論如何,我可以使用feed對話框成功發佈到用戶的牆上,但這不是我想要的Play操作。

這裏是什麼,我走到這一步,一個簡潔的版本,任何的幫助深表感謝:

夫婦注: 我有一個單身FacebookInfo這需要處理Facebook的代表和東西照顧。我也有一個FacebookUser類,它保存當前用戶的當前會話信息,當調用me時填充。我也有一個DLog方法,它只在調試模式下執行NSlog。

當用戶點擊我遊戲中的Play按鈕時,我想在下面調用我的方法[[Facebook sharedInfo] publishAction:@"play"]。我通過NSString作爲動作,因此稍後我可以調用相同的方法並使用類似Score的動作,並相應地修改該帖子。

@interface FacebookInfo : NSObject { 
    Facebook *_facebook; 
    FacebookUser *_facebookUser; 
} 

@property (nonatomic, retain) Facebook *facebook; 
@property (nonatomic, retain) FacebookUser *facebookUser; 

+(id)sharedInfo; 
-(BOOL)isFacebookAuthenticated; 
-(void)fbDidLogout; 
-(void)getMe; 
-(void)publishFeed; 
-(void)publishWithAction:(NSString *)action; 

@end
static FacebookInfo *facebookInfo = nil; 


@implementation FacebookInfo 


@synthesize facebook = _facebook; 
@synthesize facebookUser = _facebookUser; 


#pragma mark - Custom Methods 


-(void)getMe { 
    DLog(@"**********"); 
/* when forcing FBConnect to show inline dialog instead of using SSO, this works. 
apparently this fails when using SSO, error: 
Err message: (null) 
Err code: 10000 
*/ 
    [[self facebook] requestWithGraphPath:@"me" andDelegate:self]; 
} 


-(void)publishWithAction:(NSString *)action { 
    DLog(@"**********"); 

    if ([action isEqualToString:@"play"]) { 
     // Build the params list 
     NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:1]; 
     // all sample have this pointing to a URL. Do i really need to do that? 
     [params setValue:kFBAppNameSpace forKey:@"game"]; 
     // I know I may need more parameters, but which are required? 
     // Do I need to add the one's that Facebook Javascript examples have, 
     // like title, description? I think it's here where I'm mostly confused. 

     // Make the Graph API call to add to the wishlist 
     [[self facebook] requestWithGraphPath:@"me/[myappnamespace]:play" 
           andParams:params 
          andHttpMethod:@"POST" 
           andDelegate:self]; 

     [params release]; 
    } 
} 


-(void)publishFeed { 
    DLog(@"**********"); 
    /* 
This works perfectly fine 
*/ 
    SBJSON *jsonWriter = [[SBJSON new] autorelease]; 

    NSDictionary *actionLinks = [NSArray arrayWithObjects: 
           [NSDictionary dictionaryWithObjectsAndKeys: 
            @"Get The App",@"name", 
            kFBAppURL,@"link", 
            nil], 
           nil]; 

    NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks]; 

    NSString *app_id = kFBAppID; 
    NSString *user_message_prompt = [NSString stringWithFormat:@"Post to Your Wall!"]; 
    NSString *name = [NSString stringWithFormat:@"[MyGameName]"]; 
    NSString *caption = [NSString stringWithFormat:@"%@ has gotten a score of %@!",[[self facebookUser] firstName],[[[GameInfo sharedGameInfo] scoreTotal] stringValue]]; 
    NSString *description = [NSString stringWithFormat:@"Can you beat this score?!"]; 
    NSString *link = kFBAppURL; 
    NSString *picture = kFBAppImage; 

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
            app_id, @"app_id", 
            user_message_prompt, @"user_message_prompt", 
            name, @"name", 
            caption, @"caption", 
            description, @"description", 
            link, @"link", 
            picture, @"picture", 
            actionLinksStr, @"actions", 
            nil]; 

    [[self facebook] dialog:@"feed" 
        andParams:params 
       andDelegate:self]; 
} 


-(BOOL)checkForPreviousAccessToken { 
    DLog(@"**********"); 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) { 
     DLog(@"FB: Token Exists!"); 
     [[self facebook] setAccessToken:[defaults objectForKey:@"FBAccessTokenKey"]]; 
     [[self facebook] setExpirationDate:[defaults objectForKey:@"FBExpirationDateKey"]]; 
    } 
    if (![[self facebook] isSessionValid]) { 
     DLog(@"FB: Authorizing..."); 
     NSArray *permissions = [[NSArray alloc] initWithObjects: 
           @"publish_stream", 
           @"publish_actions", 
           @"offline_access", 
           nil]; 
     [[self facebook] authorize:permissions]; 
     [permissions release]; 
    } else { 
     DLog(@"FB: Authorized!!!"); 
     // show logged in 
     [self getMe]; 
    } 

    return [[self facebook] isSessionValid]; 
} 


-(BOOL)isFacebookAuthenticated { 
    DLog(@"**********"); 

    return [self checkForPreviousAccessToken]; 
} 


-(void)extendAccessTokenIfNeeded { 
    DLog(@"**********"); 

    [[self facebook] extendAccessTokenIfNeeded]; 
    [[FacebookInfo sharedInfo] getMe]; 
} 


-(void)logout { 
    DLog(@"**********"); 

    [[self facebook] logout:self]; 
} 


#pragma mark - FBConnect Delegate Methods 

-(void)fbDidLogin { 
    DLog(@"**********"); 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[[self facebook] accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[[self facebook] expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 

    [self getMe]; 
} 


- (void)fbDidNotLogin:(BOOL)cancelled { 
    DLog(@"**********"); 


} 


- (void)fbDidExtendToken:(NSString*)accessToken expiresAt:(NSDate*)expiresAt { 
    DLog(@"**********"); 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:accessToken forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:expiresAt forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 
} 


-(void)fbDidLogout { 
    DLog(@"**********"); 

    // Remove saved authorization information if it exists 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if ([defaults objectForKey:@"FBAccessTokenKey"]) { 
     [defaults removeObjectForKey:@"FBAccessTokenKey"]; 
     [defaults removeObjectForKey:@"FBExpirationDateKey"]; 
     [defaults synchronize]; 
    } 
} 


- (void)fbSessionInvalidated { 
    DLog(@"**********"); 


} 


#pragma mark - FBRequestDelegate Methods 

/** 
* Called when the Facebook API request has returned a response. This callback 
* gives you access to the raw response. It's called before 
* (void)request:(FBRequest *)request didLoad:(id)result, 
* which is passed the parsed response object. 
*/ 
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response { 
    DLog(@"**********"); 

    //DLog(@"received response"); 
} 

/** 
* Called when a request returns and its response has been parsed into 
* an object. The resulting object may be a dictionary, an array, a string, 
* or a number, depending on the format of the API response. If you need access 
* to the raw response, use: 
* 
* (void)request:(FBRequest *)request 
*  didReceiveResponse:(NSURLResponse *)response 
*/ 
- (void)request:(FBRequest *)request didLoad:(id)result { 
    DLog(@"**********"); 
    //code removed for this example 
} 

/** 
* Called when an error prevents the Facebook API request from completing 
* successfully. 
*/ 
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error { 
    DLog(@"**********"); 

    DLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]); 
    DLog(@"Err code: %d", [error code]); 
    if ([error code] == 190) { 
     // logout 
    } else { 
     DLog(@"There was an error making your request."); 
    } 
} 


#pragma mark - Singleton Methods 


+ (id)sharedInfo { 
    //DLog(@"**********"); 

    @synchronized(self) { 
     if(facebookInfo == nil) 
      facebookInfo = [[super allocWithZone:NULL] init]; 
    } 
    return facebookInfo; 
} 


+ (id)allocWithZone:(NSZone *)zone { 
    DLog(@"**********"); 

    return [[self sharedInfo] retain]; 
} 


- (id)copyWithZone:(NSZone *)zone { 
    DLog(@"**********"); 

    return self; 
} 


- (id)retain { 
    DLog(@"**********"); 

    return self; 
} 


- (unsigned)retainCount { 
    DLog(@"**********"); 

    return UINT_MAX; //denotes an object that cannot be released 
} 


- (oneway void)release { 
    DLog(@"**********"); 

    // never release 
} 


- (id)autorelease { 
    DLog(@"**********"); 

    return self; 
} 


- (id)init { 
    DLog(@"**********"); 

    if ((self = [super init]) != NULL) { 
     //Init 
     [self setFacebook:[[[Facebook alloc] initWithAppId:kFBAppID urlSchemeSuffix:kFBUrlSchemeSuffix andDelegate:self] autorelease]]; 
     [self setFacebookUser:[[[FacebookUser alloc] init] autorelease]]; 
    } 
    return self; 
} 


- (void)dealloc { 
    DLog(@"**********"); 

    // Should never be called, but just here for clarity really. 
    DLog(@"Release FacebookInfo..."); 
    [super dealloc]; 
} 


@end
@implementation AppDelegate_iPhone 

// Add for Facebook SSO support (4.2+) 
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 
    [[[FacebookInfo sharedInfo] facebook] handleOpenURL:url]; 
} 


// Add for Facebook SSO support (pre 4.2) 
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 
    [[[FacebookInfo sharedInfo] facebook] handleOpenURL:url]; 
} 


- (void)applicationDidBecomeActive:(UIApplication *)application { 
    DLog(@"**********"); 

    [[FacebookInfo sharedInfo] extendAccessTokenIfNeeded]; 
}

哇,我知道這是一個很長的帖子,但我希望有人能幫點我朝着正確的方向。

UPDATE 1:(2012/18/02)

好的。所以我決定做一個包含我不想做的og元數據的頁面,併爲我的對象提供url。按照該FB文檔,發現here

Open Graph Mechanics 

When users take an action in your app, such as cook the Stuffed Cookie, the app calls a Graph API to create a new cook action that connects the user with the Stuffed Cookie object. This is accomplished by issuing a HTTP POST to the user’s /me/myapp:cook connection with the URL of the recipe object. Facebook will then crawl the object URL, read the metadata, and connect the object to user's Graph via the action. 

The diagram below illustrates the process: 

User takes an action in the app, such as "cook" a "recipe" 
App calls a Graph API /me/action:object=Object_URL 
Facebook will crawl the object URL, read its meta tags and connects the object to the user's Graph via the action.

這將是冷靜,如果我們可以定義的應用程序本身這些東西作爲params用於在那裏,我不需要一個網站的情況。

+0

我想做同樣的事情,比如發佈操作而不創建帶有元標記的關聯網頁。所以,基本上,它只在*創建頁面後起作用。那是對的嗎 ? – vipinagg 2012-03-05 18:29:55

+0

這是正確的。截至目前,2012年3月9日,我只能通過創建一個頁面來完成此任務。 – RoLYroLLs 2012-03-10 02:27:02

回答

0

截至4/21/12 Facebook要求您創建一個頁面以獲取所需的數據。