一、去Facebook开发者中心注册APP,获取APP ID https://developers.facebook.com
二、导入 FBSDKCoreKit.Framework, FBSDKLoginKit.Framework, FBSDKShareKit.Framework 三、在info.plist 文件中加入 <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>fb218334765200160</string> </array> </dict> </array> <key>FacebookAppID</key> <string>218334765200160</string> <key>FacebookDisplayName</key> <string>AirPlane</string> <key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fb-messenger-api</string> <string>fbauth2</string> <string>fbshareextension</string> </array> 四、设置bundle id 五、AppDelegate中设置 #import <FBSDKCoreKit/FBSDKCoreKit.h> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; return YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { [FBSDKAppEvents activateApp]; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]; } 六、控制器中设置 #import "ViewController.h" #import <FBSDKShareKit/FBSDKShareKit.h> #import <Social/Social.h> #import "UIToastUtil.h" @interface ViewController ()<FBSDKSharingDelegate> @property (nonatomic, strong) SLComposeViewController *mySLComposerSheet; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } /** 首先调用facebook客户端 */ -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self shareToFacebook]; } /** 有Facebook客户端 */ - (void)shareToFacebook{ FBSDKShareLinkContent *content1 = [[FBSDKShareLinkContent alloc] init]; content1.contentURL = [NSURL URLWithString:@"https://developers.facebook.com"]; content1.contentTitle=@"我就是分享"; // [FBSDKShareDialog showFromViewController:self withContent:content1 delegate:nil]; // [FBSDKMessageDialog showWithContent:content1 delegate:nil]; FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init]; dialog.fromViewController = self; dialog.delegate = self; [dialog setShareContent:content1]; dialog.mode = FBSDKShareDialogModeNative; [dialog show]; } #pragma mark -FBSHARE DELEGATE - (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results{ NSLog(@"--->有Facebook客户端,成功分享!"); } - (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error{ NSLog(@"--->分享失败!, %@", error); if(error==nil||[[NSNull null] isEqual:error]){ /** 没有facebook客户端,检查手机是否绑定账号 */ [self shareFacebook]; } } - (void)sharerDidCancel:(id<FBSDKSharing>)sharer{ NSLog(@"--->取消分享!"); } - (void)shareFacebook{ if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=6){ if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){ self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; // [self.mySLComposerSheet setInitialText:self.productDetailModel.productName];//产品名 // NSURL *imageUrl = [NSURL URLWithString:self.productDetailModel.primaryThumbimage]; // NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; // [self.mySLComposerSheet addImage:[UIImage imageWithData:imageData scale:1]];//产品图 [self.mySLComposerSheet addURL:[NSURL URLWithString:@"https://developers.facebook.com"]];//产品地址 [self presentViewController:self.mySLComposerSheet animated:YES completion:nil]; [MBProgressHUD hideHUDForView:self.view animated:YES]; }else{ /** 没有安装客户端,并且手机也没有绑定账号,使用网页分享 */ [self shareWithWeb]; } [self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) { NSString *output; switch (result) { case SLComposeViewControllerResultCancelled: output = @"点击取消分享"; break; case SLComposeViewControllerResultDone: output = @"点击分享"; break; default: break; } if ((result = SLComposeViewControllerResultCancelled)) { // [UIToastUtil showToast:self.view message:@"you have not install facebook app."]; // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:MBLocalizedString(kFacebookMessage) message:output delegate:nil cancelButtonTitle:MBLocalizedString(kFacebookOK) otherButtonTitles:nil]; // [alert show]; } }]; } } /** 没有Facebook客户端,手机Facebook也没有绑定账号,使用网页分享 */ - (void)shareWithWeb{ FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init]; content1.contentURL = [NSURL URLWithString:@"https://developers.facebook.com"]; content1.contentTitle[email protected]“这是分享"; FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init]; dialog.fromViewController = self; dialog.delegate = self; [dialog setShareContent:content]; dialog.mode = FBSDKShareDialogModeWeb; [dialog show]; }
时间: 2024-10-18 05:53:23