以下全是正事:
关于友盟的UShare如何安装,请移步到友盟官方文档iOS | iOS | 开发者中心 | 友盟-专业的移动开发者服务平台 | 移动应用统计 | Android统计 | iPhone统计
首选得申请微信的appkey和secret。分享时免费的,微信登录需要申请:300人民币
假设已经安装好了:并且白名单已经添加到info.plist,url scheme也已经添加到url type,-objc也添加了,如果不知道怎么填,官方文档中有。
关于配置怎么填就不多说了。上代码。
1、 AppDelegate中的代码
#import
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* 打开调试日志 */
[[UMSocialManager defaultManager] openLog:YES];
/* 设置友盟appkey */
[[UMSocialManager defaultManager] setUmSocialAppkey:kUMeng];
[self configUSharePlatforms];
[self confitUShareSettings];
}
- (void)confitUShareSettings
{
// [UMSocialGlobal shareInstance].isUsingWaterMark = YES;
// [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
}
- (void)configUSharePlatforms
{
// [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:kSinaWBKey appSecret:kSinaWBSecret redirectURL:kRedirectURL];
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:kWechatKey appSecret:kWechatSrcret redirectURL:kRedirectURL];
}
// 支持所有iOS系统
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
// 其他如支付等SDK的回调
}
return result;
}
登录控制器的代码
#import
- (void)getAuthWithUserInfoFromWechat
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:self completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// // 授权信息
// NSLog(@"Wechat uid: %@", resp.uid);
// NSLog(@"Wechat openid: %@", resp.openid);
// NSLog(@"Wechat unionid: %@", resp.unionId);
// NSLog(@"Wechat accessToken: %@", resp.accessToken);
// NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
// NSLog(@"Wechat expiration: %@", resp.expiration);
//
// // 用户信息
// NSLog(@"Wechat name: %@", resp.name);
// NSLog(@"Wechat iconurl: %@", resp.iconurl);
// NSLog(@"Wechat gender: %@", resp.unionGender);
//
// // 第三方平台SDK源数据
// NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
/***下面是我的登录相关代码,可以不看**/
[self.wechatInfoDict setObject:@"weixin" forKey:@"accountType"];
[self.wechatInfoDict setObject:resp.openid forKey:@"openID"];
[self.wechatInfoDict setObject:resp.name forKey:@"nickname"];
NSString * imgUrl = [resp.iconurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self.wechatInfoDict setObject:imgUrl forKey:@"headPhoto"];
NSNumber *gender = [resp.gender isEqualToString:@"m"] ? @1:@2;
[self.wechatInfoDict setObject:gender forKey:@"sex"];
[self.wechatInfoDict setObject:@"北京市" forKey:@"city"];
[kUserDefaults setObject:imgUrl forKey:kWxIcon];
[kUserDefaults setObject:resp.name forKey:kWXName];
[kUserDefaults setObject:gender forKey:kWxGender];
[kUserDefaults synchronize];
[self login];
}
}];
}
分享控制器中的代码
#import
-(void)goShare
{
//设置平台类型:依次为微信好友,朋友圈,收藏
[UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_WechatFavorite)]];
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建网页内容对象
NSString *title = [NSString stringWithFormat:@"%@",[Tools dealIsNullWithStr:_model.houseName]];
NSString *price = [NSString stringWithFormat:@"%@元/月",[Tools dealIsNullWithStr:_model.roomMoney]];
NSString *area = [NSString stringWithFormat:@"%@㎡",[Tools dealIsNullWithStr:_model.roomArea]];
NSString *der = [NSString stringWithFormat:@"%@",[Tools dealIsNullWithStr:_model.roomDirection]];
NSString *des = [NSString stringWithFormat:@"%@ %@ %@ %@",title,price,area,der];
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:des thumImage:self.shareImgView.image];
//设置网页地址
shareObject.webpageUrl =@"http://www.baidu.com";
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//调用分享接口
//根据平台类型分享具体内容
if (platformType == UMSocialPlatformType_WechatSession) {
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}else if (platformType == UMSocialPlatformType_WechatTimeLine)
{
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}else if (platformType == UMSocialPlatformType_WechatFavorite)
{
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatFavorite messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}
}];
}
太晚了就这么写这么多了,我这里跑通了,欢迎交流指正。