我按照https://github.com/facebook/react-native-fbsdk上編寫的指南安裝並使用react-native-fbsdk模塊。當我運行應用程序實施react-native-fbsdkshare
'Error: undefined is not an object (evaluating \'FBSDKShareDialogInterface.show\')
而且3個警告顯示:然而,當我點擊「分享」按鈕,它引發以下錯誤
「警告:‘RCTFBSDKShareButton’不存在天然成分」 「警告:‘RCTFBSDKSendButton’本地組件不存在」 「警告:本機組件,用於‘RCTFBSDKLikeControl’不存在」
這是我使用的代碼:
VAR˚F BSDKShare = require('react-native-fbsdkshare');
變種{ FBSDKShareDialog, FBSDKShareDialogInterface, FBSDKShareLinkContent, FBSDKShareShareButton } = FBSDKShare;
shareOnFacebook() {
var linkContent = new FBSDKShareLinkContent('https://facebook.com', 'Wow, check out this great site!', 'Facebook.com', null);
// Share the link using the native share dialog.
FBSDKShareDialog.show(linkContent, (error, result) => {
if (!error) {
if (result.isCancelled) {
alert('Share cancelled.');
} else {
alert('Thanks for sharing!');
}
} else {
alert('Error sharing.');
}
});
<TouchableHighlight style={[itemStyles.itemButtonShare,itemStyles.itemButtonShareFacebook]} onPress={() => this.shareOnFacebook(item)}><Text>Share On Facebook</Text></TouchableHighlight>
(我FBSDKCoreKit.framework,FBSDKLoginKit.framework和FBSDKShareKit.framework已經安裝和框架的搜索路徑設置。) 任何人都可以用這個錯誤幫助嗎? 感謝
----- EDITED -------
好最後我決定創建一個本地方法,並從側面反應調用它。如果有人面對這一問題,這是替代的解決方案:
創建2個文件,FacebookShareManager.h和FacebookShareManager.m:
FacebookShareManager.h的含量應爲:
#import "RCTBridgeModule.h"
@interface FacebookShareManager : NSObject <RCTBridgeModule>
@end
和內容在FacebookShareManager.m的:
#import "FacebookShareManager.h"
#import "FBSDKCoreKit/FBSDKCoreKit.h"
#import "FBSDKShareKit/FBSDKShareKit.h"
@implementation FacebookShareManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(showDialog: (NSDictionary *)params callback:(RCTResponseSenderBlock)callback) {
dispatch_async(dispatch_get_main_queue(), ^{
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
NSURL * shareUrl = [NSURL URLWithString:params[@"contentUrl"]];
NSURL * shareImageUrl = [NSURL URLWithString:params[@"contentImageUrl"]];
NSString * shareTitle = [NSString stringWithString:params[@"contentTitle"]];
NSString * shareDescription = [NSString stringWithString:params[@"contentDescription"]];
content.contentURL = shareUrl;
content.imageURL = shareImageUrl;
content.contentTitle = shareTitle;
content.contentDescription = shareDescription;
[FBSDKShareDialog showFromViewController:nil
withContent:content
delegate:nil];
});
};
@end
現在我們需要從我們的反應,本地組件調用此方法:
首先包括經理您的反應本地組件類文件的頂部:
var FacebookShareManager = require('NativeModules').FacebookShareManager;
現在裏面你的組件類添加這個方法:
shareOnFacebook(item) {
var params = {"contentUrl": item.url,"contentImageUrl": item.pictureUrl,"contentTitle": item.title,"contentDescription":item.description};
FacebookShareManager.showDialog(params, (info)=> {
// do something you need...
});
}
您是否已經在XCode中清理並重建應用程序,並且在安裝該組件後重新啓動了React Native Packager進程? – jevakallio
是的,我已經多次重新啓動打包程序。重建項目多次。沒有什麼變化@jevakallio – meteorite
不幸的是,我不得不從反應方創建一個本地方法來調用: – meteorite