swift 项目集成微信登录

发布时间:2022-06-27 23:18:44

为了让项目管理系统更加简便注册,于是开发微信登录,在此之前需要别外两个准备工作

1.制作桥接文件

2.制作通用链接

3.安装sdk

pod 'WechatOpenSDK'

4.在桥接文件中引入sdk的内容

#ifndef bridging_header_h
#define bridging_header_h

#import <CommonCrypto/CommonDigest.h>
#import "WXApi.h"
#import <WXApiObject.h>
#endif /* bridging_header_h */

5.AppDelegate 中的代码

AppDelegate:,WXApiDelegate,WXApiLogDelegate

在application方法中注册
  WXApi.startLog(by: .detail, logDelegate: self)
  WXApi.registerApp(ConstHelper.WechatAppId, universalLink:ConstHelper.universalLink)

然后在类中加入以下五个方法
 func onLog(_ log: String, logLevel level: WXLogLevel) {
        print("wechat-log: ",log)
    }
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: 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 application(_ application: UIApplication, continue userActivity:
        NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
    }

    
    func onReq(_ req: BaseReq) {
           //onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。
           print(req.type)
       }
       func onResp(_ resp: BaseResp) {
           //如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面。
           print("--------------------",resp)
           if resp.isKind(of: SendAuthResp.self) {
               let response = resp as! SendAuthResp
               print("ErrCode :",response.errCode)
               print("EodeStr :",response.errStr)
               print("Code : ",response.code!)
//               print("State : ",response.state)
//               print("Lang : ",response.lang)
//               print("Country : ",response.country)
               NotificationCenter.default.post(name: NSNotification.Name(rawValue: "WXLoginSuccessNotification"), object: response.code)
               
           }
           
       }

6.在loginController中处理登录

  center.addObserver(self, selector: #selector(doWechatLogin(info:)), name:NSNotification.Name(rawValue: "WXLoginSuccessNotification"), object: nil)//单个值得传递

Card image cap
APP STORE
Card image cap
应用宝
Card image cap
小米
Card image cap
华为