引入项目的文件
info.plist 添加内容
WXApi.registerApp(Config.wx.APP_ID,enableMTA: true)//注册微信api(在AppDelegate里面注册,AppDelegate 需要实现WxApiDelegate)
WXApi.isWXAppInstalled() //检测微信是否安装
WXApi.isWXAppSupport() //检测当前安装微信app,是否支持当前api
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 onReq(_ req: BaseReq!) { print("======onReq=(req.type)"); } func onResp(_ resp: BaseResp!) { print("======onResp:type=(resp.type);errCode=(resp.errCode)") if(resp.isKind(of: SendAuthResp.self)){
//微信登录回调 if(resp.errCode == 0 && resp.type == 0){ let response:SendAuthResp = resp as! SendAuthResp print("code=(response.code)") print("state=(response.state)") //这里获取到code可以进行获取用户信息处理了 }else{ toastShow(title: "授权失败") } } if(resp.isKind(of: SendMessageToWXResp.self)){ if(resp.errCode == 0 && resp.type == 0){ toastShow(title: "分享成功") }else{ toastShow(title: "分享失败") } } }
微信登录
let req = SendAuthReq.init(); req.scope = "snsapi_userinfo" req.state = "(arc4random()%100)" WXApi.send(req)