关于分享,估计很多都是用ShareSDK的社交分享,简单方便,支持的类型也多,但一般以QQ、微信、新浪微博、腾讯微博为主.最近需要导入一个分享模块。几次失败后,我终于成功了。分享给大家,让大家少走一些弯路,让大家更快的解决IOS分享的问题。步骤如下:首先下载ShareSDK。将项目导入需要的一些库文件导入到各个开放平台,注册自己的APPKey,创建自己的应用。最后到项目中为各个平台配置AppKey等。进入正文:1.注册一个ShareSDK账号,然后创建自己的应用(需要APPKey),下载ShareSDK:http://sharesdk.mob.com/#/sha...,然后解压导入到自己的项目,接下来就是按照官网添加额外的依赖库,剩下的就是如何实现共享功能了。2、注册各个平台的AppKey,首先要注册一个账号,然后创建一个应用,找到对应的AppKey。以下是三个最常用平台的链接。3.在代码中注册ShareSDK和各种平台。首先导入头文件#import#import#import#import#import"WXApi.h"#import"WeiboSDK.h"然后在下面的方法中添加这段代码(BOOL)application:(UIApplication)applicationdidFinishLaunchingWithOptions:(NSDictionary)启动选项;在这个方法中添加这段代码即可(AppKey需要填写自己的新浪微博和回调地址)。代码如下:[ShareSDKregisterApp:@"你注册的ShareSDKAppKey"activePlatforms://共享平台,这里只有新浪微博、QQ、微信@[@(SSDKPlatformTypeSinaWeibo),@(SSDKPlatformTypeWechat),@(SSDKPlatformTypeQQ)]onImport:^(SSDKPlatformTypeplatformType){switch(platformType){//这里和上面的一一对应case一样类]];休息;caseSSDKPlatformTypeQQ:[ShareSDKConnectorconnectQQ:[QQApiInterfaceclass]tencentOAuthClass:[TencentOAuthclass]];break;default:break;}}onConfiguration:^(SSDKPlatformTypeplatformType,NSMutableDictionary*appInfo){switch(platformType){//这里是一样的以上一一对应案例SSDKPlatformTypeSinaWeibo://设置新浪微博应用信息,其中authType设置为使用SSO+Web表单授权[appInfoSSDKSetupSinaWeiboByAppKey:@"新浪微博的AppKey"appSecret:@"新浪微博的Secret"redirectUri:@"回调地址"authType:SSDKAuthTypeBoth];break;caseSSDKPlatformTypeWechat://设置微信应用信息[appInfoSSDKSetupWeChatByAppId:@"WeChatAppId"appSecret:@"WeChatSecret"];break;caseSSDKPlatformTypeQQ://设置QQ应用信息,其中authType设置为只使用SSO授权[appInfoSSDKSetupQQByAppId:@"QQAppId"appKey:@"QQ的AppKey"authType:SSDKAuthTypeSSO];break;default:break;}}];4、接下来是写分享参数(即触发分享事件调用分享接口的地方)。首先导入头文件#import#import#import#import#import"WXApi.h"#import"WeiboSDK.h"然后在下面的方法中添加这段代码,将下面的代码放在触发点击事件的地方地方会做。//1。创建分享参数NSArray*imageArray=@[[UIImageimageNamed:@"Sharingimage"]];//(注意:图片必须在Xcode的左边目录,名称必须传递正确。如果要分享对于网络图片,可以这样传递iamge参数images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])if(imageArray){NSMutableDictionary*shareParams=[NSMutableDictionary字典];[shareParamsSSDKSetupShareParamsByText:@"sharedcontent"images:imageArrayurl:[NSURLURLWithString:@"sharedURL"]title:@"sharedtitle"type:SSDKContentTypeAuto];//2、分享(可以弹出我们的分享菜单和editinginterface)//显示菜单的视图,此参数作为iPad版弹出菜单的参考视图。只有传递这个参数,才能弹出我们的分享菜单。您可以传递共享按钮对象或自己创建一个小视图对象。iPhone可以通过nil而不影响[ShareSDKshowShareActionSheet:nilitems:nilshareParams:shareParamsonShareStateChanged:^(SSDKResponseStatestate,SSDKPlatformTypeplatformType,NSDictionary*userData,SSDKContentEntity*contentEntity,NSError*error,BOOLend){switch(state){caseSSDKResponseStateSuccess:{UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"分享成功"message:nildelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];[alertViewshow];break;}caseSSDKResponseStateFail:{UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"Sharingfailed"消息:[NSStringstringWithFormat:@"%@",error]delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil,nil];[alertshow];break;}default:break;}}];}5.设置调用QQ微信和微博客户端如图:设置微信:在微信AppID前加wx设置QQ:在URL类型中添加QQ的AppID,格式为:“QQ”+十六进制AppId的个数(如果appId转换后的16进制数不够8位,则在前面补0,如果转换为:5FB8B52,则最后填写为:QQ05FB8B52注意:转换后的字母要大写)的十六进制转换方法:echo'ibase=10;obase=16;801312852'|bc其中801312852为QQ的AppID。例如如下图所示:设置新浪微博:这个你还需要做,基本就这些了。重点来了,如果这样还是不能分享的话可能是因为你没有把QQ和微信加入白名单在项目的plist文件中添加如下内容(白名单):LSApplicationQueriesSchemeswechatweixinmqzoneopensdkmqzoneopensdkapimqzoneopensdkapi19mqzoneopensdkapiV2mqqOpensdkSSoLoginmqqopensdkapiV2mqqopensdkapiV3wtloginmqq2mqqapimqqwpamqzonemqqsinaweibosinaweibohdsinaweibossosinaweibohdssoweibosdkweibosdk2.5TencentWeibotencentweiboSdkv2如果你的项目是一个新项目还没写Fornetworkrequests,thereisstillasectiontoaddNSAppTransportSecurityNSAllowsArbitraryLoads.Basically,thereshouldbenoproblem.QQandWeChatmustusearealmachinewithQQandWeChattocomeout,sotheemulatorcannotcomeoutofQQandWeChat..Whenyourapplicationislaunched,youmustunifytheinformationofyourvariousplatformstopreventerrors.Therestistowaitforyourapplicationtopassthereviewandtakealookateachplatform,andsomestillneedtobereviewed.Inaddition,thedefaultdisplayofShareSDKisEnglish,andaChineselanguageneedstobelocalized.
