// // shareUtil.m // ThePaperDemo // // Created by scar1900 on 14/12/3. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "shareUtil.h" #import #import "WXApi.h" #import "WeiboSDK.h" #import //#import #import #import #import #import #import #import "SDWebImageManager.h" //#import static shareUtil* shareInstance; @interface shareUtil() @property (copy) void (^tencentLoginHandler)(BOOL isSuccess,NSDictionary* dic); @property (copy) void (^tencentShareHandler)(BOOL isSuccess); @end @implementation shareUtil @synthesize tencentOAuth; #pragma mark - initial + (shareUtil *)instance { @synchronized(self) { if (nil == shareInstance) { shareInstance = [[shareUtil alloc] init]; } } return shareInstance; } + (void)connectTecent:(NSString*)appId { [shareUtil instance].tencentOAuth = [[TencentOAuth alloc]initWithAppId:appId andDelegate:[shareUtil instance]]; } #pragma mark - society login + (void) weChatLogin:(void (^)(BOOL isSuccess,NSDictionary* dic))completion { if (![shareUtil isHaveWeChatClient]) { ShowTextMessage(@"您尚未安装微信"); completion(NO,nil); return; } clearImageMemCache(); ShareType type = ShareTypeWeixiSession; [self thirdPartyOutLogin:weChatLoginType]; // //设置授权选项 id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK getUserInfoWithType:type authOptions:authOptions result:^(BOOL result, id userInfo, id error) { if (result) { NSMutableDictionary *infoDic = [NSMutableDictionary dictionary]; NSArray *keys = [[userInfo sourceData] allKeys]; for (int i = 0; i < [keys count]; i++) { NSString *keyName = [keys objectAtIndex:i]; id value = [[userInfo sourceData] objectForKey:keyName]; if (![value isKindOfClass:[NSString class]]) { if ([value respondsToSelector:@selector(stringValue)]) { value = [value stringValue]; } else { value = @""; } } [infoDic setValue:value forKey:keyName]; } TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:weChatLoginType]; TPLOG(@"token:%@ userInfo:%@",token,infoDic); if (completion) completion(YES,infoDic); }else { [ShareSDK cancelAuthWithType:type]; ShowTextMessage(@"授权失败"); TPLOG(@"%@",error.errorDescription); if (completion) completion(NO,nil); } }]; } + (void) sinaLogin:(void (^)(BOOL isSuccess,NSDictionary *dic))completion { [self thirdPartyOutLogin:sinaWeiBoLoginType]; clearImageMemCache(); id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:authOptions result:^(BOOL result, id userInfo, id error) { if (result) { NSMutableDictionary *infoDic = [NSMutableDictionary dictionary]; NSArray *keys = [[userInfo sourceData] allKeys]; for (int i = 0; i < [keys count]; i++) { NSString *keyName = [keys objectAtIndex:i]; id value = [[userInfo sourceData] objectForKey:keyName]; if (![value isKindOfClass:[NSString class]]) { if ([value respondsToSelector:@selector(stringValue)]) { value = [value stringValue]; } else { value = @""; } } [infoDic setValue:value forKey:keyName]; } TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:sinaWeiBoLoginType]; TPLOG(@"token:%@ userInfo:%@",token,infoDic); completion(YES,infoDic); }else { ShowTextMessage(@"授权失败"); TPLOG(@"%@",error.errorDescription); completion(NO,nil); } }]; } + (void) qqLogin:(void (^)(BOOL isSuccess,NSDictionary *dic))completion { [self thirdPartyOutLogin:QQLoginType]; clearImageMemCache(); [shareUtil connectTecent:QQAppId]; [shareUtil instance].tencentLoginHandler = completion; NSArray * permissions = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, kOPEN_PERMISSION_ADD_TOPIC,kOPEN_PERMISSION_ADD_SHARE,kOPEN_PERMISSION_ADD_PIC_T, nil]; [[shareUtil instance].tencentOAuth authorize:permissions inSafari:NO]; } + (void) renrenLogin:(void (^)(BOOL isSuccess,NSDictionary *dic))completion { [self thirdPartyOutLogin:renrenLoginType]; id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK getUserInfoWithType:ShareTypeRenren authOptions:authOptions result:^(BOOL result, id userInfo, id error) { if (result) { NSMutableDictionary *infoDic = [NSMutableDictionary dictionary]; NSArray *keys = [[userInfo sourceData] allKeys]; for (int i = 0; i < [keys count]; i++) { NSString *keyName = [keys objectAtIndex:i]; id value = [[userInfo sourceData] objectForKey:keyName]; if (![value isKindOfClass:[NSString class]]) { if ([value respondsToSelector:@selector(stringValue)]) { value = [value stringValue]; } else { value = @""; } } [infoDic setValue:value forKey:keyName]; } TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:renrenLoginType]; TPLOG(@"token:%@ userInfo:%@",token,infoDic); completion(YES,infoDic); }else { ShowTextMessage(@"授权失败"); TPLOG(@"%@",error.errorDescription); completion(NO,nil); } }]; } +(void)doubanLogin:(void (^)(BOOL, NSDictionary *))completion{ [self thirdPartyOutLogin:doubanLoginType]; id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK getUserInfoWithType:ShareTypeDouBan authOptions:authOptions result:^(BOOL result, id userInfo, id error) { if (result) { NSMutableDictionary *infoDic = [NSMutableDictionary dictionary]; NSArray *keys = [[userInfo sourceData] allKeys]; for (int i = 0; i < [keys count]; i++) { NSString *keyName = [keys objectAtIndex:i]; id value = [[userInfo sourceData] objectForKey:keyName]; if (![value isKindOfClass:[NSString class]]) { if ([value respondsToSelector:@selector(stringValue)]) { value = [value stringValue]; } else { value = @""; } } [infoDic setValue:value forKey:keyName]; } TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:doubanLoginType]; TPLOG(@"token:%@ userInfo:%@",token,infoDic); completion(YES,infoDic); }else { ShowTextMessage(@"授权失败"); TPLOG(@"%@",error.errorDescription); completion(NO,nil); } }]; } + (void) evernoteLogin:(void (^)(BOOL isSuccess,NSDictionary *dic))completion { [self thirdPartyOutLogin:evernoteLoginType]; id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK getUserInfoWithType:ShareTypeEvernote authOptions:authOptions result:^(BOOL result, id userInfo, id error) { if (result) { NSMutableDictionary *infoDic = [NSMutableDictionary dictionary]; NSArray *keys = [[userInfo sourceData] allKeys]; for (int i = 0; i < [keys count]; i++) { NSString *keyName = [keys objectAtIndex:i]; id value = [[userInfo sourceData] objectForKey:keyName]; if (![value isKindOfClass:[NSString class]]) { if ([value respondsToSelector:@selector(stringValue)]) { value = [value stringValue]; } else { value = @""; } } [infoDic setValue:value forKey:keyName]; } TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:evernoteLoginType]; TPLOG(@"token:%@ userInfo:%@",token,infoDic); completion(YES,infoDic); }else { ShowTextMessage(@"授权失败"); TPLOG(@"%@",error.errorDescription); completion(NO,nil); } }]; } + (void) youdaoLogin:(void (^)(BOOL isSuccess,NSDictionary *dic))completion { [self thirdPartyOutLogin:youdaoLoginType]; id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK getUserInfoWithType:ShareTypeYouDaoNote authOptions:authOptions result:^(BOOL result, id userInfo, id error) { if (result) { NSMutableDictionary *infoDic = [NSMutableDictionary dictionary]; NSArray *keys = [[userInfo sourceData] allKeys]; for (int i = 0; i < [keys count]; i++) { NSString *keyName = [keys objectAtIndex:i]; id value = [[userInfo sourceData] objectForKey:keyName]; if (![value isKindOfClass:[NSString class]]) { if ([value respondsToSelector:@selector(stringValue)]) { value = [value stringValue]; } else { value = @""; } } [infoDic setValue:value forKey:keyName]; } TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:youdaoLoginType]; TPLOG(@"token:%@ userInfo:%@",token,infoDic); completion(YES,infoDic); }else { ShowTextMessage(@"授权失败"); TPLOG(@"%@",error.errorDescription); completion(NO,nil); } }]; } + (void) pocketLogin:(void (^)(BOOL isSuccess,id userInfo))completion { [self thirdPartyOutLogin:pocketLoginType]; clearImageMemCache(); id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK getUserInfoWithType:ShareTypePocket authOptions:authOptions result:^(BOOL result, id userInfo, id error) { if (result) { NSMutableDictionary *infoDic = [NSMutableDictionary dictionary]; NSArray *keys = [[userInfo sourceData] allKeys]; if (keys || keys.count > 0) { for (int i = 0; i < [keys count]; i++) { NSString *keyName = [keys objectAtIndex:i]; id value = [[userInfo sourceData] objectForKey:keyName]; if (![value isKindOfClass:[NSString class]]) { if ([value respondsToSelector:@selector(stringValue)]) { value = [value stringValue]; } else { value = @""; } } [infoDic setValue:value forKey:keyName]; } TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:pocketLoginType]; TPLOG(@"token:%@ userInfo:%@",token,infoDic); if (completion) completion(YES,infoDic); }else { TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:pocketLoginType]; TPLOG(@"token:%@ userInfo:%@",token,infoDic); if (completion) completion(YES,userInfo); } }else { ShowTextMessage(@"授权失败"); TPLOG(@"%@",error.errorDescription); completion(NO,nil); } }]; } #pragma mark - thirParty bind /** * 第三方社交账号绑定 * * @param type 操作类型 * @param completion 完成回调 */ + (void)bindSocietyAccountWithType:(shareAndLoginType)type completion:(void (^)(BOOL isSuccess))completion { [shareUtil thirdPartyOutLogin:type]; NSString *typeStr = [shareUtil returnServerStrWithType:type]; if ([typeStr isEqualToString: @"WEIXIN"]) { [shareUtil weChatBind:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else if ([typeStr isEqualToString: @"SINA"]) { [shareUtil sinaBind:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else if ([typeStr isEqualToString: @"TENCENT"]) { [shareUtil qqBind:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else if ([typeStr isEqualToString: @"RENREN"]) { [shareUtil renrenBind:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else if ([typeStr isEqualToString: @"YINXIANG"]) { [shareUtil evernoteBind:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else if ([typeStr isEqualToString: @"YOUDAO"]) { [shareUtil youdaoBind:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else if ([typeStr isEqualToString: @"POCKET"]) { [shareUtil pocketBind:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else if ([typeStr isEqualToString:@"DOUBAN"]){ [shareUtil doubanBind:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; } } //微信绑定 + (void)weChatBind:(void (^)(BOOL isSuccess))completion { [shareUtil weChatLogin:^(BOOL isSuccess, NSDictionary *dic) { if (isSuccess) { NSDictionary *tempDic = @{@"weiboType":@"WEIXIN", @"otype":@"2", @"uid":dic[@"unionid"], @"token":[shareUtil getTokenWithType:weChatLoginType], @"name":dic[@"nickname"], @"pic":dic[@"headimgurl"]}; [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } }]; } //qq绑定 + (void)qqBind:(void (^)(BOOL isSuccess))completion { [shareUtil qqLogin:^(BOOL isSuccess, NSDictionary *dic) { if (isSuccess) { NSDictionary *tempDic = @{@"weiboType":@"TENCENT", @"otype":@"2", @"uid":[[shareUtil instance].tencentOAuth openId], @"token":[shareUtil getTokenWithType:QQLoginType], @"name":dic[@"nickname"], @"pic":dic[@"figureurl_qq_1"]}; [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } }]; } //新浪微博绑定 + (void)sinaBind:(void (^)(BOOL isSuccess))completion { [shareUtil sinaLogin:^(BOOL isSuccess, NSDictionary *dic) { if (isSuccess) { NSDictionary *tempDic = @{@"weiboType":@"SINA", @"otype":@"2", @"uid":dic[@"id"], @"token":[shareUtil getTokenWithType:sinaWeiBoLoginType], @"name":dic[@"name"], @"pic":dic[@"avatar_hd"]}; [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } }]; } //人人绑定 + (void)renrenBind:(void (^)(BOOL isSuccess))completion { [shareUtil renrenLogin:^(BOOL isSuccess, NSDictionary *dic) { if (isSuccess) { NSDictionary *tempDic = @{@"weiboType":@"RENREN", @"otype":@"2", @"uid":dic[@"id"], @"token":[shareUtil getTokenWithType:renrenLoginType], @"name":dic[@"name"], @"pic":dic[@"avatar"]}; [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } }]; } //豆瓣绑定 + (void)doubanBind:(void (^)(BOOL isSuccess))completion { [shareUtil doubanLogin:^(BOOL isSuccess, NSDictionary *dic) { if (isSuccess) { NSDictionary *tempDic = @{@"weiboType":@"DOUBAN", @"otype":@"2", @"uid":dic[@"id"], @"token":[shareUtil getTokenWithType:doubanLoginType], @"name":dic[@"name"], @"pic":dic[@"avatar"]}; [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } }]; } //印象笔记绑定 + (void)evernoteBind:(void (^)(BOOL isSuccess))completion { [shareUtil evernoteLogin:^(BOOL isSuccess, NSDictionary *dic) { if (isSuccess) { NSDictionary *tempDic = [NSDictionary dictionary]; if ([ShareSDK hasAuthorizedWithType:ShareTypeEvernote]) { id credential = [ShareSDK getCredentialWithType:ShareTypeEvernote]; TPLOG(@"%@",[credential extInfo]); NSDictionary *extInfoDic = [credential extInfo]; tempDic = @{@"weiboType":@"YINXIANG", @"otype":@"2", @"uid":dic[@"id"], @"token":[shareUtil getTokenWithType:evernoteLoginType], @"name":dic[@"username"], @"tokenSecret":extInfoDic[@"edam_noteStoreUrl"]}; } [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } }]; } //有道云笔记绑定 + (void)youdaoBind:(void (^)(BOOL isSuccess))completion { [shareUtil youdaoLogin:^(BOOL isSuccess, NSDictionary *dic) { if (isSuccess) { NSDictionary *tempDic = @{@"weiboType":@"YOUDAO", @"otype":@"2", @"uid":dic[@"id"], @"token":[shareUtil getTokenWithType:youdaoLoginType], @"name":dic[@"user"]}; [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } }]; } //pocket绑定 + (void)pocketBind:(void (^)(BOOL isSuccess))completion { [shareUtil pocketLogin:^(BOOL isSuccess, id userInfo) { if (isSuccess) { id pocketInfo = userInfo; NSDictionary *tempDic = @{@"weiboType":@"POCKET", @"otype":@"2", @"uid":[pocketInfo uid], @"token":[shareUtil getTokenWithType:pocketLoginType], @"name":[pocketInfo nickname]}; [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } }]; } //解除绑定 + (void)bindOutWithType:(shareAndLoginType)type completion:(void (^)(BOOL isSuccess))completion { [shareUtil thirdPartyOutLogin:type]; NSString *typeStr = [shareUtil returnServerStrWithType:type]; NSDictionary *tempDic = @{@"weiboType":typeStr, @"otype":@"3"}; [Remote doJsonActionWithBlock:0 requestUrl:societyFromSDKURL parameter:tempDic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; }else { ShowTextMessage(message); } if (completion) completion(success); }]; } /** * 检查用户是否已经绑定相应社交账号 * * @param type 社交操作类型 * * @return BOOL值 */ + (BOOL)IsUserBindedWithType:(shareAndLoginType)type { NSString *typeStr = [shareUtil returnServerStrWithType:type]; NSDictionary *weiboMap = [TPUserDefault instance].userBO.weiboIdMap; __block BOOL isHaveBinded = NO; [weiboMap.allKeys enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) { if ([typeStr isEqualToString:key]) { isHaveBinded = YES; } }]; if (!isHaveBinded) { return NO; }else { return YES; } } /** * 获取服务端存储的token并传入本地社交SDK * * @param type 社交操作类型 */ + (void)getTokenFromServerToLocalSDK:(shareAndLoginType)type completion:(void (^)(BOOL isSuccess))completion{ NSString *token = [shareUtil getTokenWithType:type]; if (token || !isBlankString(token)) { if (completion) completion(YES); return; } NSString *typeStr = [shareUtil returnServerStrWithType:type]; NSDictionary *dic = @{@"weiboType":typeStr}; [Remote doJsonActionWithBlock:100 requestUrl:getSocietyInfoFromSDKURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { NSArray *list = responseData[@"societyList"]; if (!list || list.count == 0) { if (completion) completion(NO); }else { NSDictionary *societyDic = list[0]; NSString *sdkToken = societyDic[@"token"]; NSString *openId = societyDic[@"uid"]; NSDate *eDate = [NSDate dateWithTimeIntervalSinceNow:[societyDic[@"expiresIn"] doubleValue]]; if ([typeStr isEqualToString:@"TENCENT"]) { [[shareUtil instance].tencentOAuth setAccessToken:sdkToken]; [[shareUtil instance].tencentOAuth setOpenId:openId]; [[shareUtil instance].tencentOAuth setExpirationDate:eDate]; if (completion) completion(YES); }else { ShareType sharetype = [shareUtil returnShareTypeWithLocalType:type]; NSString *secret = [shareUtil returnSecretWithType:type]; if ([typeStr isEqualToString:@"YINXIANG"]) { if (societyDic[@"tokenSecret"]) { NSDictionary *dic = @{@"edam_noteStoreUrl":societyDic[@"tokenSecret"]}; id credential = [ShareSDK credentialWithType:sharetype uid:openId token:sdkToken secret:secret expired:eDate extInfo:dic]; [ShareSDK setCredential:credential type:sharetype]; if (completion) completion(YES); }else { ShowTextMessage(@"权限验证出错,重新授权"); if (completion) completion(NO); } }else { id credential = [ShareSDK credentialWithType:sharetype uid:openId token:sdkToken secret:secret expired:eDate extInfo:nil]; [ShareSDK setCredential:credential type:sharetype]; if (completion) completion(YES); } } } }else { ShowTextMessage(message); if (completion) completion(NO); } }]; } /** * 获取服务端社交账号分类描述 * * @param type 本地社交操作类型 * * @return 服务端社交分类类型 */ + (NSString*)returnServerStrWithType:(shareAndLoginType)type { NSString *typeStr = @""; if (type == weChatSessionShareType || type == weChatTimeLineShareType || type == weChatLoginType) { typeStr = @"WEIXIN"; }else if (type == sinaWeiBoShareType || type == sinaWeiBoLoginType) { typeStr = @"SINA"; }else if (type == QQFriendShareType || type == QQZoneShareType || type == TencentWeboShareType || type == QQLoginType) { typeStr = @"TENCENT"; }else if (type == renrenShareType || type == renrenLoginType) { typeStr = @"RENREN"; }else if (type == evernoteShareType || type == evernoteLoginType) { typeStr = @"YINXIANG"; }else if (type == youdaoShareType || type == youdaoLoginType) { typeStr = @"YOUDAO"; }else if (type == pocketShareType || type == pocketLoginType) { typeStr = @"POCKET"; }else if(type == doubanLoginType || type == doubanShareType){ typeStr = @"DOUBAN"; } return typeStr; } /** * 返回ShareSDK操作类型 * * @param type 本地社交操作类型 * * @return ShareSDK操作类型 */ + (ShareType) returnShareTypeWithLocalType:(shareAndLoginType)type { if (type == weChatSessionShareType || type == weChatLoginType) { return ShareTypeWeixiSession; }else if (type == weChatTimeLineShareType) { return ShareTypeWeixiTimeline; }else if (type == sinaWeiBoShareType || type == sinaWeiBoLoginType) { return ShareTypeSinaWeibo; }else if (type == renrenShareType || type == renrenLoginType) { return ShareTypeRenren; }else if (type == evernoteShareType || type == evernoteLoginType) { return ShareTypeEvernote; }else if (type == youdaoLoginType || type == youdaoShareType){ return ShareTypeYouDaoNote; }else if (type == emailShareType) { return ShareTypeMail; }else if (type == pocketShareType || type == pocketLoginType) { return ShareTypePocket; }else if(type == doubanLoginType || type == doubanShareType){ return ShareTypeDouBan; }else return ShareTypeCopy; } /** * 返回操作需要的对应secret * * @param type 操作类型 * * @return secret */ + (NSString*) returnSecretWithType:(shareAndLoginType)type { NSString *secret = @""; if (type == weChatSessionShareType || type == weChatTimeLineShareType || type == weChatLoginType) { secret = wechatSecret; }else if (type == sinaWeiBoShareType || type == sinaWeiBoLoginType) { secret = sinaSecret; }else if (type == renrenShareType || type == renrenLoginType) { secret = renrenSecret; }else if (type == evernoteShareType || type == evernoteLoginType) { secret = evernoteSecret; }else if (type == youdaoShareType || type == youdaoLoginType) { secret = youdaoSecret; }else if (type == doubanLoginType || type == doubanShareType){ secret = doubanSecret; } return secret; } #pragma mark #pragma mark - society login out + (BOOL) thirdPartyOutLogin:(shareAndLoginType)type { NSString *token = [shareUtil getTokenWithType:type]; TPLOG(@"login out of token(%@)",token); if (type == weChatSessionShareType || type == weChatTimeLineShareType || type == weChatLoginType) { [ShareSDK cancelAuthWithType:ShareTypeWeixiSession]; }else if (type == sinaWeiBoShareType || type == sinaWeiBoLoginType) { [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo]; }else if (type == renrenShareType || type == renrenLoginType) { [ShareSDK cancelAuthWithType:ShareTypeRenren]; }else if (type == QQLoginType) { [[shareUtil instance].tencentOAuth logout:[shareUtil instance]]; }else if (type == evernoteShareType || type == evernoteLoginType) { [ShareSDK cancelAuthWithType:ShareTypeEvernote]; }else if (type == youdaoShareType || type == youdaoLoginType) { [ShareSDK cancelAuthWithType:ShareTypeYouDaoNote]; }else if (type == pocketLoginType || type == pocketShareType) { [ShareSDK cancelAuthWithType:ShareTypePocket]; }else if (type == doubanLoginType || type == doubanShareType){ [ShareSDK cancelAuthWithType:ShareTypeDouBan]; } return YES; } #pragma mark - society share + (void) shareNews:(NSString*)shareUrl imageUrl:(NSString*)imageUrl title:(NSString*)title summary:(NSString*)summary shareType:(shareAndLoginType)type shareStyle:(shareStyle)style completion:(void (^)(BOOL isSuccess))completion { if ([TPUserDefault instance].userBO) { if (type != emailShareType && type != copyType && type != systemShareType) { if ([shareUtil IsUserBindedWithType:type]) { //已绑定 [shareUtil getTokenFromServerToLocalSDK:type completion:^(BOOL isSuccess) { if (isSuccess) { [shareUtil directShareNews:shareUrl imageUrl:imageUrl title:title summary: summary shareType:type shareStyle:style completion:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else { //获取token失败(过期或者信息有错误) [shareUtil bindOutWithType:type completion:^(BOOL isSuccess) { if (isSuccess) { [shareUtil bindSocietyAccountWithType:type completion:^(BOOL isSuccess) { if (isSuccess) {//绑定成功后进行分享操作 [shareUtil directShareNews:shareUrl imageUrl:imageUrl title:title summary:summary shareType:type shareStyle:style completion:^(BOOL success) { if (completion) completion(success); }]; }else { if (completion) completion(isSuccess); } }]; } }]; } }]; }else { //未绑定 先绑定再分享 [shareUtil bindSocietyAccountWithType:type completion:^(BOOL isSuccess) { if (isSuccess) {//绑定成功后进行分享操作 [shareUtil directShareNews:shareUrl imageUrl:imageUrl title:title summary:summary shareType:type shareStyle:style completion:^(BOOL success) { if (completion) completion(success); }]; }else { if (completion) completion(isSuccess); } }]; } }else { [shareUtil directShareNews:shareUrl imageUrl:imageUrl title:title summary:summary shareType:type shareStyle:style completion:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; } }else { [shareUtil directShareNews:shareUrl imageUrl:imageUrl title:title summary:summary shareType:type shareStyle:style completion:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; } } + (void) interactionShareWithImage:(UIImage*)image title:(NSString*)title comment:(NSString*)commentString shareUrl:(NSString*)shareUrl shareStyle:(shareStyle)style shareType:(shareAndLoginType)type completion:(void (^)(BOOL isSuccess))completion { if ([TPUserDefault instance].userBO) { if ([shareUtil IsUserBindedWithType:type]) { //已绑定 [shareUtil getTokenFromServerToLocalSDK:type completion:^(BOOL isSuccess) { if (isSuccess) { [shareUtil directShareInteractionImage:image title:title comment:commentString shareUrl:shareUrl shareStyle:(shareStyle)style shareType:type completion:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else { //获取token失败(过期或者信息有错误) [shareUtil bindOutWithType:type completion:^(BOOL isSuccess) { if (isSuccess) { [shareUtil bindSocietyAccountWithType:type completion:^(BOOL isSuccess) { if (isSuccess) {//绑定成功后进行分享操作 [shareUtil directShareInteractionImage:image title:title comment:commentString shareUrl:shareUrl shareStyle:(shareStyle)style shareType:type completion:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else { if (completion) completion(isSuccess); } }]; } }]; } }]; }else { //未绑定 先绑定再分享 [shareUtil bindSocietyAccountWithType:type completion:^(BOOL isSuccess) { if (isSuccess) {//绑定成功后进行分享操作 [shareUtil directShareInteractionImage:image title:title comment:commentString shareUrl:shareUrl shareStyle:(shareStyle)style shareType:type completion:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; }else { if (completion) completion(isSuccess); } }]; } }else { [shareUtil directShareInteractionImage:image title:title comment:commentString shareUrl:shareUrl shareStyle:(shareStyle)style shareType:type completion:^(BOOL isSuccess) { if (completion) completion(isSuccess); }]; } } + (void) directShareInteractionImage:(UIImage*)image title:(NSString*)title comment:(NSString*)commentString shareUrl:(NSString*)shareUrl shareStyle:(shareStyle)style shareType:(shareAndLoginType)type completion:(void (^)(BOOL isSuccess))completion { if (type != QQZoneShareType && type != QQFriendShareType && type != TencentWeboShareType) { [shareUtil shareSDKShareWithImage:image title:(NSString*)title comment:(NSString*)commentString shareUrl:(NSString*)shareUrl shareStyle:(shareStyle)style shareType:type completion:^(BOOL isSuccess) { if (completion) { if (completion) completion(isSuccess); } }]; }else { [shareUtil QQConnectShareWithImage:image title:(NSString*)title comment:(NSString*)commentString shareUrl:(NSString*)shareUrl shareStyle:(shareStyle)style shareType:type completion:^(BOOL isSuccess) { if (completion) { if (completion) completion(isSuccess); } }]; } } + (void) directShareNews:(NSString*)shareUrl imageUrl:(NSString*)imageUrl title:(NSString*)title summary:(NSString*)summary shareType:(shareAndLoginType)type shareStyle:(shareStyle)style completion:(void (^)(BOOL isSuccess))completion { if (type != QQZoneShareType && type != QQFriendShareType && type != TencentWeboShareType) { [shareUtil shareSDKShare:shareUrl imageUrl:imageUrl title:title summary:summary shareType:type shareStyle:style completion:^(BOOL isSuccess) { if (completion) { if (completion) completion(isSuccess); } }]; }else { [shareUtil QQConnectShare:shareUrl imageUrl:imageUrl title:title summary:summary shareType:type shareStyle:style completion:^(BOOL isSuccess) { if (completion) { if (completion) completion(isSuccess); } }]; } } + (void) shareSDKShare:(NSString*)shareUrl imageUrl:(NSString*)imageUrl title:(NSString*)title summary:(NSString*)summary shareType:(shareAndLoginType)type shareStyle:(shareStyle)style completion:(void (^)(BOOL isSuccess))completion { clearImageMemCache(); id attachImage = nil; if (imageUrl && !isBlankString(imageUrl)) { if ([imageUrl hasPrefix:@"http"]) { attachImage = [ShareSDK imageWithUrl:imageUrl]; }else { attachImage = [ShareSDK jpegImageWithImage:Image(imageUrl) quality:1.0]; } }else { attachImage = [ShareSDK pngImageWithImage:Image(@"other/paperLogo.png")]; } if (type == copyType) { [MobClick event:@"43"]; attachImage = nil; } id shareContent = [ShareSDK content:nil defaultContent:nil image:attachImage title:title url:shareUrl description:nil mediaType:SSPublishContentMediaTypeNews]; ShareType shareType = [shareUtil returnShareTypeWithLocalType:type]; if (type == weChatSessionShareType) { [shareContent setContent:summary]; }else if (type == weChatTimeLineShareType) { [shareContent setContent:summary]; }else if (type == sinaWeiBoShareType) { /** * 用户编辑过的内容 +【澎湃新闻:文章标题】 + 文章的WAP的URL (直播的话,为直播入口文章的WAP的URL)+ (分享自@澎湃新闻) */ NSString *styleStr = @""; NSString *content = @""; if (style == shareNewsStyle) { styleStr = @"澎湃新闻"; content = [NSString stringWithFormat:@"【%@:%@】%@ (分享自@澎湃新闻)#我读澎湃#",styleStr,title?title:@"",shareUrl]; }else if (style == shareLiveStyle) { styleStr = @"澎湃直播"; content = [NSString stringWithFormat:@"【%@:%@】%@ (分享自@澎湃新闻)#我读澎湃#",styleStr,summary?summary:@"",shareUrl]; }else if (style == shareSpecicalTopicStyle) { styleStr = @"澎湃专题"; content = [NSString stringWithFormat:@"【%@:%@】%@ (分享自@澎湃新闻)#我读澎湃#",styleStr,title?title:@"",shareUrl]; }else if (style == shareTopicStyle) { styleStr = @"澎湃问吧"; content = [NSString stringWithFormat:@"【%@:%@】%@ (分享自@澎湃新闻)#我读澎湃#",styleStr,title?title:@"",shareUrl]; } [shareContent setContent:content]; }else if (type == renrenShareType) { [shareContent setDesc:summary?summary:@""]; }else if (type == emailShareType) { NSString *content; if (style == shareTopicStyle) { content = [NSString stringWithFormat:@"我在澎湃问吧看到一个有意思的话题,推荐你看一下 \n%@\n%@",summary?summary:@"",shareUrl]; }else{ content = [NSString stringWithFormat:@"我在澎湃新闻看到一篇有意思的文章,推荐你看一下 \n%@\n%@",summary?summary:@"",shareUrl]; } [shareContent setContent:content]; }else if (type == evernoteShareType) { NSString *content; if (style == shareTopicStyle) { content = [NSString stringWithFormat:@"我在澎湃问吧看到一个有意思的话题,推荐你看一下

