关于微信登录(Swift)
一. 第一步:下载微信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! SendAuthRespNotificationCenter.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 versionsUIApplication.shared.openURL(URL.init(string: "http://weixin.qq.com/r/qUQVDfDEVK0rrbRu9xG7")!)}}
}/** 微信通知 */
func WXLoginSuccess(notification:Notification) {let code = notification.object as! Stringlet 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! Dictionarylet openid: String = jsonResult["openid"] as! Stringlet access_token: String = jsonResult["access_token"] as! Stringself.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! Dictionaryprint(jsonResult)}}
}
好了,微信用户信息我们已经拿到了,下面只需要把这些信息发送给后台想怎么登就怎么登,哈哈哈!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
