ShareSDK的使用

ShareSDK是cocoaPods管理的,这是以QQ分享为例,Podfile代码如下:



我使用的是‘MobShareSDK’,其他shareSDK都是一样使用,需要导入三个模块,‘MobShareSDK/UI/iPhoneDefault’是UI界面,选择的是iPhone,‘MobShareSDK/Connection/QQ’是分享平台,这里选择QQ;安装好了之后就是配置工程


一.配置工程


(1)bitcode

iOS 9新建项目默认需要支持bitcode,而不支持bitcode的SDK会导致无法编译运行。

不支持 bitcode 的 SDK 解决方案:

1、暂时关闭对bitcode的支持(建议),方法如下图



2、移除不支持bitcode的平台SDK


(2)添加Scheme白名单

在iOS 9下涉及到平台客户端跳转,系统会自动到项目info.plist下检测是否设置平台Scheme。对于需要配置的平台,如果没有配置,就无法正常跳转平台客户端。因此要支持客户端的分享和授权等,需要配置Scheme名单,下图是QQ需要配置的白名单,其他平台请参照如下网址:点击打开链接

注意:白名单一定要填写正确,不然会出一些你意想不到的错,比如之前把wtloginmaqq2后面多加了一个‘,’号,结果在qq上分享时图片显示不出来,微信,微博都能正常显示,就是QQ弄死都不出来,把我郁闷的,找了半天就是找不到哪里错了,结果后面添加其他白名单时发现这个白名单有点不对劲,后面多了个‘,’号,去掉之后一切都正常了,坑啊,小伙伴们加的时候注意一下



(3)添加URL Schemes

在URL Types中添加QQ的AppID,其格式为:”QQ” + AppId的16进制(如果appId转换的16进制数不够8位则在前面补0,如转换的是:5FB8B52,则最终填入为:QQ05FB8B52 注意:转换后的字母要大写),转换16进制的方法,在终端输入:echo ‘ibase=10;obase=16;801312852′ |bc,其中801312852为QQ的AppID


其他平台如何添加请参照如下网址:点击打开链接


二.代码

(1)注册应用

在AppDelegate.m里导入相关头文件,其他平台头文件可以参照上面链接里面的相关内容

#import <ShareSDK/ShareSDK.h>
//腾讯开放平台(对应QQ和QQ空间)SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>


在application: didFinishLaunchingWithOptions:方法里注册应用

//应用Key,在ShareSDK官网中注册的应用Key
[ShareSDK registerApp:@"你的应用Key"];

//连接QQ应用以使用相关功能
[ShareSDK connectQQWithQZoneAppKey:@"应用Key" qqApiInterfaceCls:[QQApiInterface class] tencentOAuthCls:[TencentOAuth class]];


self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

ShareViewController *VC = [[ShareViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];
self.window.rootViewController = nav;

return YES;
}


检查是否已加入handleOpenURL的处理方法,如果没有则添加如下代码:

- (BOOL)application:(UIApplication *)application
handleOpenURL:(NSURL *)url
{
return [ShareSDK handleOpenURL:url
wxDelegate:nil];
}

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];
}


(2)分享代码

导入头文件

#import <ShareSDK/ShareSDK.h>


分享按钮点击事件

-(void)shareButtonEvent:(UIButton*)sender{
//1、构造分享内容
//1.1、要分享的图片(以下分别是网络图片和本地图片的生成方式的示例)
id<ISSCAttachment> remoteAttachment = [ShareSDKCoreService attachmentWithUrl:[ImageURL stringByAppendingString:_infoDic[@"image"]]];
// id<ISSCAttachment> localAttachment = [ShareSDKCoreService attachmentWithPath:[[NSBundle mainBundle] pathForResource:@"分享图片" ofType:@"png"]];
//1.2、以下参数分别对应:内容、默认内容、图片、标题、链接、描述、分享类型
id<ISSContent> publishContent = [ShareSDK content:@"分享内容" 
                                   defaultContent:@"默认内容"
                                            image:remoteAttachment 
                                            title:@"ShareSDK" 
                                              url:@"http://www.baidu.com"
                                      description:@"描述" 
                                        mediaType:SSPublishContentMediaTypeNews];
//1.3、创建弹出菜单容器(iPad应用必要,iPhone应用非必要)
id<ISSContainer> container = [ShareSDK container];
[container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
//1.4、 弹出分享菜单
[ShareSDK showShareActionSheet:container 
                     shareList:ShareTypeQQ 
                       content:publishContent 
                 statusBarTips:YES        
                   authOptions:nil 
                  shareOptions:nil 
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) { 
                if (state == SSResponseStateSuccess)
                {
                  NSLog(@"分享成功");
                } else if (state == SSResponseStateFail)
                { NSLog(@"分享失败,错误码:%ld,错误描述:%@", [error errorCode], [error errorDescription]);
                }
               }];

简单的分享就完成了


(3)自定义分享

有时候你觉得ShareSDK弹出的框不好看,那就自己写,建一个UIView,上面加上Button,设置QQ,新浪微博或者微信的图片,把按钮的tag设置成相应的类型(比如ShareTypeQQ == 24,就把tag值设置成24),只是调用的方法不同了而已,只需要把上面1.3去掉不用,1.4换成另外一个方法,代码如下

分享按钮点击事件

-(void)shareButtonEvent:(UIButton*)sender{
//1、构造分享内容
//1.1、要分享的图片(以下分别是网络图片和本地图片的生成方式的示例)
id<ISSCAttachment> remoteAttachment = [ShareSDKCoreService attachmentWithUrl:[ImageURL stringByAppendingString:_infoDic[@"image"]]];
// id<ISSCAttachment> localAttachment = [ShareSDKCoreService attachmentWithPath:[[NSBundle mainBundle] pathForResource:@"分享图片" ofType:@"png"]];

//1.2、以下参数分别对应:内容、默认内容、图片、标题、链接、描述、分享类型
id<ISSContent> publishContent = [ShareSDK content:@"分享内容" 
                                   defaultContent:@"默认内容"
                                            image:remoteAttachment 
                                            title:@"ShareSDK" 
                                              url:@"http://www.baidu.com"
                                      description:@"描述" 
                                        mediaType:SSPublishContentMediaTypeNews];

//1.3、 弹出分享菜单(把上面的1.4方法改成这个方法就行了)
[ShareSDK showShareViewWithType:sender.tag container:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id statusInfo, id error, BOOL end) {
                                              if (state == SSResponseStateSuccess)
                                               {
                                                NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
                                               }
                                              else if (state == SSResponseStateFail)
                                               {
                                                 NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描     述:%@"), [error errorCode], [error errorDescription]);
                                                }
                                               }];

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值