2015-04-07 37 views
3

我想使用新的iOS 4.0 SDK在Facebook上顯式共享開放圖形動作。以下不起作用。它顯示在我的活動日誌上,但不在我的時間軸上。在Facebook上進行顯式共享iOS SDK 4.0

// Construct an FBSDKSharePhoto 
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init]; 
photo.image = img; 
photo.userGenerated = YES; 

// Create an object 
NSDictionary *properties = @{ 
          @"og:type": @"theprose:post", 
          @"og:title": post.title, 
          @"og:description": post.text, 
          @"og:caption": @"A fresh picked Prose for you.", 
          @"og:url": post.url, 
          @"fb:explicitly_shared": @"true" 
          }; 
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties]; 

// Create an action 
FBSDKShareOpenGraphAction *act = [[FBSDKShareOpenGraphAction alloc] init]; 
act.actionType = @"theprose:share"; 
[act setObject:object forKey:@"theprose:post"]; 
[act setPhoto:photo forKey:@"image"]; 

// Create the content 
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init]; 
content.action = act; 
content.previewPropertyName = @"theprose:post"; 

[[FBSDKShareAPI shareWithContent:content delegate:nil] share]; 
+1

你有沒有解決過這個問題?我遇到了同樣的問題。 – sak

+0

我在swift 3有同樣的問題..你能在這裏發佈最新的代碼嗎? – Jan

回答

9

拼湊從這個頁面的答案,這是對我工作:

1.檢查明確共享BOX

轉到您打開的圖形設置,操作類型,然後選擇您的自定義操作。向下滾動到功能部分,並確保選中「顯式共享」框。

Capabilities Settings

2.設置FB:explicitely_shared在行動

[action setString:@"true" forKey:@"fb:explicitly_shared"]; 

3套動作&客體關係的

確保您知道正確的連接你的行爲對你的對象。在這種情況下,關鍵是「文章」:

[FBSDKShareOpenGraphAction actionWithType:@"mynamespace:share" object:object key:@"article"] 

您可以找到通過查看您在FB應用程序的開放圖譜設置創建的動作類型是關鍵。當您通過Story將Action與Ob​​ject連接起來時,該關係的新鍵將出現在Action的頁面下的Property Name列中。

當您真正需要的只是該鍵的property_name時,原始海報正在使用namespace:property_name格式。這可能是OP的修復。

4.設置委託,查找錯誤

的OP未服用的FBSDKSharingDelegate功能。它報告了錯誤,會幫助您解決問題:

[[FBSDKShareAPI shareWithContent:content delegate:self] share]; 

和實施自我的委託方法:

#pragma mark - FBSDKSharingDelegate 

- (void) sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results { 

    NSLog(@"Facebook sharing completed: %@", results); 
} 

- (void) sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error { 

    NSLog(@"Facebook sharing failed: %@", error); 
} 

- (void) sharerDidCancel:(id<FBSDKSharing>)sharer { 

    NSLog(@"Facebook sharing cancelled."); 
} 

僅供參考,這是我工作的代碼

// Create the object 
NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary: @{ 
                        @"og:type": @"mynamespace:article", 
                        @"og:title": myModel.title, 
                        @"og:description": @"myModel.description", 
                        @"og:url": @"http://mywebsite.com" 
                        }]; 


NSURL *imageURL = [myModel getImageURL]; 
if (imageURL) { 

    FBSDKSharePhoto *photo = [FBSDKSharePhoto photoWithImageURL:imageURL userGenerated:NO]; 
    [properties setObject:@[photo] forKey:@"og:image"]; 
} 

FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties]; 

// Create the action 
FBSDKShareOpenGraphAction *action = [FBSDKShareOpenGraphAction actionWithType:@"mynamespace:share" object:object key:@"article"]; 
[action setString:@"true" forKey:@"fb:explicitly_shared"]; 

// Create the content 
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init]; 
content.action = action; 
content.previewPropertyName = @"article"; 

// Share the content 
FBSDKShareAPI *shareAPI = [[FBSDKShareAPI alloc] init]; 
shareAPI.shareContent = content; 
shareAPI.delegate = self; 

[shareAPI share]; 
+0

我有基於你的代碼不起作用 - 調用[shareAPI share]不會導致任何委託方法被調用。我在這裏發佈了一個單獨的問題:http://stackoverflow.com/questions/33175479/ios-non-dialog-facebook-share-fails-with-no-errors-and-no-feedback - 任何想法,將不勝感激 – wheeliebin

+0

如果我已經有fb:explicit_shared,我是否需要請求publish_actions權限? – TomSawyer

+0

swift 3版請 – Jan

1
返回錯誤的機會很高

同樣的問題,但在FBSDKShareDialog下使用相同的帳戶正常工作。所以問題在於別的。

+0

同樣在這裏,我最終也只是使用了FBSDKShareDialog,但我並不喜歡應用程序切換流程。感覺笨重。 – CaptainStiggz

0

轉到您打開的圖形設置,操作類型,然後選擇您的自定義操作。向下滾動到功能部分,並確保選中「顯式共享」框。

enter image description here

1

我得到了同樣的問題。我的意思是你的代碼中的原因是,你在對象中設置了fb:clearly_shared屬性,而不是在關聯動作中。

以下代碼應該解決問題。

[action setString:@"true" forKey:@"fb:explicitly_shared"]; 
+0

我已經添加了,但它表示它需要'publish_actions' – TomSawyer

0

一更多檢查清單:FBSDKShareApi只適用於主線程。所以請確保你從那裏分享。