iOS中facebook与twitter分享问题总结

来源:互联网 发布:人工智能 呼叫中心 编辑:程序博客网 时间:2024/06/09 21:19
移动分享是一个很普遍常见的问题,其中针对国外的app经常用到的无非这两个。

以下是解决方案。
1.我也极力推荐的一个方案,ShareSDK,这个是中国自己的一个第三方集成的分享模块,里面的功能很强大,包括目前所有的客户端的分享。只要在官网上下载sharesdk的包,然后阅读相关的文档,依样画葫芦就行了。把包导入到工程里面,注意路径的配置,然后注册facebook和twitter的APIkey,然后是在ShareSDK官网上注册appKey进行绑定。最后是代码里面
 1.1在appdelegate.m文件里面注册
#pragma mark -- SSO授权本地初始化SDK
+ (void)initializePlatWithSSO
{
    [ShareSDK registerApp:@"ebc7ea75b14"];
    
    //twitter
    [ShareSDK connectTwitterWithConsumerKey:@"WhdXQJq8TVnNtU9eyoCxQ"
                             consumerSecret:@"YY1975oq70MGojGXKuTxbgINXSrikfMfQDhSLr04"
                                redirectUri:@"http://website.com/"];
    
    //facebook
    [ShareSDK connectFacebookWithAppKey:@"703090989701898"
                              appSecret:@"59186b061caf72a34c7e5860cb65b745"];
}

然后- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [ShareSDK handleOpenURL:url
                 sourceApplication:sourceApplication
                        annotation:annotation
                        wxDelegate:self];
    
}
在要使用的类里面,引入头文件。
- (void)shareBySimpleway:(ShareType)type andContent:(NSString *)shareContent
{
//    NSString *imagePath = [[NSBundle mainBundle] pathForResource:IMAGE_NAME ofType:IMAGE_EXT];
    id<ISSShareOptions> simpleShareOptions =
    [ShareSDK simpleShareOptionsWithTitle:RBTLocalizedString(@"25_setting_contentShare", @"内容分享")
                        shareViewDelegate:nil ];
    
    id obj = nil;
    if(self.musicType == ShareMusicByFacebook)
    {
        obj = [ShareSDK imageWithUrl:self.shareImgStr];
    }
    
    if (self.musicType == ShareMusicByTwitter || m_shareType == ShareTypeClient || m_shareType == ShareTypePlayList) {
        obj = nil;
    }
    
    id<ISSContent> publishContent = [ShareSDK content:shareContent defaultContent:@"Greetings!"
                                                image:obj
                                                title:@"ShareSDK"
                                                  url:@"http://www.sharesdk.cn"
                                          description:@"Music shared!"
                                            mediaType:SSPublishContentMediaTypeNews];
    
    [ShareSDK showShareViewWithType:type container:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:simpleShareOptions result:^(ShareType type, SSPublishContentState state, id<ISSStatusInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        if (error != nil)
        {
            FXTRACE(FXT_DEBUG_LVL, @"Share", @"errorcode:%d errorDes:%@", [error errorCode], [error errorDescription]);
            [Utils showToast:[error errorDescription]];
        }
    }];
}

基本完成了,ShareMusicByTwitter和facebook是自定义的,原版的是ShareTypeFacebook和ShareTypeTwitter.

方式2:webview的形式这种很简单,
分享到facebook:
 self.musicShareStr = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?m2w&s=100&p[title]=%@&p[summary]=%@&p[url]=%@&p[images][0]=%@",self.shareName,@"",urlString,self.shareImgStr];
分享到twitter:
self.musicShareStr = [NSString stringWithFormat:@"https://twitter.com/intent/tweet?&text=%@&url=%@",self.smsContent,urlStr1];

这种形式记住要清除缓存:参考http://wbqingheng.blog.163.com/blog/static/179383760201402073948573/

方式3:使用Facebook官方sdk分享
就是一般的方式了,有官方的sdk包,在plist文件配置FacebookAppID和FacebookDisplayName,然后在appdelegate里面配置
在didfinishload里面加入    [FBAppEvents activateApp];
    [FBAppCall handleDidBecomeActive];
在- (void)applicationWillTerminate:(UIApplication *)application
{
    // FBSample logic
    // if the app is going away, we close the session object
    [FBSession.activeSession close];
}
在使用的类里面配置:
    // Show the feed dialog
    [FBWebDialogs presentFeedDialogModallyWithSession:nil
                                           parameters:[self setFacebookShareContent]
                                              handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
     {
          if (error)
          {
              // An error occurred, we need to handle the error
              // See: https://developers.facebook.com/docs/ios/errors
              FXTRACE(FXT_ERROR_LVL, @"Share", @"Share to Facebook error:%@",
                      error.description);
              [Utils showToast:RBTLocalizedString(@"25_setting_share_failed", @"failed")];
          }
          else
          {
              if (result == FBWebDialogResultDialogNotCompleted)
              {
                  // User cancelled.
                  FXTRACE(FXT_INFO_LVL, @"Share", @"User cancel shareing");
              }
              else
              {
                  // Handle the publish feed callback
                  NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
                  if (![urlParams valueForKey:@"post_id"])
                  {
                      // User cancelled.
                      FXTRACE(FXT_INFO_LVL, @"Share", @"User cancel shareing");
                  } else {
                      // User clicked the Share button
                      FXTRACE(FXT_INFO_LVL, @"Share", @"Share success, story id:%@",
                              [urlParams valueForKey:@"post_id"]);
                      [Utils showToast:RBTLocalizedString(@"25_setting_share_success", @"success")];
                  }
              }
          }
    }];

- (NSMutableDictionary *)setFacebookShareContent
{
    ConfigManager *configMg = [ConfigManager sharedInstance];
    NSMutableDictionary *shareFacebookDic=nil;
    if ([self.titleS isEqualToString:NSLocalizedString(@"Share", @"")])
    {
        shareFacebookDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:configMg.shareClientAppToFbTextStr, @"name",
                                             configMg.shareClientAppToFbUrlStr,@"link", nil];
    }
    else
    {
        shareFacebookDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:configMg.shareClientAppTextStr, @"name", nil];
    }
    
    if (!shareFacebookDic)
    {
        shareFacebookDic =  [NSMutableDictionary dictionaryWithObjectsAndKeys:RBTLocalizedString(@"default_share_Social", @"ShareFacebook"), @"name", nil];
    }
    return shareFacebookDic;
}
要配置这个分享的dic。

基本就这三种方式。
0 0
原创粉丝点击