導語:
完成原生QQ的SDK集成后,接下來就是微信的了座舍,這兩家的SDK我懷疑是同個人寫的,如此相像,雖不難贬养,但還是有一些坑需要注意团驱。
微信 SDK官方文檔
實現(xiàn)微信登錄
一挣输、準備工作
- 在微信·開放平臺,注冊成為開發(fā)者疮薇。
- 創(chuàng)建應用,申請你的AppID和AppSecret我注。
二按咒、集成SDK
下載好完整包,如圖導入工程需要的文件但骨。
三励七、配置工程
1. 添加SDK依賴的系統(tǒng)庫文件
SystemConfiguration.framework
Security.framework
CFNetwork.framework
CoreTelephony.framework
libsqlite3.0.tbd
libstdc++.tbd
libz.tbd
libc++.tbd
2. 修改必要的工程配置屬性
在工程配置中的Build Settings
一欄中找到Linking
配置區(qū),給Other Linker Flags
配置項添加屬性值-Objc
奔缠、-all_load
3. 修改Info.plist文件
在XCode中掠抬,選中TARGETS
一欄,在Info
標簽欄中找到URL Types
,添加一條新的URL scheme
校哎。(必須填寫
Identifier: wexin
URL Schemes: wx + "appid"
想要實現(xiàn)應用間跳轉两波,而不是打開一個登陸網(wǎng)頁,在Info.plist
中添加LSApplicationQueriesSchemes
:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>weixin</string>
</array>
四闷哆、代碼實現(xiàn)
1.AppDelegate
在AppDelegate中添加頭文件腰奋,并重寫AppDelegate的handleOpenURL
和openURL
方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:@"appid"];
return YES;
}
-
iOS9之前,分別重寫
handleOpenURL && openURL
方法//handleOpenURL(ios10已棄用) NS_DEPRECATED_IOS(2_0, 9_0, "Please use application:openURL:options:") - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [WXApi handleOpenURL:url delegate:self]; } //openURL(iOS10已棄用) NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:") - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ return [WXApi handleOpenURL:url delegate:self]; }
-
iOS9之后抱怔,
handleOpenURL && openURL
合成為同一個方法//handleOpenURL && openURL - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{ return [WXApi handleOpenURL:url delegate:self]; }
2.實現(xiàn)代理方法
- (void)onReq:(BaseReq *)req{
NSLog(@"wx onReq");
}
- (void)onResp:(BaseResp *)resp{
NSLog(@"wx onResp");
//登錄 分享 都走同一個回調
if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp* authResp = (SendAuthResp *)resp;
[self loginResponse:authResp];
}
}
- (void)loginResponse:(SendAuthResp*)response{
switch (response.errCode) {
case WXSuccess:{
[self getUserToken:response.code];
}
break;
case WXErrCodeCommon:
break;
case WXErrCodeUserCancel:
break;
case WXErrCodeSentFail:
break;
case WXErrCodeAuthDeny:
break;
case WXErrCodeUnsupport:
break;
}
}
3.請求code劣坊,通過login點擊事件
-(void)sendAuthRequest{
//構造SendAuthReq結構體
SendAuthReq* req =[[[SendAuthReq alloc ] init ] autorelease ];
req.scope = @"snsapi_userinfo" ;
req.state = @"123" ;
//第三方向微信終端發(fā)送一個SendAuthReq消息結構
[WXApi sendReq:req];
}
4.通過code獲取access_token
- (void)getUserToken:(NSString*)code{
NSString* url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WXAppID,WXSecret,code];
//獲取第一步的code后,請求以下鏈接獲取access_token:
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE",
"unionid":"o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
//將獲取到的userToken && openID 繼續(xù)網(wǎng)絡請求獲取userInfo
[self getUserInfo:response[@"access_token"] openID:response[@"openid"]];
}
參數(shù) | 說明 |
---|---|
access_token | 接口調用憑證(有效期30天) |
expires_in | access_token接口調用憑證超時時間屈留,單位(秒) |
refresh_token | 用戶刷新access_token |
openid | 授權用戶唯一標識 |
scope | 用戶授權的作用域局冰,使用逗號(,)分隔 |
unionid | 當且僅當該移動應用已獲得該用戶的userinfo授權時,才會出現(xiàn)該字段 |
5.刷新access_token有效期
獲取第一步的code后灌危,請求以下鏈接進行refresh_token:
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
6.根據(jù)獲取到的userToken && openID 繼續(xù)網(wǎng)絡請求獲取userInfo
- (void)getUserInfo:(NSString*)token openID:(NSString*)openID{
NSString* url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID];
//網(wǎng)絡請求 返回response包含UserInfo
}
實現(xiàn)微信分享
步驟同上一康二、二、三
微信 SDK官方文檔
代碼實現(xiàn)
1.AppDelegate
設置分享支持類型
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:@"appid"];
UInt64 typeFlag = MMAPP_SUPPORT_TEXT | MMAPP_SUPPORT_PICTURE |
MMAPP_SUPPORT_LOCATION | MMAPP_SUPPORT_VIDEO |
MMAPP_SUPPORT_AUDIO | MMAPP_SUPPORT_WEBPAGE |
MMAPP_SUPPORT_DOC | MMAPP_SUPPORT_DOCX |
MMAPP_SUPPORT_PPT | MMAPP_SUPPORT_PPTX |
MMAPP_SUPPORT_XLS | MMAPP_SUPPORT_XLSX |
MMAPP_SUPPORT_PDF;
[WXApi registerAppSupportContentFlag:typeFlag];
return YES;
}
2.實現(xiàn)代理方法
- (void)onReq:(BaseReq *)req{
NSLog(@"wx onReq");
}
- (void)onResp:(BaseResp *)resp{
NSLog(@"wx onResp");
//登錄 分享 都走同一個回調
if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
SendMessageToWXResp* messageResp = (SendMessageToWXResp *)resp;
[self shareResponse:messageResp];
}
}
- (void)shareResponse:(SendAuthResp*)response{
switch (response.errCode) {
case WXSuccess:{
//分享成功
}
break;
case WXErrCodeCommon:
break;
case WXErrCodeUserCancel:
break;
case WXErrCodeSentFail:
break;
case WXErrCodeAuthDeny:
break;
case WXErrCodeUnsupport:
break;
}
}
3.分享示例
WXSceneSession
發(fā)送到聊天界面
WXSceneTimeline
發(fā)送到朋友圈
WXSceneFavorite
添加到微信收藏
-
文字
// 第三方程序發(fā)送消息至微信終端程序的消息結構體 SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.text = @"分享的內(nèi)容"; // 發(fā)送消息的類型乍狐,包括文本消息和多媒體消息兩種赠摇,兩者只能選擇其一,不能同時發(fā)送文本和多媒體消息 req.bText = YES; req.scene = WXSceneTimeline; [WXApi sendReq:req];
-
圖片
WXMediaMessage *message = [WXMediaMessage message]; [message setThumbImage:[UIImage imageNamed:@"圖片.png"]]; WXImageObject *imageObject = [WXImageObject object]; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res1" ofType:@"jpg"]; // 圖片真實數(shù)據(jù)內(nèi)容 imageObject.imageData = [NSData dataWithContentsOfFile:filePath]; message.mediaObject = imageObject; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneTimeline;// 分享到朋友圈 [WXApi sendReq:req];
-
音樂
WXMediaMessage *message = [WXMediaMessage message]; message.title = @"音樂標題"; message.description = @"音樂描述"; [message setThumbImage:[UIImage imageNamed:@"縮略圖.jpg"]]; WXMusicObject *musicObject = [WXMusicObject object]; musicObject.musicUrl = @"音樂url"; musicObject.musicLowBandDataUrl = @"音樂lowband數(shù)據(jù)url地址"; musicObject.musicDataUrl = @"音樂數(shù)據(jù)url"; musicObject.musicLowBandDataUrl = @"音樂lowband數(shù)據(jù)url地址"; message.mediaObject = musicObject; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneTimeline; [WXApi sendReq:req];
-
網(wǎng)頁
WXMediaMessage *message = [WXMediaMessage message]; message.title = @"標題"; message.description = @"描述"; [message setThumbImage:[UIImage imageNamed:@"res2.png"]]; WXWebpageObject *webpageObject = [WXWebpageObject object]; webpageObject.webpageUrl = @"https://opne.weixin.qq.com"; message.mediaObject = webpageObject; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneTimeline; [WXApi sendReq:req];
結束語
-
如果同時集成QQ SDK和微信 SDK浅蚪,需要注意兩者的回調方法會沖突藕帜。解決辦法可以分別創(chuàng)建對應的管理類來管理代理方法。
-(void)onReq:(BaseReq *)req; -(void)onResp:(BaseResp *)resp;
微信登錄和分享的回調方法為同一個
純文字分享可以跳轉惜傲,其他類型無法跳轉時洽故,請注意縮略圖(thumbImage)大小是否超過32kb (重要