shareSDK的介绍
在今天,很多app的开发,都会涉及到一些分享,比如微信分享,QQ分享,新浪微博分享等等。在这里介绍一个第三方的SDK ------------shareSDK.
它包含了各类的分享继承,也介绍了很多配置,详情查看官方文档---(适当参考)mob的快速集成文档。http://wiki.mob.com/sharerec%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E6%96%87%E6%A1%A3/
步骤如下:
1.去mob官网下载(自身app开发需求)需分享的sdk.
2.去各分享的开发平台,注册开发者账号。
3.将自身的APP开发添加到各类平台的开发应用中,获取相对应得APPKEY 和 APPSelect
4.根据mob的官方集成文档,在XCODE中导入相对应的类库和框架,添加参数
5.在target->building->Info->URLtype中添加各分享平台的APPKEY,便于回调
6.开辟框架头文件,用于专门导入第三方SDK的头文件,便于管理
7.开发参数头文件,用于将APPKEY或APPselcet等常用的第三方SDK参数成“宏”,便于更换
8.创建一个或多个(同一个平台分享多种类型时最好独立一个出来)的分享类,声明并实现相对应的分享方法。(对于常用和特殊的方法使用,最好归为一个整体或多个的类),便于增删改查。以保证后期版本更新的变动。
9.定义方法,最好为类方法(或静态方法),便于内存管理。
10.对于相对平台的实现,必须“command点进去看SDK的方法介绍和参数要求”,避免规格不符合要求,而无法触发相对应的方法。如下图示:
在AppDelegate.m 中添加的方法:(注:需添加对应的代理协议以及重写方法,看实际情况而定。如果在运行时,提示注册平台发生崩溃-----target-buildSeting->linking->other linker flags 中添加 -Objc , 也可提前添加) ---------http://blog.csdn.net/yqmfly/article/details/45148081
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 3 4 //添加微信应用 5 [ShareSDK connectWeChatWithAppId:IBOS_WECHAT_APPKEY 6 wechatCls:[WXApi class]]; 7 //微信登陆的时候需要初始化 8 [ShareSDK connectWeChatWithAppId:IBOS_WECHAT_APPKEY 9 appSecret:IBOS_WECHAT_APPSECRET 10 wechatCls:[WXApi class]]; 11 [WXApi registerApp:IBOS_WECHAT_APPKEY withDescription:@"weixin"]; 12 13 14 //添加新浪微博应用; 15 [ShareSDK connectSinaWeiboWithAppKey:IBOS_WEIBO_APPKEY appSecret:IBOS_WEIBO_APPSECRET redirectUri:@"http://www.baidu.com"]; 16 [ShareSDK connectSinaWeiboWithAppKey:IBOS_WEIBO_APPKEY appSecret:IBOS_WEIBO_APPSECRET redirectUri:@"http://www.baidu.com" weiboSDKCls:[WeiboSDK class]]; 17 [WeiboSDK enableDebugMode:YES]; 18 [WeiboSDK registerApp:IBOS_WEIBO_APPKEY]; 19 20 } 21 22 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 23 if ([url.scheme isEqualToString:IBOS_WECHAT_APPKEY]) { 24 return [WXApi handleOpenURL:url delegate:self]; 25 } 26 else { 27 return [ShareSDK handleOpenURL:url wxDelegate:self]; 28 } 29 } 30 31 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 32 if ([url.scheme isEqualToString:IBOS_WECHAT_APPKEY]) { 33 return [WXApi handleOpenURL:url delegate:self]; 34 } 35 else { 36 return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self]; 37 } 38 }
以下为项目期创建的类库,主要用于微信和微博分享:
1 @interface IBOSShareServer : NSObject 2 3 /** 4 * 5 * 6 * @param text 文本信息 7 * 8 * @return 邀请微信好友 只能发送纯文本信息 (可包含网址,微信会自动识别) 9 */ 10 +(BOOL)invitationWXfriendOnlyText:(NSString*)text; 11 12 13 /** 14 * 15 * 16 * @param title 分享标题 17 * @param description 分享的内容 18 * @param image 分享的logo 19 * @param url 分享的链接 20 * @param type 分享的平台类型 0-微信好友 1-微信朋友圈 2- WXSceneFavorite 21 * 22 * @return 分享内容到微信朋友圈 或者 微信好友 23 */ 24 +(BOOL)shareContentTOWXSessionOrTimelineTitle:(NSString*)title description:(NSString*)description image:(NSString*)image url:(NSString*)url type:(int)type; 25 /** 26 * 27 * 28 * @param image 分享的logo 29 * @param description 分享的内容 30 * @param url 分享的链接 31 * 32 * @return 分享内容到微博 ----内容、图片、链接 不含标题 33 */ 34 +(BOOL)shareContentToWeiBOImage:(NSString*)image description:(NSString*)description url :(NSString*)url; 35 /** 36 * 37 * 38 * @param target 响应对象 39 * @param sel 响应的方法 40 * @param name 通知注册的名字 41 * 42 * @return 检测是否安装微信 ,并且获取授权 ,注:以上参数只有在需要使用通知监听授权时才使用,不需要则传nil; 43 */ 44 45 +(BOOL)sendAuthWxTarget:(id)target sel:(SEL)sel notiName:(NSString*)name; 46 /** 47 * 48 * 49 * @param resp 微信响应的类型 50 * 51 * @return 获取微信授权后的回调 52 */ 53 + (BOOL)getAuthWXMessage:(BaseResp *)resp target:(id)target sel:(SEL)sel ; 54 55 56 @end
点m文件实现:
1 #import "IBOSShareServer.h" 2 3 static IBOSShareServer * share = nil; 4 @implementation IBOSShareServer 5 6 //邀请微信好友 只能发送纯文本信息 (可包含网址,微信会自动识别) 7 + (BOOL)invitationWXfriendOnlyText:(NSString *)text { 8 SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; 9 req.text = text; 10 req.bText = YES; 11 12 [WXApi sendReq:req]; 13 14 return YES; 15 } 16 17 //分享内容到微信朋友圈 或者 微信好友 18 + (BOOL)shareContentTOWXSessionOrTimelineTitle:(NSString *)title description:(NSString *)description image:(NSString *)image url:(NSString *)url type:(int)type { 19 20 WXMediaMessage *message = [WXMediaMessage message]; 21 message.title = title; 22 message.description = description; 23 [message setThumbImage:[UIImage imageNamed:image]]; 24 25 26 WXAppExtendObject *ext = [WXAppExtendObject object]; 27 ext.extInfo = @""; 28 ext.url = url; 29 30 ext.fileData = NULL; 31 32 message.mediaObject = ext; 33 34 35 SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; 36 req.bText = NO; 37 req.message = message; 38 switch (type) { 39 case 0: 40 { 41 req.scene = WXSceneSession;//朋友 42 43 break; 44 } 45 46 case 1: 47 { 48 req.scene = WXSceneTimeline;//朋友圈 49 break; 50 } 51 52 case 2: 53 { 54 req.scene = WXSceneFavorite;//我的收藏 55 break; 56 } 57 58 default: 59 break; 60 } 61 [WXApi sendReq:req]; 62 return YES; 63 } 64 65 //分享内容到微博 ----内容、图片、链接 不含标题 66 + (BOOL)shareContentToWeiBOImage:(NSString *)image description:(NSString *)description url:(NSString *)url { 67 UIImage *logoimage = [UIImage imageNamed:image]; 68 logoimage = [logoimage scale:0.8]; 69 NSData *imageData = UIImagePNGRepresentation(logoimage); 70 WBImageObject *imageObj = [WBImageObject object]; 71 imageObj.imageData = imageData; 72 73 WBMessageObject *message = [WBMessageObject message]; 74 NSString *str = description; 75 message.text = [str stringByAppendingString:url]; 76 message.imageObject = imageObj; 77 78 79 WBSendMessageToWeiboRequest *sendReq = [WBSendMessageToWeiboRequest requestWithMessage:message]; 80 81 [WeiboSDK sendRequest:sendReq]; 82 83 return YES; 84 } 85 86 // 检测是否安装微信 ,并且获取授权 ,注:以上参数只有在需要使用通知监听授权时才使用,不需要则传nil; 87 + (BOOL)sendAuthWxTarget:(id)target sel:(SEL)sel notiName:(NSString *)name { 88 89 if (![WXApi isWXAppInstalled]) { 90 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你没有安装微信,不能使用此功能!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; 91 92 [alert show]; 93 } 94 else { 95 SendAuthReq *req = [[SendAuthReq alloc] init]; 96 req.scope = @"snsapi_userinfo";//,snsapi_base 97 req.state = @"ibos"; //ibos_login 98 [WXApi sendReq:req]; 99 addPost(target, sel, name); 100 } 101 102 103 return YES; 104 } 105 106 // 获取微信授权后的回调 107 +(BOOL)getAuthWXMessage:(BaseResp *)resp target:(id)target sel:(SEL)sel 108 { 109 if ([resp isKindOfClass:[SendAuthResp class]]) { 110 SendAuthResp *aresp = (SendAuthResp *)resp; 111 112 if (aresp.state) { 113 if (aresp.errCode == 0) { 114 NSString *code = aresp.code; 115 116 [IBOSShareServer getAccessTokenWithCode:code target:target sel:sel]; 117 } 118 } 119 } 120 121 122 return YES; 123 } 124 125 //回调后 通过Code APPKEY APPSECRET 获取 access_token 和 openid 126 +(void)getAccessTokenWithCode:(NSString *)code target:(id)target sel:(SEL)sel { 127 128 NSString *urlString = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", IBOS_WECHAT_APPKEY, IBOS_WECHAT_APPSECRET, code]; 129 NSURL *url = [NSURL URLWithString:urlString]; 130 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 131 NSString *dataStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 132 133 NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding]; 134 135 dispatch_async(dispatch_get_main_queue(), ^{ 136 if (data) { 137 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 138 139 if ([dict objectForKey:@"errcode"]) { 140 //获取授权错误 141 MLog(@"授权错误 --------- %@", [dict objectForKey:@"errcode"]); 142 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[dict objectForKey:@"errcode"] delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil]; 143 144 [alert show]; 145 } 146 else { 147 //存储AccessToken OpenId RefreshToken 以便下次登录 148 //AccessToken 有效期为两个小时 , RefreshToken 有效期30天 149 150 // self.openID = dict[@"openid"]; 151 //使用AccessToken获取用户信息 152 [IBOSShareServer getUserIofoWithAccessToken:dict[@"access_token"] openID:dict[@"openid"] target:target sel:sel]; 153 } 154 } 155 }); 156 }); 157 } 158 159 //得到access_token openid获取用户信息 160 + (void)getUserIofoWithAccessToken:(NSString *)accessToken openID:(NSString *)openid target:(id)target sel:(SEL)sel { 161 162 NSString *userInfo = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@", accessToken, openid]; 163 NSURL *url = [NSURL URLWithString:userInfo]; 164 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 165 NSString *dataString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 166 NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding]; 167 dispatch_async(dispatch_get_main_queue(), ^{ 168 if (data) { 169 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 170 if (![dict objectForKey:@"errcode"]) { 171 performSelector(target, sel, dict); 172 }else 173 { 174 UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:dict[@"errcode"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; 175 [alert show]; 176 177 } 178 } 179 }); 180 }); 181 }