微信大大的一些用于登录、分享和支付的硬性前提要求:
- 不论是接入微信的登录、支付还是分享都必须要提前进行
应用审核
。 - 只有审核完成后才能有AppID和App Secret。
- 并且如果需要获取更高的权限,必须要进行申请审核 (还要交钱)。
-
微信开发者平台
提交的应用中的审核官网地址可填如下: http://www.apple.com/cn/itunes/ 公司官网地址即可。
微信开发者平台
SDK资源包的下载: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&token=&lang=zh_CN
微信登录、分享集成接入指南: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=&lang=zh_CN
微信登录、分享和支付的SDK,包含如下文件:
接入微信给工程添加相应的配置
1.开发者需要链接下面的系统库: Link Frameworks and Libraries
2.在工程文件中选择 Taeget-->Build Setting
,在"Other Linker Flags"中加入"-Objc -all_load"。 在 Search Paths
中添加 libWeChatSDK.a ,WXApi.h,WXApiObject.h,文件所在位置。
3.在Xcode中,选择你的工程设置项,选中 TARGETS-->info-->URL type
“添加“ URL scheme”为你所注册的应用程序id(应用程序AppID在微信开发者平台创建移动应用)
4.在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的 LSApplicationQueriesSchemes
添加 weixin和wechat
两个白名单
5.允许网络传输: iOS9中新增App Transport Security(简称ATS)特性, 主要使到原来请求的时候用到的HTTP,都转向TLS1.2协议进行传输。这也意味着所有的HTTP协议都强制使用了HTTPS协议进行传输。需要在Info.plist新增一段用于控制ATS的配置:
NSAppTransportSecurity
NSAllowsArbitraryLoads
使用微信SDK具体实现代码: (编写此文档时使用WeChatSDK 1.8.3版本)
AppDelegate.m
中注册微信创建的移动应用ID
注意: 在未获得创建的移动应用ID时,可以使用 Wechat SDK Demo的AppID和Bundle ID操作实现相应流程
// 注册AppID
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[WXApi registerApp:@"wxd930ea5d5a258f4f"];
}
// 微信回调
// iOS 9.0之后
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{
NSLog(@"log-- xxx openURL:xxx");
/* 微信登录和分享 */
if ([WXApi handleOpenURL:url delegate:[[WeChatHandleURLDelegate alloc]init]]) {
return YES;
}
}
// iOS 9.0 之前
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
/* 微信登录和分享 */
// `WeChatHandleURLDelegate ` 为 `WXApiDelegate`代理文件
if ([WXApi handleOpenURL:url delegate:[[WeChatHandleURLDelegate alloc]init]]) {
return YES;
}
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
/* 微信登录和分享 */
if ([WXApi handleOpenURL:url delegate:[[WeChatHandleURLDelegate alloc]init]]) {
return YES;
}
}
创建并设置代理文件
// set
/**
* 是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。
*/
-(void) onReq:(BaseReq*)req{
NSLog(@"log--%@--onReq:%@",self,req);
if([req isKindOfClass:[GetMessageFromWXReq class]])
{
// 微信请求App提供内容, 需要app提供内容后使用sendRsp返回
NSString *strTitle = [NSString stringWithFormat:@"微信请求App提供内容"];
NSString *strMsg = @"微信请求App提供内容,App要调用sendResp:GetMessageFromWXResp返回给微信";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
alert.tag = 1000;
[alert show];
}
else if([req isKindOfClass:[ShowMessageFromWXReq class]])
{
ShowMessageFromWXReq* temp = (ShowMessageFromWXReq*)req;
WXMediaMessage *msg = temp.message;
//显示微信传过来的内容
WXAppExtendObject *obj = msg.mediaObject;
NSString *strTitle = [NSString stringWithFormat:@"微信请求App显示内容"];
NSString *strMsg = [NSString stringWithFormat:@"标题:%@ \n内容:%@ \n附带信息:%@ \n缩略图:%u bytes\n\n", msg.title, msg.description, obj.extInfo, msg.thumbData.length];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([req isKindOfClass:[LaunchFromWXReq class]])
{
//从微信启动App
NSString *strTitle = [NSString stringWithFormat:@"从微信启动"];
NSString *strMsg = @"这是从微信启动的消息";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
/**
* 如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调
*/
-(void) onResp:(BaseResp*)resp{
NSLog(@"log--%@--onReq:%@",self,resp);
// 回调在发送成功和取消发送时都会调用
if([resp isKindOfClass:[SendMessageToWXResp class]]){
NSString *strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
调用WeChat
微信
登录方法
目前移动应用上德微信登录只提供 原生的登录方式
,需要用户 安装微信客户端
才能配合使用。
对于iOS应用,考虑到iOS应用商店审核指南中的相关规定,建议开发者接入微信登录时,先检测用户手机是否已经安装
微信客户端(使用sdk中的isWXAppInstall函数),对于未安装的用户隐藏微信 登录按钮,只提供其他登录方式。
以下代码内容,类似于WeChat SDK Demo内容,但不完全等同。
各个场景下的不同的调用方法 -- 三个场景下不同应用 -- 会话、朋友圈、收藏
#pragma mark - target
// 切换场景方法
- (void)onSelectSessionScene:(UIButton *)selectBtn{
if (selectBtn.tag == 0) {
_scene = WXSceneSession;
self.tipsLab.text = @"分享场景:会话";
}else if(selectBtn.tag == 1){
_scene = WXSceneTimeline;
self.tipsLab.text = @"分享场景:朋友圈";
}else if(selectBtn.tag == 2){
_scene = WXSceneSession;
self.tipsLab.text = @"分享场景:收藏";
}
}
#pragma mark - send method
#pragma mark send text
// text -- `sendrTextContent`/`RespTextContent`都可以发送文本消息
- (void) sendTextContent{
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.text = @"人文的东西并不是体现在你看得到的方面,它更多的体现在你看不到的那些方面,它会影响每一个功能,这才是最本质的。但是,对这点可能很多人没有思考过,以为人文的东西就是我们搞一个很小清新的图片什么的。”综合来看,人文的东西其实是贯穿整个产品的脉络,或者说是它的灵魂所在。";
req.bText = YES;
req.scene = _scene;
[WXApi sendReq:req];
}
-(void) RespTextContent{
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.text = @"人文的东西并不是体现在你看得到的方面,它更多的体现在你看不到的那些方面,它会影响每一个功能,这才是最本质的。但是,对这点可能很多人没有思考过,以为人文的东西就是我们搞一个很小清新的图片什么的。”综合来看,人文的东西其实是贯穿整个产品的脉络,或者说是它的灵魂所在。";
resp.bText = YES;
[WXApi sendResp:resp];
}
#pragma mark image/photo
// image/photo
- (void) sendImageContent{
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"res5thumb.png"]];
WXImageObject *ext = [WXImageObject object];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5thumb" ofType:@"png"];
NSLog(@"filepath :%@",filePath);
ext.imageData = [NSData dataWithContentsOfFile:filePath];
//UIImage* image = [UIImage imageWithContentsOfFile:filePath];
UIImage* image = [UIImage imageWithData:ext.imageData];
ext.imageData = UIImagePNGRepresentation(image);
// UIImage* image = [UIImage imageNamed:@"res5thumb.png"];
// ext.imageData = UIImagePNGRepresentation(image);
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
- (void) RespImageContent{
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"res5thumb.png"]];
WXImageObject *ext = [WXImageObject object];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5thumb" ofType:@"png"];
ext.imageData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}
#pragma mark link
//link
- (void) sendLinkContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"专访张小龙:产品之上的世界观";
message.description = @"微信的平台化发展方向是否真的会让这个原本简洁的产品变得臃肿?在国际化发展方向上,微信面临的问题真的是文化差异壁垒吗?腾讯高级副总裁、微信产品负责人张小龙给出了自己的回复。";
[message setThumbImage:[UIImage imageNamed:@"res2.png"]];
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = @"http://tech.qq.com/zt2012/tmtdecode/252.htm";
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
-(void) RespLinkContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"专访张小龙:产品之上的世界观";
message.description = @"微信的平台化发展方向是否真的会让这个原本简洁的产品变得臃肿?在国际化发展方向上,微信面临的问题真的是文化差异壁垒吗?腾讯高级副总裁、微信产品负责人张小龙给出了自己的回复。";
[message setThumbImage:[UIImage imageNamed:@"res2.png"]];
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = @"http://tech.qq.com/zt2012/tmtdecode/252.htm";
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}
#pragma mark music
// music
-(void) sendMusicContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"一无所有";
message.description = @"崔健";
[message setThumbImage:[UIImage imageNamed:@"res3.jpg"]];
WXMusicObject *ext = [WXMusicObject object];
ext.musicUrl = @"http://y.qq.com/i/song.html#p=7B22736F6E675F4E616D65223A22E4B880E697A0E68980E69C89222C22736F6E675F5761704C69766555524C223A22687474703A2F2F74736D7573696334382E74632E71712E636F6D2F586B30305156342F4141414130414141414E5430577532394D7A59344D7A63774D4C6735586A4C517747335A50676F47443864704151526643473444442F4E653765776B617A733D2F31303130333334372E6D34613F7569643D3233343734363930373526616D703B63743D3026616D703B636869643D30222C22736F6E675F5769666955524C223A22687474703A2F2F73747265616D31342E71716D757369632E71712E636F6D2F33303130333334372E6D7033222C226E657454797065223A2277696669222C22736F6E675F416C62756D223A22E4B880E697A0E68980E69C89222C22736F6E675F4944223A3130333334372C22736F6E675F54797065223A312C22736F6E675F53696E676572223A22E5B494E581A5222C22736F6E675F576170446F776E4C6F616455524C223A22687474703A2F2F74736D757369633132382E74632E71712E636F6D2F586C464E4D313574414141416A41414141477A4C36445039536A457A525467304E7A38774E446E752B6473483833344843756B5041576B6D48316C4A434E626F4D34394E4E7A754450444A647A7A45304F513D3D2F33303130333334372E6D70333F7569643D3233343734363930373526616D703B63743D3026616D703B636869643D3026616D703B73747265616D5F706F733D35227D";
ext.musicDataUrl = @"http://stream20.qqmusic.qq.com/32464723.mp3";
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
-(void) RespMusicContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"一无所有";
message.description = @"崔健";
[message setThumbImage:[UIImage imageNamed:@"res3.jpg"]];
WXMusicObject *ext = [WXMusicObject object];
ext.musicUrl = @"http://y.qq.com/i/song.html#p=7B22736F6E675F4E616D65223A22E4B880E697A0E68980E69C89222C22736F6E675F5761704C69766555524C223A22687474703A2F2F74736D7573696334382E74632E71712E636F6D2F586B30305156342F4141414130414141414E5430577532394D7A59344D7A63774D4C6735586A4C517747335A50676F47443864704151526643473444442F4E653765776B617A733D2F31303130333334372E6D34613F7569643D3233343734363930373526616D703B63743D3026616D703B636869643D30222C22736F6E675F5769666955524C223A22687474703A2F2F73747265616D31342E71716D757369632E71712E636F6D2F33303130333334372E6D7033222C226E657454797065223A2277696669222C22736F6E675F416C62756D223A22E4B880E697A0E68980E69C89222C22736F6E675F4944223A3130333334372C22736F6E675F54797065223A312C22736F6E675F53696E676572223A22E5B494E581A5222C22736F6E675F576170446F776E4C6F616455524C223A22687474703A2F2F74736D757369633132382E74632E71712E636F6D2F586C464E4D313574414141416A41414141477A4C36445039536A457A525467304E7A38774E446E752B6473483833344843756B5041576B6D48316C4A434E626F4D34394E4E7A754450444A647A7A45304F513D3D2F33303130333334372E6D70333F7569643D3233343734363930373526616D703B63743D3026616D703B636869643D3026616D703B73747265616D5F706F733D35227D";
ext.musicDataUrl = @"http://stream20.qqmusic.qq.com/32464723.mp3";
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}
#pragma mark video
// video
-(void) sendVideoContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"乔布斯访谈";
message.description = @"饿着肚皮,傻逼着。";
[message setThumbImage:[UIImage imageNamed:@"res7.jpg"]];
WXVideoObject *ext = [WXVideoObject object];
ext.videoUrl = @"http://v.youku.com/v_show/id_XNTUxNDY1NDY4.html";
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
-(void) RespVideoContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"楚门的世界";
message.description = @"一样的监牢,不一样的门";
[message setThumbImage:[UIImage imageNamed:@"res4.jpg"]];
WXVideoObject *ext = [WXVideoObject object];
ext.videoUrl = @"http://video.sina.com.cn/v/b/65203474-2472729284.html";
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}
#pragma mark app
// app
#define BUFFER_SIZE 1024 * 100
- (void) sendAppContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"App消息";
message.description = @"这种消息只有App自己才能理解,由App指定打开方式!";
[message setThumbImage:[UIImage imageNamed:@"res2.jpg"]];
WXAppExtendObject *ext = [WXAppExtendObject object];
ext.extInfo = @"extend info ";
ext.url = @"http://www.qq.com";
Byte* pBuffer = (Byte *)malloc(BUFFER_SIZE);
memset(pBuffer, 0, BUFFER_SIZE);
NSData* data = [NSData dataWithBytes:pBuffer length:BUFFER_SIZE];
free(pBuffer);
ext.fileData = data;
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
-(void) RespAppContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"App消息";
message.description = @"这种消息只有App自己才能理解,由App指定打开方式!";
[message setThumbImage:[UIImage imageNamed:@"res2.jpg"]];
WXAppExtendObject *ext = [WXAppExtendObject object];
ext.extInfo = @"extend info ";
ext.url = @"http://weixin.qq.com";
Byte* pBuffer = (Byte *)malloc(BUFFER_SIZE);
memset(pBuffer, 0, BUFFER_SIZE);
NSData* data = [NSData dataWithBytes:pBuffer length:BUFFER_SIZE];
free(pBuffer);
ext.fileData = data;
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init];
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}
#pragma mark no gif
// no gif
- (void) sendNonGifContent
{
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"res5thumb.png"]];
WXEmoticonObject *ext = [WXEmoticonObject object];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5" ofType:@"jpg"];
ext.emoticonData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
- (void)RespNonGifContent{
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"res5thumb.png"]];
WXEmoticonObject *ext = [WXEmoticonObject object];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5" ofType:@"jpg"];
ext.emoticonData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}
#pragma mark gif
// gif
- (void) sendGifContent
{
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"res6thumb.png"]];
WXEmoticonObject *ext = [WXEmoticonObject object];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res6" ofType:@"gif"];
ext.emoticonData = [NSData dataWithContentsOfFile:filePath] ;
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
- (void)RespGifContent
{
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"res6thumb.png"]];
WXEmoticonObject *ext = [WXEmoticonObject object];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res6" ofType:@"gif"];
ext.emoticonData = [NSData dataWithContentsOfFile:filePath] ;
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}
//
- (void) RespEmoticonContent{
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"res5thumb.png"]];
WXEmoticonObject *ext = [WXEmoticonObject object];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5" ofType:@"jpg"];
ext.emoticonData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}
#pragma mark send file
// file content
- (void)sendFileContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"ML.pdf";
message.description = @"Pro CoreData";
[message setThumbImage:[UIImage imageNamed:@"res2.jpg"]];
WXFileObject *ext = [WXFileObject object];
ext.fileExtension = @"pdf";
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"ML" ofType:@"pdf"];
ext.fileData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
- (void)RespFileContent{
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"ML.pdf";
message.description = @"机器学习与人工智能学习资源导引";
[message setThumbImage:[UIImage imageNamed:@"res2.jpg"]];
WXFileObject *ext = [WXFileObject object];
ext.fileExtension = @"pdf";
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ML" ofType:@"pdf"];
ext.fileData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = ext;
GetMessageFromWXResp* resp = [[GetMessageFromWXResp alloc] init] ;
resp.message = message;
resp.bText = NO;
[WXApi sendResp:resp];
}