1,系统自带的分享
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) { NSLog(@"没有设置新浪微博的分享,请到设置里进行登录账户"); return; } SLComposeViewController * cvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo]; //设置分享文字内容与图片 [cvc setTitle:@"什么???"]; [cvc addImage:[UIImage imageNamed:@"IMG_0762.jpg"]]; //跳转到新浪分享界面
[self presentViewController:cvc animated:YES completion:nil];
}
sharedSDK
1.手动集成
1.将下载好的SDK拖入项目
2.添加需要的库
3.导入所需的头文件(在delegate.m中)
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//腾讯开放平台(对应QQ和QQ空间)SDK头文件#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//微信SDK头文件
#import "WXApi.h"
//新浪微博SDK头文件
#import "WeiboSDK.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ /** * 设置ShareSDK的appKey,如果尚未在ShareSDK官网注册过App,请移步到 登录后台进行应用注册 * 在将生成的AppKey传入到此方法中。 * 方法中的第二个第三个参数为需要连接社交平台SDK时触发, * 在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发,根据返回的平台类型来配置平台信息。 * 如果您使用的时服务端托管平台信息时,第二、四项参数可以传入nil,第三项参数则根据服务端托管平台来决定要连接的社交SDK。 */ [ShareSDK registerApp:@"iosv1101" activePlatforms:@[ @(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeMail), @(SSDKPlatformTypeSMS), @(SSDKPlatformTypeCopy), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeQQ), @(SSDKPlatformTypeRenren), @(SSDKPlatformTypeGooglePlus)] onImport:^(SSDKPlatformType platformType) { switch (platformType) { case SSDKPlatformTypeWechat: [ShareSDKConnector connectWeChat:[WXApi class]]; break; case SSDKPlatformTypeQQ: [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]]; break; case SSDKPlatformTypeSinaWeibo: [ShareSDKConnector connectWeibo:[WeiboSDK class]]; break; default: break; } } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) { switch (platformType)
{
// * SSO授权方式 不会弹出登录界面,跳转到应用中进行分享,只能使用真机测试// *///extern NSString *const SSDKAuthTypeSSO;///**// * 网页授权方式 弹出登录界面,输入账号和密码登录后才能分享// */
//extern NSString *const SSDKAuthTypeWeb;
case SSDKPlatformTypeSinaWeibo: //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权 [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243" appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3" redirectUri:@"" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeWechat: [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885" appSecret:@"64020361b8ec4c99936c0e3999a9f249"]; break; case SSDKPlatformTypeQQ: [appInfo SSDKSetupQQByAppId:@"100371282" appKey:@"aed9b0303e3ed1e27bae87c33761161d" authType:SSDKAuthTypeBoth]; break; default: break; } }]; return YES;
}
运行:出错
//崩了:[NSConcreteMutableData wbsdk_base64EncodedString]: unrecognized selector sent to instance 0x7f805aca5920'
//新浪微博SDK需要在项目Build Settings中的Other Linker Flags添加"-ObjC"
加了这个参数后,链接器就会把静态库中所有的
Objective-C
类和分类都加载到最后的可执行文件中 // 注意: 这里的O是大小,C也是大写
4.在控制器中添加头文件
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1、创建分享参数 NSArray* imageArray = @[[UIImage imageNamed:@"IMG_0506.JPG"]]; if (imageArray) { NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText:@"分享内容" images:imageArray url:[NSURL URLWithString:@""] title:@"分享标题" type:SSDKContentTypeAuto]; //2、分享(可以弹出我们的分享菜单和编辑界面) [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,只有传这个才可以弹出我们的分享菜单,可以传分享的按钮对象或者自己创建小的view 对象,iPhone可以传nil不会影响 items:nil shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { switch (state) { case SSDKResponseStateSuccess: { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alertView show]; break; } case SSDKResponseStateFail: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; break; } default: break; } } ];}
}
// 错误: [__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
//因为找不到那个图片,用demo的代码找不到那个图片资源,自己换一个就行。
效果图:
2.cocoaPod集成
podFile中
# 主模块(必须)
pod 'ShareSDK3'
# Mob 公共库(必须) 如果同时集成SMSSDK iOS2.0:可看此注意事项:http://bbs.mob.com/thread-20051-1-1.html
pod 'MOBFoundation'
# UI模块(非必须,需要用到ShareSDK提供的分享菜单栏和分享编辑页面需要以下1行)
pod 'ShareSDK3/ShareSDKUI'
# 平台SDK模块(对照一下平台,需要的加上。如果只需要QQ、微信、新浪微博,只需要以下3行)
pod 'ShareSDK3/ShareSDKPlatforms/QQ'
pod 'ShareSDK3/ShareSDKPlatforms/SinaWeibo'
pod 'ShareSDK3/ShareSDKPlatforms/WeChat'
# pod 'ShareSDK3/ShareSDKPlatforms/RenRen’ //人人
# pod 'ShareSDK3/ShareSDKPlatforms/AliPaySocial’ //支付宝
pod install即可
1. 注册Mob账号,按照文档进行集成2. 使用pod按照sharedSDK3. 拷贝文档上的相关代码4. 修改 registerApp:@"你在Mob上的注册的分享的AppKey"
5. 修改各个平台注册的应用信用信息
case SSDKPlatformTypeSinaWeibo://设置你在新浪微博应用信息,其中authType设置为使用SSO+Web形式授权[appInfo SSDKSetupSinaWeiboByAppKey:@"568898243" appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3" redirectUri:@"" authType:SSDKAuthTypeBoth];break;case SSDKPlatformTypeWechat://这里需要去微信开发者平台注册,才能获取得.[appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885" appSecret:@"64020361b8ec4c99936c0e3999a9f249"];break;//真实开发中这些都需要去QQ开放平台注册.QQ05FB8B52case SSDKPlatformTypeQQ:[appInfo SSDKSetupQQByAppId:@"100371282" appKey:@"aed9b0303e3ed1e27bae87c33761161d" authType:SSDKAuthTypeBoth];break;default:
break;
重点内容:
5. 修改分享的内容.6. SSO授权6.1 配置对应URLScheme 当分享成功之后,应用需要回跳到我们应用. 那么他们即必须有我的URLScheme.6.2 配置SCheme白单 iOS9之后,你要跳转到的应用必须在白名单中说明.常见错误处理1. 微信分享,不能跳转的微信应用程序 这是由于微信对图片有一定要求,jpg图片不能大于32K. 绝大多数的.jpg图片是不支持的,绝大多数png图片是支持的. 建议大家分享的时候,尽量使用png的图片
2. 新浪微博分享SSO授权报SSO授权错误.是由于新浪微博要求:你应用的bundleId与注册新浪微博使用bundleID必须一致. 也是就是说新浪微博要求:bundle id,新浪微博的注册的应用AppKey,appSecret,redirectUri还有配置URLScheme必须一致.
1. Web授权与SSO授权的区别 Web授权: 会弹出一个登陆界面,需要用户输入账号和密码登陆,后才能授权 SSO授权: 跳转到分享平台的App中,无需登陆,直接授权2. 你在公司做过分享功能吗? 有遇到什么印象深刻的问题吗? 1. 微信分享使用某些.jpg图片,导致打不开微信的App,无法进行分享,所以分享时候要使用png的图片. 2. 新浪微博SSO授权错误:1. 我们应用的bundle Id 与 在新浪微博开放平台上注册的App的时候写的 写bundle ID要一致 2. AppKey ,appSecret,redirectUri,URLSCheme要一致.3. 使用SharedSDK进行分享,如何配置SSO授权? 1. 配置SChemeURL: target->info->最下面那个选哪个选项中配置
2. 配置SChemeURL白名单