可以参考demo来做,demo是可以跑起来的,如果用demo,只需要把key和ID换掉就好了,点击下载demo
注册APPKey
去友盟注册新AppKey拿到注册的AppKey
导入SDK
导入友盟提供的SDK,其目录结构如下
只需要将UMSocial导入项目中,接下来配置SDK
配置
在Build Settings
->Other Linker Flags
加入-ObjC
如图所示
设置Web支持HTTP
因为iOS目前不支持HTTP,所以在web登录中还需要配置HTTP,配置方法如下:
在Info-plist
文件中添加App Transport Security Settings
其类型为Dictionary
,然后在App Transport Security Settings
中添加Allow Arbitrary Loads
,其类型为BOOL,将其修改为YES
配置成功如下:
添加依赖库
友盟依赖库
- libsqlite3.tbd
- CoreGraphics.framework
微信依赖库
- SystemConfiguration.framework
- CoreTelephony.framework
- libsqlite3.tbd
- libc++.tbd
- libz.tbd
- QQ依赖库
- SystemConfiguration.framework
- libc++.tbd
- 微博依赖库
- SystemConfiguration.framework
- CoreTelephony.framework
- ImageIO.framework
- libsqlite3.tbd
- libz.tbd
导入的库如图:
设置白名单&Schemes
- 设置SSO白名单
配置白名单小技巧,右击Info.plist文件,选择文本编辑器打开,将下面代码直接copy进去,没必要为这些东西浪费时间
<key>LSApplicationQueriesSchemes</key>
<array>
<!-- 微信 URL Scheme 白名单-->
<string>wechat</string>
<string>weixin</string>
<!-- 新浪微博 URL Scheme 白名单-->
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<!-- QQ、Qzone URL Scheme 白名单-->
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV4</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqqbrowser</string>
<string>mttbrowser</string>
</array>
- 配置Schemes
来到Info
->URL Types
配置Schemes
QQ空间的是将QQ的ID改为16进制,QQID原本是8进制,在前面+QQ,不足八位数前面+0
平台 | APPKey | 举例 | 注意 |
---|---|---|---|
100424468 | tencent100424468 | QQ需要添加tencent | |
QQ空间 | 918d3f7 | QQ0918d3f7 | 不足八位前面+0 |
微信 | wxdc1e388c3822c80b | wxdc1e388c3822c80b | 微信直接用 |
微博 | 3921700954 | wb3921700954 | 微博添加wb |
配置如图所示:
注册平台
导入SDK头文件#import <UMSocialCore/UMSocialCore.h>
在AppDelegate.m文件中配置,在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中注册友盟的AppKey
//设置AppKey,是在友盟注册之后给到的key
[[UMSocialManager defaultManager] setUmSocialAppkey:UMKEY];
然后注册各个平台的AppKey和AppID
// setPlaform是要注册的平台
/* 微信聊天 */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxdc1e388c3822c80b" appSecret:@"3baf1193c85774b3fd9d18447d76cab0" redirectURL:@"http://mobile.umeng.com/social"];
/*QQ*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1106152098" appSecret:@"KexLaOiU6VVzxgQb" redirectURL:@"http://mobile.umeng.com/social"];
/*
注**
redirectURL必须要和微博设置的回调一致
*/
/* 新浪 */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"1234957746" appSecret:@"b3564b393bfcb42672d8e84f084ea550" redirectURL:@"https://sns.whalecloud.com/sina2/callback"];
设置回调
在AppDelegate.m中添加以下方法设置回调
// 支持所有iOS系统版本回调
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if (!result) {
// 其他如支付等SDK的回调
}
return result;
}
使用
导入SDK头文件#import <UMSocialCore/UMSocialCore.h>
- (void)share{
/*
创建网页内容对象
根据不同需求设置不同分享内容,一般为图片,标题,描述,url
*/
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"分享标题" descr:@"分享的内容的描述" thumImage:[UIImage imageNamed:@"head@2x"]];
//设置网页地址
shareObject.webpageUrl = @"http://www.talkmoney.cn";
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
/**
设置分享
@param data 分享返回信息
@param error 失败信息
@param UMSocialPlatformType 分享平台
*/
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享结果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的数据
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
}];
}