繁体   English   中英

如何使用iOS SDK进行微信授权登录?

[英]How do I do authorization and login with WeChat using the iOS SDK?

如何使用iOS SDK进行微信授权登录? 堆栈溢出或谷歌上似乎没有太多关于此的信息,而且大多数文档都是中文的。

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

1.) 按照Suragch关于如何设置 iOS SDK优秀回答: 如何将微信 API 添加到 Swift 项目? . 确保按照func onReq(req: BaseReq!)func onResp(resp: BaseResp!)方法的描述设置AppDelegate

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

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

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!)我实现了这样的方法 - 触发通知:

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。 为此,我们需要使用appSecretappID构建链接并执行 HTTP GET 请求。 appIDappSecret是您在微信注册应用程序时获得的详细信息。 像这样的例子:

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。

5.) 最后我们可以更进一步,尝试获取微信用户的昵称和头像。 与我们之前使用我们在上一步中获得的openIDaccessToken创建新链接之前非常相似。 像这样:

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 将返回昵称和个人资料照片链接!

加:这里的详细指南: http : //www.kekearif.com/how-to-implement-ios-wechat-login/

我已经按照接受的答案做了所有的事情,但它没有奏效,直到我改变了我的Info.plist

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>weixin</string>
    </array>

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>weixin</string>
        <string>weixinULAPI</string>
    </array>

从官方指南这里得到它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM