如何使用iOS SDK通过微信进行授权和登录? [英] How do I do authorization and login with WeChat using the iOS SDK?

查看:575
本文介绍了如何使用iOS SDK通过微信进行授权和登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用iOS SDK通过微信进行授权和登录?关于堆栈溢出或google的信息似乎很少,并且大多数文档都是中文的.

How do I do authorization and login with WeChat using the iOS SDK? There doesn't seem to be much information about this on stack overflow or google and most of the documents are in Chinese.

推荐答案

在这里选择回答我自己的问题,因为关于堆栈溢出和google的信息似乎缺乏.我希望其他人也觉得它有用.

Choosing to answer my own question here as there seems to be lack of information about this on stack overflow and google. I hope others also find it useful.

1.)按照 Suragch's 的出色解答,了解如何设置iOS SDK:

1.) Follow Suragch's excellent answer on how to setup the iOS SDK: How to add the WeChat API to a Swift project?. Make sure AppDelegate is setup as described with the func onReq(req: BaseReq!) and func onResp(resp: BaseResp!) methods implemented.

2.)要获得登录和授权,您必须下载并使用中文版的SDK.奇怪的是,登录所需的某些功能已从英文版中删除.中文SDK在这里: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&ifyverify=1&id=open1419319164&lang=zh_CN

2.) To get login and authorization working you MUST download and use the Chinese version of the SDK. Curiously some of the functions needed to login are removed from the English version. Chinese SDK here: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&lang=zh_CN

3.)首先,我们要授权我们要与微信一起使用的应用程序.可以这样完成:

3.) Firstly we want to authorize the app we want to use with WeChat. This can be done like so:

let req = SendAuthReq()
req.scope = "snsapi_userinfo" //Important that this is the same
req.state = "co.company.yourapp_wx_login" //This can be any random value
WXApi.sendReq(req)

这应该将代码返回给func onResp(resp: BaseResp!)我实现了这样的方法-触发通知:

This should return a code to func onResp(resp: BaseResp!) I implemented the method like so - triggering a notification:

func onResp(resp: BaseResp!) {
        if let authResp = resp as? SendAuthResp {
            if authResp.code != nil {
                let dict = ["response": authResp.code]
                NSNotificationCenter.defaultCenter().postNotificationName("WeChatAuthCodeResp", object: nil, userInfo: dict)             
            } else {                    
                let dict = ["response": "Fail"]
                NSNotificationCenter.defaultCenter().postNotificationName("WeChatAuthCodeResp", object: nil, userInfo: dict)                    
            }                
        } else {                
            let dict = ["response": "Fail"]
            NSNotificationCenter.defaultCenter().postNotificationName("WeChatAuthCodeResp", object: nil, userInfo: dict)
        }
    }

4.)现在,通过代码,我们可以尝试获取openID和accessToken.为此,我们需要使用appIDappSecret建立链接并执行HTTP GET请求. appIDappSecret是您在微信上注册应用程序时获得的详细信息.像这样的例子:

4.) With the code we can now try and get the openID and the accessToken. To do this we need to build a link using the appID, appSecret and do a HTTP GET request. The appID and appSecret are details you get when you register the app with WeChat. Example like so:

private let appID = "somecode2132113"
private let appSecret = "someappsecret213123"

private let accessTokenPrefix = "https://api.weixin.qq.com/sns/oauth2/access_token?"

private func buildAccessTokenLink(withCode code: String) -> String {
        return accessTokenPrefix + "appid=" + appID + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code"
    }

通过此链接,我们可以执行HTTP GET请求,并在JSON中获取openIDaccessToken. (在邮递员中尝试).我不会为此发布代码,但是我正在使用Alamofire.

With this link we can perform a HTTP GET request and obtain the openID and accessToken in the JSON. (Try it in Postman). I won't post the code for this, but I'm using Alamofire.

5.)最后,我们可以更进一步,尝试获取微信用户的昵称和个人资料照片.与之前使用在上一步中获得的openIDaccessToken创建新链接之前类似.像这样:

5.) Finally we can go one step further and try to get the WeChat user's nickname and profile photo. Very similar to before we create a new link using the openID and accessToken we obtained in the step before. Like so:

private let userInfoPrefix = "https://api.weixin.qq.com/sns/userinfo?"

private func buildUserInfoLink(withOpenID openID: String, accessToken: String) -> String {
        return userInfoPrefix + "access_token=" + accessToken + "&openid=" + openID
    }

再次执行HTTP GET请求,JSON将返回昵称和个人资料照片链接!

Again, perform a HTTP GET request and the JSON will return the nickname and profile photo link!

plus:此处的详细指南: http://www. kekearif.com/how-to-implement-ios-wechat-login/

plus: detailed guide here: http://www.kekearif.com/how-to-implement-ios-wechat-login/

这篇关于如何使用iOS SDK通过微信进行授权和登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