%@

%@

原文链接:%@",title?title:@"",summary?summary:@"",shareUrl?shareUrl:@""]; }else{ content = [NSString stringWithFormat:@"我在澎湃新闻看到一篇有意思的文章,推荐你看一下

%@

%@

原文链接:%@",title?title:@"",summary?summary:@"",shareUrl?shareUrl:@""]; } [shareContent setContent:content]; }else if (type == youdaoShareType) { NSString *content; if (style == shareTopicStyle) { content = [NSString stringWithFormat:@"我在澎湃问吧看到一个有意思的话题,推荐你看一下
%@
%@
原文链接:%@
",title?title:@"",summary?summary:@"",shareUrl?shareUrl:@""]; }else{ content = [NSString stringWithFormat:@"我在澎湃新闻看到一篇有意思的文章,推荐你看一下
%@
%@
原文链接:%@
",title?title:@"",summary?summary:@"",shareUrl?shareUrl:@""]; } [shareContent setContent:content]; }else if (type == pocketShareType) { NSString *content = [NSString stringWithFormat:@"我在澎湃新闻看到一篇有意思的文章,推荐你看一下。\n%@\n%@",summary?summary:@"",shareUrl]; [shareContent setContent:content]; }else if (type == copyType) { NSString *content = [NSString stringWithFormat:@"%@",shareUrl]; [shareContent setContent:content]; }else if(type == doubanShareType){ /** * 用户编辑过的内容 +【澎湃新闻:文章标题】 + 文章的WAP的URL (直播的话,为直播入口文章的WAP的URL)+ (分享自@澎湃新闻) */ NSString *styleStr = @""; if (style == shareNewsStyle) { styleStr = @"澎湃新闻"; }else if (style == shareLiveStyle) { styleStr = @"澎湃直播"; }else if (style == shareSpecicalTopicStyle) { styleStr = @"澎湃专题"; }else if (style == shareTopicStyle) { styleStr = @"澎湃问吧"; } NSString *content = [NSString stringWithFormat:@"【%@:%@】%@",styleStr,title?title:@"",shareUrl]; [shareContent setContent:content]; }else if (type == systemShareType) { ShowTextMessage(@"正在获取分享信息"); NSString *content; if (style == shareTopicStyle) { content = [NSString stringWithFormat:@"我在澎湃问吧看到一个有意思的话题,推荐你看一下 \n%@\n",summary?summary:@""]; }else{ content = [NSString stringWithFormat:@"我在澎湃新闻看到一篇有意思的文章,推荐你看一下 \n%@\n",summary?summary:@""]; } NSString *textToShare = content; NSURL *urlToShare = [NSURL URLWithString:shareUrl]; UIImage *imageToShare = nil; if (imageUrl && !isBlankString(imageUrl) && [imageUrl hasPrefix:@"http"]) { SDWebImageManager *imageManager = [SDWebImageManager sharedManager]; [imageManager downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) { } completed:^(UIImage *img, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { [shareUtil systemShareMethod:img textToshare:textToShare shareUrl:urlToShare completion:^(BOOL isSuccess) { if (isSuccess) { if (completion) completion(YES); }else { if (completion) completion(NO); } }]; }]; return; }else if (!imageUrl || isBlankString(imageUrl)){ imageToShare = Image(@"other/paperLogo.png"); [shareUtil systemShareMethod:imageToShare textToshare:textToShare shareUrl:urlToShare completion:^(BOOL isSuccess) { if (isSuccess) { if (completion) completion(YES); }else { if (completion) completion(NO); } }]; return; }else { imageToShare = Image(imageUrl); [shareUtil systemShareMethod:imageToShare textToshare:textToShare shareUrl:urlToShare completion:^(BOOL isSuccess) { if (isSuccess) { if (completion) completion(YES); }else { if (completion) completion(NO); } }]; return; } } id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK clientShareContent:shareContent type:shareType authOptions:authOptions shareOptions:nil statusBarTips:YES result:^(ShareType type, SSResponseState state, id statusInfo, id error, BOOL end) { //返回事件 if (state == SSPublishContentStateSuccess) { // NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"分享成功!")); if (type == ShareTypeCopy) { ShowMessage(@"复制成功", YES); }else { if (type != ShareTypeWeixiSession && type != ShareTypeWeixiTimeline) { ShowMessage(@"分享成功", YES); } } if (completion) completion(YES); } else if (state == SSPublishContentStateFail) { // NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败!"), [error errorCode], [error errorDescription]); TPLOG(@"%@",[error errorDescription]); if (type == ShareTypeCopy) { ShowTextMessage(@"复制取消"); }else { if (type != ShareTypeWeixiSession && type != ShareTypeWeixiTimeline) { if (ShareTypeMail == type) { ShowTextMessage([error errorDescription]); }else { ShowTextMessage(@"分享取消"); } }else { [ShareSDK cancelAuthWithType:type]; } } if (completion) completion(NO); }else if (state == SSPublishContentStateBegan) { if (type != ShareTypeCopy) { ShowTextMessage(@"正在发送"); } }else if (state == SSPublishContentStateCancel) { if (type == ShareTypeCopy) { ShowTextMessage(@"复制取消"); }else { if (type != ShareTypeWeixiSession && type != ShareTypeWeixiTimeline) { ShowTextMessage(@"分享取消"); } } if (completion) completion(NO); } }]; } + (void) shareSDKShareWithImage:(UIImage*)image title:(NSString*)title comment:(NSString*)commentString shareUrl:(NSString*)shareUrl shareStyle:(shareStyle)style shareType:(shareAndLoginType)type completion:(void (^)(BOOL isSuccess))completion { clearImageMemCache(); id attachImage = [ShareSDK jpegImageWithImage:image quality:1.0]; id shareContent = nil; if (type == sinaWeiBoShareType) { NSString* content; if (style == shareTopicStyle) { content = [NSString stringWithFormat:@"【%@:%@】澎友评论:%@%@ (分享自@澎湃新闻)#我读澎湃#",@"澎湃问吧",title?title:@"",commentString,shareUrl]; }else{ content = [NSString stringWithFormat:@"【%@:%@】澎友评论:%@%@ (分享自@澎湃新闻)#我读澎湃#",@"澎湃新闻",title?title:@"",commentString,shareUrl]; } shareContent = [ShareSDK content:content defaultContent:nil image:attachImage title:title url:shareUrl description:nil mediaType:SSPublishContentMediaTypeImage]; }else { NSString* content; if (style == shareTopicStyle) { content = [NSString stringWithFormat:@"【%@:%@】澎友评论:%@%@ (分享自@澎湃新闻)#我读澎湃#",@"澎湃问吧",title?title:@"",commentString,shareUrl]; }else{ content = [NSString stringWithFormat:@"【%@:%@】澎友评论:%@%@ (分享自@澎湃新闻)#我读澎湃#",@"澎湃新闻",title?title:@"",commentString,shareUrl]; } shareContent = [ShareSDK content:commentString defaultContent:nil image:attachImage title:title url:shareUrl description:nil mediaType:SSPublishContentMediaTypeImage]; } ShareType shareType = [shareUtil returnShareTypeWithLocalType:type]; id authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleModal viewDelegate:nil authManagerViewDelegate:nil]; [ShareSDK clientShareContent:shareContent type:shareType authOptions:authOptions shareOptions:nil statusBarTips:YES result:^(ShareType type, SSResponseState state, id statusInfo, id error, BOOL end) { //返回事件 if (state == SSPublishContentStateSuccess) { // NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"分享成功!")); if (type == ShareTypeCopy) { ShowMessage(@"复制成功", YES); }else { if (type != ShareTypeWeixiSession && type != ShareTypeWeixiTimeline) { ShowMessage(@"分享成功", YES); } } if (completion) completion(YES); } else if (state == SSPublishContentStateFail) { // NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败!"), [error errorCode], [error errorDescription]); TPLOG(@"%@",[error errorDescription]); if (type == ShareTypeCopy) { ShowTextMessage(@"复制取消"); }else { if (type != ShareTypeWeixiSession && type != ShareTypeWeixiTimeline) { if (ShareTypeMail == type) { ShowTextMessage([error errorDescription]); }else { ShowTextMessage(@"分享取消"); } }else { [ShareSDK cancelAuthWithType:type]; } } if (completion) completion(NO); }else if (state == SSPublishContentStateBegan) { if (type != ShareTypeCopy) { ShowTextMessage(@"正在发送"); } }else if (state == SSPublishContentStateCancel) { if (type == ShareTypeCopy) { ShowTextMessage(@"复制取消"); }else { if (type != ShareTypeWeixiSession && type != ShareTypeWeixiTimeline) { ShowTextMessage(@"分享取消"); } } if (completion) completion(NO); } }]; } + (void)systemShareMethod:(UIImage*)imageToShare textToshare:(NSString*)textToShare shareUrl:(NSURL*)urlToShare completion:(void (^)(BOOL isSuccess))completion{ // NSArray *shareTypeList = @[UIActivityTypePrint, // UIActivityTypeAssignToContact, // UIActivityTypeSaveToCameraRoll, // UIActivityTypePostToWeibo, // UIActivityTypePostToTencentWeibo]; NSArray *activityItems = @[textToShare, imageToShare, urlToShare]; UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil]; // activityView.excludedActivityTypes = shareTypeList; // if (isIOS8) { // UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) // { //// NSLog(@"activityType :%@", activityType); //// if (completed) //// { //// NSLog(@"completed"); //// } //// else //// { //// NSLog(@"cancel"); //// } //// //// //放回上一级界面 //// [[shareUtil instance].baseSharedController dismissModalViewControllerAnimated:YES]; // // }; // // // 初始化completionHandler,当post结束之后(无论是done还是cancell)该blog都会被调用 // activityView.completionWithItemsHandler = myBlock; // } if ([activityView respondsToSelector:@selector(completionWithItemsHandler)]) { activityView.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) { // When completed flag is YES, user performed specific activity if (completed) { if (completion) { completion(YES); } }else { if (completion) { completion(NO); } } }; } else { activityView.completionHandler = ^(NSString *activityType, BOOL completed) { // When completed flag is YES, user performed specific activity if (completed) { if (completed) { if (completion) { completion(YES); } }else { if (completion) { completion(NO); } } } }; } if (isPad) { if (isIOS8) { activityView.popoverPresentationController.sourceView = [shareUtil instance].baseSharedController.view; activityView.popoverPresentationController.sourceRect = [shareUtil instance].systemShareSourceRect; [[shareUtil instance].baseSharedController presentViewController:activityView animated:YES completion:nil]; }else { [shareUtil instance].popController = [[UIPopoverController alloc]initWithContentViewController:activityView]; [[shareUtil instance].popController presentPopoverFromRect:[shareUtil instance].systemShareSourceRect inView:[shareUtil instance].baseSharedController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } }else { [[shareUtil instance].baseSharedController presentViewController:activityView animated:YES completion:nil]; } if (isPad && isIOS8) { activityView.popoverPresentationController.sourceView = [shareUtil instance].baseSharedController.view; activityView.popoverPresentationController.sourceRect = [shareUtil instance].systemShareSourceRect; } } + (void) QQConnectShare:(NSString*)shareUrl imageUrl:(NSString*)imageUrl title:(NSString*)title summary:(NSString*)summary shareType:(shareAndLoginType)type shareStyle:(shareStyle)style completion:(void (^)(BOOL isSuccess))completion { clearImageMemCache(); [shareUtil instance].tencentShareHandler = completion; shareUrl = [NSString stringWithFormat:@"%@",shareUrl]; imageUrl = [NSString stringWithFormat:@"%@",imageUrl]; if (type == TencentWeboShareType) { if (imageUrl && !isBlankString(imageUrl)) { if ([imageUrl hasPrefix:@"http"]) { [Remote downloadFileAsyncWithBlock:imageUrl actionTag:0 filePath:CachePath(getImageNameFromURL(imageUrl)) completion:^(BOOL success, NSString *message, id responseData) { if (success) { WeiBo_add_pic_t_POST *weiBoPOST = [[WeiBo_add_pic_t_POST alloc]init]; /** * 用户编辑过的内容 +【澎湃新闻:文章标题】 + 文章的WAP的URL (直播的话,为直播入口文章的WAP的URL)+ (分享自@澎湃新闻) */ NSString *styleStr = @""; if (style == shareNewsStyle) { styleStr = @"澎湃新闻"; }else if (style == shareLiveStyle) { styleStr = @"澎湃直播"; }else if (style == shareSpecicalTopicStyle) { styleStr = @"澎湃专题"; }else if (style == shareTopicStyle) { styleStr = @"澎湃问吧"; } NSString *content = [NSString stringWithFormat:@"【%@:%@】%@ (分享自@澎湃新闻)#我读澎湃#",styleStr,title,shareUrl]; weiBoPOST.param_content = content; if (responseData) { NSData *imgData = [NSData dataWithContentsOfFile:responseData]; weiBoPOST.param_pic = [UIImage imageWithData:imgData]; weiBoPOST.param_compatibleflag = @"0x2|0x4|0x8|0x20"; if(NO == [[shareUtil instance].tencentOAuth sendAPIRequest:weiBoPOST callback:[shareUtil instance]]) { [shareUtil showInvalidTokenOrOpenIDMessage:weiBoPOST]; } } }else { ShowTextMessage(@"分享失败"); if (completion) completion(NO); } }]; }else { UIImage *image = Image(imageUrl); WeiBo_add_pic_t_POST *weiBoPOST = [[WeiBo_add_pic_t_POST alloc]init]; /** * 用户编辑过的内容 +【澎湃新闻:文章标题】 + 文章的WAP的URL (直播的话,为直播入口文章的WAP的URL)+ (分享自@澎湃新闻) */ NSString *styleStr = @""; if (style == shareNewsStyle) { styleStr = @"澎湃新闻"; }else if (style == shareLiveStyle) { styleStr = @"澎湃直播"; }else if (style == shareSpecicalTopicStyle) { styleStr = @"澎湃专题"; }else if (style == shareTopicStyle) { styleStr = @"澎湃问吧"; } NSString *content = [NSString stringWithFormat:@"【%@:%@】%@ (分享自@澎湃新闻)#我读澎湃#",styleStr,title,shareUrl]; weiBoPOST.param_content = content; NSData *imgData = UIImageJPEGRepresentation(image, 1.0f); weiBoPOST.param_pic = [UIImage imageWithData:imgData]; weiBoPOST.param_compatibleflag = @"0x2|0x4|0x8|0x20"; if(NO == [[shareUtil instance].tencentOAuth sendAPIRequest:weiBoPOST callback:[shareUtil instance]]) { [shareUtil showInvalidTokenOrOpenIDMessage:weiBoPOST]; } } }else { WeiBo_add_pic_t_POST *weiBoPOST = [[WeiBo_add_pic_t_POST alloc]init]; /** * 用户编辑过的内容 +【澎湃新闻:文章标题】 + 文章的WAP的URL (直播的话,为直播入口文章的WAP的URL)+ (分享自@澎湃新闻) */ NSString *styleStr = @""; if (style == shareNewsStyle) { styleStr = @"澎湃新闻"; }else if (style == shareLiveStyle) { styleStr = @"澎湃直播"; }else if (style == shareSpecicalTopicStyle) { styleStr = @"澎湃专题"; }else if (style == shareTopicStyle) { styleStr = @"澎湃问吧"; } NSString *content = [NSString stringWithFormat:@"【%@:%@】%@ (分享自@澎湃新闻)#我读澎湃#",styleStr,title,shareUrl]; weiBoPOST.param_content = content; weiBoPOST.param_pic = Image(@"other/paperLogo.png"); weiBoPOST.param_compatibleflag = @"0x2|0x4|0x8|0x20"; if(NO == [[shareUtil instance].tencentOAuth sendAPIRequest:weiBoPOST callback:[shareUtil instance]]) { [shareUtil showInvalidTokenOrOpenIDMessage:weiBoPOST]; } } }else if (type == QQFriendShareType) { if (imageUrl && !isBlankString(imageUrl)) { if (imageUrl && !isBlankString(imageUrl)) { if ([imageUrl hasPrefix:@"http"]) { QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:shareUrl] title:title description:summary previewImageURL:[NSURL URLWithString:imageUrl]]; SendMessageToQQReq *qqReq = [SendMessageToQQReq reqWithContent:newsObj]; QQApiSendResultCode code = [QQApiInterface sendReq:qqReq]; [[shareUtil instance] handleSendResult:code]; }else { UIImage *image = Image(imageUrl); QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:shareUrl] title:title description:summary previewImageData:UIImageJPEGRepresentation(image, 1.0f)]; SendMessageToQQReq *qqReq = [SendMessageToQQReq reqWithContent:newsObj]; QQApiSendResultCode code = [QQApiInterface sendReq:qqReq]; [[shareUtil instance] handleSendResult:code]; } } }else { QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:shareUrl] title:title description:summary previewImageData:UIImagePNGRepresentation(Image(@"other/paperLogo.png"))]; SendMessageToQQReq *qqReq = [SendMessageToQQReq reqWithContent:newsObj]; QQApiSendResultCode code = [QQApiInterface sendReq:qqReq]; [[shareUtil instance] handleSendResult:code]; } }else if (type == QQZoneShareType) { if (imageUrl && !isBlankString(imageUrl)) { if ([imageUrl hasPrefix:@"http"]) { QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:shareUrl] title:title description:summary previewImageURL:[NSURL URLWithString:imageUrl]]; SendMessageToQQReq *qqReq = [SendMessageToQQReq reqWithContent:newsObj]; QQApiSendResultCode code = [QQApiInterface SendReqToQZone:qqReq]; [[shareUtil instance] handleSendResult:code]; }else { UIImage *image = Image(imageUrl); QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:shareUrl] title:title description:summary previewImageData:UIImageJPEGRepresentation(image, 1.0f)]; SendMessageToQQReq *qqReq = [SendMessageToQQReq reqWithContent:newsObj]; QQApiSendResultCode code = [QQApiInterface SendReqToQZone:qqReq]; [[shareUtil instance] handleSendResult:code]; } }else { QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:shareUrl] title:title description:summary previewImageData:UIImagePNGRepresentation(Image(@"other/paperLogo.png"))]; SendMessageToQQReq *qqReq = [SendMessageToQQReq reqWithContent:newsObj]; QQApiSendResultCode code = [QQApiInterface SendReqToQZone:qqReq]; [[shareUtil instance] handleSendResult:code]; } } } + (void) QQConnectShareWithImage:(UIImage*)image title:(NSString*)title comment:(NSString*)commentString shareUrl:(NSString*)shareUrl shareStyle:(shareStyle)style shareType:(shareAndLoginType)type completion:(void (^)(BOOL isSuccess))completion { clearImageMemCache(); [shareUtil instance].tencentShareHandler = completion; if (type == QQFriendShareType) { QQApiImageObject *imageObj = [QQApiImageObject objectWithData:UIImageJPEGRepresentation(image, 1.0f) previewImageData:nil title:title description:nil]; SendMessageToQQReq *qqReq = [SendMessageToQQReq reqWithContent:imageObj]; QQApiSendResultCode code = [QQApiInterface sendReq:qqReq]; [[shareUtil instance] handleSendResult:code]; }else if (type == QQZoneShareType) { QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:shareUrl] title:@"分享自澎湃新闻" description:nil previewImageData:UIImageJPEGRepresentation(image, 1.0f)]; SendMessageToQQReq *qqReq = [SendMessageToQQReq reqWithContent:newsObj]; QQApiSendResultCode code = [QQApiInterface SendReqToQZone:qqReq]; [[shareUtil instance] handleSendResult:code]; } } #pragma mark - check method + (BOOL) isHaveWeChatClient{ if (![WXApi isWXAppInstalled]) { return NO; }else return YES; } //检查QQ授权是否过期 + (void)showInvalidTokenOrOpenIDMessage:(TCAPIRequest*)lastReq { ShowTextMessage(@"可能授权已过期,请重新获取"); [shareUtil qqLogin:^(BOOL isSuccess, NSDictionary *dic) { if (isSuccess) { if(NO == [[shareUtil instance].tencentOAuth sendAPIRequest:lastReq callback:[shareUtil instance]]) { [shareUtil showInvalidTokenOrOpenIDMessage:lastReq]; } } }]; } +(BOOL)isHaveQQClient { if ([TencentOAuth iphoneQQInstalled]) { return YES; }else return NO; } + (NSString*) getTokenWithType:(shareAndLoginType)type { NSString *token = @""; if (type == weChatLoginType || type == weChatSessionShareType || type == weChatTimeLineShareType) { token =[[ShareSDK getCredentialWithType:ShareTypeWeixiSession] token]; }else if (type == sinaWeiBoLoginType || type == sinaWeiBoShareType) { token = [[ShareSDK getCredentialWithType:ShareTypeSinaWeibo] token]; }else if (type == renrenLoginType || type == renrenShareType) { token = [[ShareSDK getCredentialWithType:ShareTypeRenren] token]; }else if (type == QQLoginType || type == QQFriendShareType || type == QQZoneShareType || type == TencentWeboShareType) { token = [[shareUtil instance].tencentOAuth accessToken]; }else if (type == evernoteLoginType || type == evernoteShareType) { token = [[ShareSDK getCredentialWithType:ShareTypeEvernote] token]; }else if (type == youdaoLoginType || type == youdaoShareType) { token = [[ShareSDK getCredentialWithType:ShareTypeYouDaoNote] token]; }else if (type == pocketShareType || type == pocketLoginType) { token = [[ShareSDK getCredentialWithType:ShareTypePocket] token]; }else if (type == doubanLoginType || type == doubanShareType){ token = [[ShareSDK getCredentialWithType:ShareTypeDouBan] token]; } return token; } #pragma mark - tencentSession delegate /** * 登陆出现网络问题 */ - (void)tencentDidNotNetWork { ShowTextMessage(@"网络错误,无法完成授权"); if (self.tencentLoginHandler) { self.tencentLoginHandler(NO,nil); self.tencentLoginHandler = nil; } } /** * 登陆成功回调 */ - (void)tencentDidLogin { [self.tencentOAuth getUserInfo]; } /** * 登录失败后的回调 * param cancelled 代表用户是否主动退出登录 */ - (void)tencentDidNotLogin:(BOOL)cancelled { ShowTextMessage(@"授权失败"); if (self.tencentLoginHandler) { self.tencentLoginHandler(NO,nil); } } /** * 退出登录的回调 */ - (void)tencentDidLogout { TPLOG(@"退出QQ互联成功"); } /** * 获取用户个人信息回调 * \param response API返回结果,具体定义参见sdkdef.h文件中\ref APIResponse * \remarks 正确返回示例: \snippet example/getUserInfoResponse.exp success * 错误返回示例: \snippet example/getUserInfoResponse.exp fail */ - (void)getUserInfoResponse:(APIResponse*) response { id userData = response.jsonResponse; TPLOG(@"授权成功"); NSString *token = [shareUtil getTokenWithType:QQLoginType]; TPLOG(@"token:%@ userInfo:%@",token,userData); if (self.tencentLoginHandler) { self.tencentLoginHandler(YES,userData); } } //需要增量授权 - (BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)auth withPermissions:(NSArray *)permissions { [auth incrAuthWithPermissions:permissions]; return NO; } /** * 社交API统一回调接口 * \param response API返回结果,具体定义参见sdkdef.h文件中\ref APIResponse * \param message 响应的消息,目前支持‘SendStory’,‘AppInvitation’,‘AppChallenge’,‘AppGiftRequest’ */ - (void)responseDidReceived:(APIResponse*)response forMessage:(NSString *)message { } - (void)addShareResponse:(APIResponse *)response { // ShowMessage(@"授权失败",NO); ShowTextMessage(response.message); } #pragma mark - QQApiInterfaceDelegate /** 处理来至QQ的请求 */ - (void)onReq:(QQBaseReq *)req { switch (req.type) { case EGETMESSAGEFROMQQREQTYPE: { break; } default: { break; } } } /** 处理来至QQ的响应 */ - (void)onResp:(QQBaseResp *)resp { if ([resp.result intValue] == 0) { // ShowMessage(@"分享成功",YES); /** * bug:5805,5804 */ if (self.tencentShareHandler) { self.tencentShareHandler(YES); self.tencentShareHandler = nil; } }else { // ShowTextMessage(@"分享失败"); /** * bug:5805,5804 */ if (self.tencentShareHandler) { self.tencentShareHandler(NO); self.tencentShareHandler = nil; } } } - (void)isOnlineResponse:(NSDictionary *)response { } //腾讯微博回调 - (void)cgiRequest:(TCAPIRequest *)request didResponse:(APIResponse *)response { TPLOG(@"%@",response); if (response.detailRetCode == 0) { ShowMessage(@"分享成功",YES); if (self.tencentShareHandler) { self.tencentShareHandler(YES); self.tencentShareHandler = nil; } }else { ShowTextMessage(@"分享失败"); if (self.tencentShareHandler) { self.tencentShareHandler(NO); self.tencentShareHandler = nil; } } } #pragma mark - handler qq request - (void)handleSendResult:(QQApiSendResultCode)sendResult { switch (sendResult) { case EQQAPIAPPNOTREGISTED: { ShowTextMessage(@"App未注册"); break; } case EQQAPIMESSAGECONTENTINVALID: case EQQAPIMESSAGECONTENTNULL: case EQQAPIMESSAGETYPEINVALID: { ShowTextMessage(@"发送参数错误"); break; } case EQQAPIQQNOTINSTALLED: { ShowTextMessage(@"未安装手机QQ"); break; } case EQQAPIQQNOTSUPPORTAPI: { ShowTextMessage(@"API接口不支持"); break; } case EQQAPISENDFAILD: { ShowTextMessage(@"发送失败"); break; } default: { ShowTextMessage(@"正在发送"); break; } } } #pragma mark - remotedelegate - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { } @end