环境描述:系统IOS 12.4 ,微信版本 7.0.8, SDK 1.8.6,
错误日志:
Error:set token fail, errCode:4, errLog:wx token[] or contextId[(null)] is nil
状态描述:可以看到有跳转微信的过程,但是不能正确获取微信授权界面。但是又能拿到相应的返回值,很奇怪,有人遇到过嘛?
相关代码:
//三个重写的API
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
returnWXApi.handleOpen(url, delegate: self)
// func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
// return WXApi.handleOpen(url, delegate: self)
// }
func application(_application: UIApplication, handleOpen url: URL) -> Bool{
returnWXApi.handleOpen(url, delegate: self)
func application(_ application: UIApplication, continueuserActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
logger.info(“调用useractivity”
returnWXApi.handleOpenUniversalLink(userActivity, delegate: self)
//回调
func onResp(_ resp: BaseResp) {
logger.info(String(format: “返回的resp是:%p”, resp))
//微信登录
ifresp is SendAuthResp {
logger.info(“微信登录接口”
// switch(resp.errCode){
// case 0:
// let response = resp as! SendAuthResp
// NotificationCenter.default.post(name: NSNotification.Name(rawValue: “WXLoginSuccessNotification”), object: response.code)
// break
// case -4: //用户拒绝
// NotificationCenter.default.post(name: NSNotification.Name(rawValue: “WXLoginDenyNotification”), object: nil)
// break
// case -2: //用户取消
// NotificationCenter.default.post(name: NSNotification.Name(rawValue: “WXLoginCancelNotification”), object: nil)
// break
// default:
// break
// }
ifresp.errCode == 0 && resp.type == 0 {//授权成功
let response = resp as! SendAuthResp
NotificationCenter.default.post(name: NSNotification.Name(rawValue: “WXLoginSuccessNotification”), object: response.code)
// end of internal if
return
// end of external if
// end of onResp
// send
@IBAction func wechatLogin(_ sender: LGButton) {
sender.isLoading = true
ifWXApi.isWXAppInstalled() {
let app = UIApplication.shared
ifapp.canOpenURL(URL.init(string: WEIXIN_SCHEME)!) {
let req = SendAuthReq.init()
req.scope = “snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact”
req.state = “\(arc4random()%100)”
logger.info(“准备开启微信登录!”
let deadlineTime = DispatchTime.now() + .seconds(1)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
//停止转圈圈
WXApi.send(req, completion: { (success) in
sender.isLoading = false
logger.info(“成功获取微信app的授权,开始向微信服务器发起请求!”
})}
} else
if#available(iOS 10.0, *) {
UIApplication.shared.open(URL.init(string: “http://weixin.qq.com/r/qUQVDfDEVK0rrbRu9xG7”)!, options: [:], completionHandler: nil)
} else
// Fallback on earlier versions
UIApplication.shared.openURL(URL.init(string: “http://weixin.qq.com/r/qUQVDfDEVK0rrbRu9xG7”)!)
else
self.sendmsgToWxServer()
sender.isLoading = false
// 登录成功之后
@objc func WXLoginSuccess(notification:Notification) {
logger.info(“WXLoginSuccess开始执行”
let code = notification.object as! String
let requestUrl = “https://api.weixin.qq.com/sns/oauth2/access_token?appid=\(WX_APPID)&secret=\(WX_APPSecret)&code=\(code)&grant_type=authorization_code”
DispatchQueue.global().async {
let requestURL: URL = URL.init(string: requestUrl)!
let data = try? Data.init(contentsOf: requestURL, options: Data.ReadingOptions())
//TODO: 从微信获取到的用户信息,然后再回传到app的后台服务器
DispatchQueue.main.async {
let jsonResult = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String,Any>
logger.info(“微信app传回来的数据: \(jsonResult)”
let openid: String = jsonResult[“openid”] as! String
let access_token: String = jsonResult[“access_token”] as! String
let unionid: String = jsonResult[“unionid”] as! String
self.openid = openid
//向app服务器保存用户的openid和access_token
let parameters: Parameters = [“openId”: openid, “accessToken”: access_token]
AF.request(SAVE_USER_INFO_URL_STR, parameters: parameters).validate()
let homeVcUrlStr = String(format: “%@%@”, arguments: [HOMEPAGE_LOGINED_URL_STR, unionid])
logger.info(String(format:“homeStr: %@, openid: %@”, arguments:[homeVcUrlStr, openid]))
let mainNavigationController = self.presentingViewController as! LFMainNavigationController
let homePageController = mainNavigationController.viewControllers[0] as! LFHomeViewController
homePageController.openid = self.openid
homePageController.homePageUrlStr = homeVcUrlStr
self.dismiss(animated: true, completion: {
}) // end of dismiss
//暂时不需要从微信服务器获取用户的信息
// self.getUserInfo(openid: openid, access_token: access_token)
有人能帮忙看看嘛? 或者有人遇到相似的问题嘛 ? 求交流 !!!
网页回复不及时可加微信:dzjMichael ( 加时注明来源 不甚感激!!!
————————————————————————————
2020 - 01 -07更新
问题已经解决,发现是UL配置的问题,建议各位老铁多多检查自己的配置吧,从scheme到ul,然后再试自己的代理函数,基本上照着文档来一般不会有问题。