系统自带分享格式:
需求:
通过IOS系统原生的分享集成Facebook 的应用邀请 功能。
前期准备:
配置Facebook分享的一些必要库到自己的工程,详细教程可以看看facebook官网
https://developers.facebook.com/docs/sharing/ios 国内用户需要翻墙哦!
下面介绍下必要的步骤供大家参考:
1.在Facebook 官网注册并关联你的app 具体地址:https://developers.facebook.com/docs/ios/getting-started/
2.下载并导入必要的.framework文件
3.配置你的项目.plist文件 看下几个关键点
4.在你的AppDelegate.m中加入以下代码:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
// Add any custom logic here.
return YES;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
];
// Add any custom logic here.
return handled;
}
-(void)applicationDidBecomeActive:(UIApplication*)application
{
[FBSDKAppEvents activateApp];
}
5.关键的一步 通过UIActivityViewController发起分享 看下参考代码:
- (void)showActivityViewController {
NSString *textToShare = @"logo";
UIImage* imageShare = [UIImage imageNamed:@""];
NSArray *activityItems = @[textToShare, imageShare];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAirDrop];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {//适配ipad
[activityViewController setModalPresentationStyle:UIModalPresentationFormSheet];
UIPopoverPresentationController *popPresenter = [activityViewController popoverPresentationController];
popPresenter.sourceView = self.view;
popPresenter.sourceRect = self.view.bounds;
popPresenter.permittedArrowDirections = 0;
}
[self presentViewController:activityViewController animated:YES completion:nil];
//分享结果回调方法
UIActivityViewControllerCompletionHandler myblock = ^(NSString *type,BOOL completed){
//new
NSLog(@"%d %@",completed,type);
if ([type isEqualToString:@"com.apple.UIKit.activity.PostToFacebook"]) {
[self inviteFBFriends];
}
};
activityViewController.completionHandler = myblock;
}
//new
-(void)inviteFBFriends
{
if (![FBSDKAccessToken currentAccessToken])
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"public_profile",@"email", @"user_photos"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
} else if (result.isCancelled) {
// Handle cancellations
} else if([FBSDKAccessToken currentAccessToken]){
[self shareLinkContent];
}
}];
return;
}else {
[self shareLinkContent];
}
}
//new
//分享链接
- (void)shareLinkContent {
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:@"https://www.jianshu.com/"];
[FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];
}
5.特别注意的地方:
其他可能出现的问题补充:
1.IOS11上分享总是不成功可以参考这篇文章:https://www.jianshu.com/p/fe4aaa54288f
2.白名单问题参考https://developers.facebook.com/docs/ios/ios9
注意: 国行的手机分享到facebook时是没有反应的哦
使用Branch分享方式:
配置:
1.在Branch配置相关APP信息https://docs.branch.io/pages/apps/ios/#share-message-dynamically
2.导入BranchSDK
3.代码
//这里使用的是BranchUniversalObject和BranchLinkProperties来配置分享的内容和自定义的信息 供前端和后台之间的数据调试
NSString *textShare = [NSString stringWithFormat:@"Come to see the design of my dream home, I need you to back me up."];
NSString *facebookShare = [[[ConfigManager sharedInstance] inviteFriendLink] stringByReplacingOccurrencesOfString:@"{{ID}}" withString:[UserManager sharedInstance].currentUser.userID];
NSString * userID = [[[UserManager sharedInstance] currentUser] userID];
BranchUniversalObject *buo = [[BranchUniversalObject alloc] init];
buo.publiclyIndex = YES;
buo.locallyIndex = YES;
buo.imageUrl = IMAGEURL;
buo.contentDescription = textShare;
buo.contentMetadata.customMetadata[kBranchDicParamsUserId] = userID;
buo.title = [NSString stringWithFormat:@"%@ on Homestyler",[UserManager sharedInstance].currentUser.getUserFullName];
BranchLinkProperties *lp = [[BranchLinkProperties alloc] init];
lp.feature = kBranchFeature;
[lp addControlParam:kBranchAddControliOS_URL withValue:facebookShare];
[buo showShareSheetWithLinkProperties:lp andShareText:textShare fromViewController:self completion:^(NSString* activityType, BOOL completed) {
HSMDebugLog(@"finished presenting");
}];
也可以使用下面的方式进行分享
BranchShareLink *shareLink = [[BranchShareLink alloc] initWithUniversalObject:buo linkProperties:lp];
shareLink.delegate = self;
[shareLink presentActivityViewControllerFromViewController:self anchor:nil];
#pragma mark - BranchShareLinkDelegate
- (void) branchShareLinkWillShare:(BranchShareLink*)shareLink {
shareLink.shareText = [NSString stringWithFormat:@"%@", shareLink.linkProperties.channel];
shareLink.title = [NSString stringWithFormat:@"%@ on Homestyler",[UserManager sharedInstance].currentUser.getUserFullName];
}
- (void) branchShareLink:(BranchShareLink*_Nonnull)shareLink
didComplete:(BOOL)completed
withError:(NSError*_Nullable)error{
if(error){
}
}