一. 第一步:下載微信SDK
包的內容如下:
二. 工程配置:
1.配置URL scheme
2.加入白名單
3.倒入必要的SDK
三. 代碼:
好了,准備工作已經完成,下面開始寫代碼了
在AppDelegate里面:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
/** 微信 */
WXApi.registerApp(WX_APPID, enableMTA: true)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
WXApi.handleOpen(url, delegate: self)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
WXApi.handleOpen(url, delegate: self)
return true
}
/** 微信會調 */
func onResp(_ resp: BaseResp!) {
if resp.errCode == 0 && resp.type == 0 {//授權成功
let response = resp as! SendAuthResp
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "WXLoginSuccessNotification"), object: response.code)
}
}
在登錄界面:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NotificationCenter.default.addObserver(self,selector: #selector(WXLoginSuccess(notification:)),name: NSNotification.Name(rawValue: "WXLoginSuccessNotification"),object: nil)
}
//微信登錄
@IBAction func wxLoginBtnAction(_ sender: UIButton) {
let urlStr = "weixin://"
if UIApplication.shared.canOpenURL(URL.init(string: urlStr)!) {
let red = SendAuthReq.init()
red.scope = "snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact"
red.state = "\(arc4random()%100)"
WXApi.send(red)
}else{
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL.init(string: "http://weixin.qq.com/r/qUQVDfDEVK0rrbRu9xG7")!, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
UIApplication.shared.openURL(URL.init(string: "http://weixin.qq.com/r/qUQVDfDEVK0rrbRu9xG7")!)
}
}
}
/** 微信通知 */
func WXLoginSuccess(notification:Notification) {
let code = notification.object as! String
let requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=\(WX_APPID)&secret=\(WX_APPSecret)&code=\(code)&grant_type=authorization_code"
DispatchQueue.global().async {
let requestURL: URL = URL.init(string: requestUrl)!
let data = try? Data.init(contentsOf: requestURL, options: Data.ReadingOptions())
DispatchQueue.main.async {
let jsonResult = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String,Any>
let openid: String = jsonResult["openid"] as! String
let access_token: String = jsonResult["access_token"] as! String
self.getUserInfo(openid: openid, access_token: access_token)
}
}
}
/** 獲取用戶信息 */
func getUserInfo(openid:String,access_token:String) {
let requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=\(access_token)&openid=\(openid)"
DispatchQueue.global().async {
let requestURL: URL = URL.init(string: requestUrl)!
let data = try? Data.init(contentsOf: requestURL, options: Data.ReadingOptions())
DispatchQueue.main.async {
let jsonResult = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String,Any>
print(jsonResult)
}
}
}
好了,微信用戶信息我們已經拿到了,下面只需要把這些信息發送給后台想怎么登就怎么登,哈哈哈!
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。