iOS开发-APP跳转微信公众号方法实现
首先感谢文扬_的思路分享。我是按照第一种方法做的。(想看效果下一个沪江网校APP在我的里面点击我的学生证即可查看)
我直接下载了微信SDK1.8.3范例代码一番配置后直接在代码中修改。
主要分为三个步骤
- 发起微信消息订阅
在SendMsgToWeChatViewController中定位到subscription方法记得把1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20- (void)subscription
{
[UIAlertView requestWithTitle:@"请输入scene" message:@"scene" defaultText:@"1000" sure:^(UIAlertView *alertView, NSString *text) {
UInt32 scene = (UInt32)[text integerValue];
[UIAlertView requestWithTitle:@"输入templateId" message:@"templateId" defaultText:@"这里输入templateId" sure:^(UIAlertView *alertView, NSString *text) {
NSString *templateId = text;
[UIAlertView requestWithTitle:@"请输入reserved" message:nil defaultText:@"" sure:^(UIAlertView *alertView, NSString *text) {
NSString *reserved = text;
WXSubscribeMsgReq *req = [[WXSubscribeMsgReq alloc] init];
req.scene = scene;
req.templateId = templateId;
req.reserved = reserved;
[WXApi sendReq:req];
}];
}];
}];
}defaultText改成你的APP一次性订阅消息的模板id
具体各个属性有啥意思去文档中找吧
调用这个方法跳转微信,用户点击授权跳转回来会在- (void)managerDidRecvSubscribeMsgResponse:(WXSubscribeMsgResp *)response获得templateId openId scene三个参数
- 获取授权Token
GET调用接口获取AccessToken
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
1 | [Request getRequestByServiceUrl:@"https://api.weixin.qq.com/cgi-bin/token?" |
- 发送订阅消息
基于前两步获得的accessTokentemplateIdopenIdscene
POST调用接口https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=ACCESS_TOKEN
1 | [Request postRequestByServiceUrl:@"https://api.weixin.qq.com/cgi-bin/message/template/subscribe?" |
参数简单说明
{
“touser”:”OPENID”,//openid
“template_id”:”TEMPLATE_ID”,//
“url”:”URL”,//跳转url
“scene”:”SCENE”,
“title”:”TITLE”,//消息名字
“data”:{
“content”:{
“value”:”VALUE”,//消息主体
“color”:”COLOR”//字体颜色 例:#173177
}
}
}
###注意点
- bundleid 和平台注册的要一致
- 把URL type改成wx…… 微信平台的appid
最终效果:
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Welcome Keven's Blog!