pod 'WechatKit'
Install Carthage
github "starboychina/WechatKit"
Set URL scheme
In Xcode, select your project settings, select the "TARGETS" column, and add "URL scheme" to the "URL type" in the "info" tab bar for the application ID you registered on the WeChat open platform.
After IOS9, you need to add weixin to the whitelist (as shown in the picture)
Or open info.plist in source code mode and add the following content.
< key >LSApplicationQueriesSchemes</ key >
< array >
< string >wechat</ string >
< string >weixin</ string >
</ array >
AppDelegate's handleOpenURL and openURL methods:
Add import WechatKit in AppDelegate.swift
/// iOS 9 以后将弃用 请用下面的方法 #20
/// -> NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED;
func application ( _ application : UIApplication , open url : URL , sourceApplication : String ? , annotation : Any ) -> Bool {
return WechatManager . shared . handleOpenURL ( url )
// 如需要使用其他第三方可以 使用 || 连接 其他第三方库的handleOpenURL
// return WechatManager.shared.handleOpenURL(url) || TencentOAuth.HandleOpenURL(url) || WeiboSDK.handleOpenURL(url, delegate: SinaWeiboManager.shared) ......
}
/// iOS 9.0 以后请使用这个方法
/// Please use this (application:openURL:options:)
func application ( _ app : UIApplication , open url : URL , options : [ UIApplicationOpenURLOptionsKey : Any ] = [ : ] ) -> Bool {
return WechatManager . shared . handleOpenURL ( url )
// 如需要使用其他第三方可以 使用 || 连接 其他第三方库的handleOpenURL
// return WechatManager.shared.handleOpenURL(url) || TencentOAuth.HandleOpenURL(url) || WeiboSDK.handleOpenURL(url, delegate: SinaWeiboManager.shared) ......
}
WechatManager . appid = "微信开放平台,注册的应用程序id "
WechatManager . universalLink = "微信开放平台,注册的应用程序所对应的 Universal Links "
WechatManager . appSecret = "微信开放平台,注册的应用程序Secret "
WechatManager . shared . isInstalled ( )
Log in using WeChat
By default, openid and access_token will be remembered. When the token is still valid, calling checkAuth will not open the WeChat client and directly use the token and WeChat server to obtain authentication information.
WechatManager . shared . checkAuth { result in
switch result {
case . failure ( let errCode ) : //登录失败
print ( errCode )
case . success ( let value ) : //登录成功 value为([String: String]) 从微信返回的openid access_token 以及 refresh_token
print ( value ) //当前是在子线程,如需回到主线程调用 DispatchQueue.main.async { print(value) }
}
}
WechatManager . shared . getUserInfo { result in
switch result {
case . failure ( let errCode ) : //获取失败
print ( errCode )
case . success ( let value ) : //获取成功 value为([String: String]) 微信用户基本信息
print ( value ) //当前是在子线程,如需回到主线程调用 DispatchQueue.main.async { print(value) }
}
}
Log out
Since openid and access_token are remembered by default, you need to log out if you need to switch users.
WechatManager . shared . logout ( )
WechatManager . shared . shareDelegate = self
/**
分享
- parameter scence: 请求发送场景
- parameter image: 消息缩略图
- parameter title: 标题
- parameter description: 描述内容
- parameter url: 地址
- parameter extInfo: app分享信息(点击分享内容返回程序时,会传给WechatManagerShareDelegate.showMessage(message: String)
*/
WechatManager . shared . share ( scence : WXScene , image : UIImage ? , title : String , description : String , url : String ? = default , extInfo : String ? = default )
ShareDelegation
//app分享后 点击分享返回时调用
func showMessage ( message : String )