微信三方登录相关(Swift)

微信登录条件

1.微信开放平台注册并认证成功(每年300RMB)

2.相关应用的微信的APPID和secret

3. 遵循微信代理WXApiDelegate

使用处编写相关登录代码

 WXApi.registerApp("wxe7d57dc35de9c3c8")

         let req = SendAuthReq.init()

         req.scope = "snsapi_userinfo"

         req.state = "wulianwang"

         WXApi.send(req)

 

在回调方法中处理相关业务

func onResp(_ resp: BaseResp!) {

        print(resp.errCode)

        //errCode == 0时,请求access_token

        if resp.errCode == 0 {

            let pathStr = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=\(wxAPPID)&secret=\(secret)&code=\(resp.errCode)&grant_type=authorization_code"

            let url = URL.init(string: pathStr)

            let str = try! String.init(contentsOf: url!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))

            

            let data = str.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))

            

            DispatchQueue.main.async {

                if (data != nil){

                    let dic = try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers)

                    /** 正确时返回的JSON格式入一下!!!

                     {

                     "access_token":"ACCESS_TOKEN",

                     "expires_in":7200,

                     "refresh_token":"REFRESH_TOKEN",

                     "openid":"OPENID",

                     "scope":"SCOPE",

                     "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"

                     }

                     */

                    print(dic)

                    

                    if (dic as! NSDictionary)["access_token"] == nil{

                        let alert = UIAlertController.init(title: "提示", message: str , preferredStyle: .alert)

                        let ok = UIAlertAction.init(title: "确定", style: .cancel, handler: nil)

                        

                        alert.addAction(ok)

                        

                        self.present(alert, animated: true, completion: nil)

                    }

                }

                

            }

        }

    }

  //注意:secret可能会报错,注意重置secret!!!

appDelegate中增加相关方法

 func application(_ application: UIApplication, handleOpen url: URL) -> Bool {

        return WXApi.handleOpen(url as URL!, delegate: self)

    }

转载于:https://my.oschina.net/wayzhu/blog/857765

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值