- 首先给大家展示下报错
Error Domain=UMSocialPlatformErrorDomain Code=2002 "(null)" UserInfo={message=appKey is nil}
- 在大家看看我是如何解决的:
友盟官方文档上这样一个方法“ confitUShareSettings”
func confitUShareSettings() {
UMSocialGlobal.shareInstance().isUsingHttpsWhenShareContent = false
//设置微信的appkey和appsecret
UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: wxAppKey, appSecret: wxAppSecret, redirectURL: XQURLString)
//设置QQ的APPkey和appsecret
UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: QQAppKey, appSecret: nil, redirectURL: XQURLString)
//设置支付宝APPkey和appsecret
UMSocialManager.default().setPlaform(UMSocialPlatformType.alipaySession, appKey: alipayAppKey, appSecret: "", redirectURL: XQURLString)
}
可惜我忘了调用。。。屏幕快照 2018-08-25 下午12.25.04.png
- 调用下,就ok了。如果你也出现了类型的问题,可能是以下方法没有调用。
UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: wxAppKey, appSecret: wxAppSecret, redirectURL: XQURLString)
在此对友盟的集成做个总结:
1、 设置URL Types
屏幕快照 2018-08-25 下午12.30.58.png
2、设置Info.plist文件

屏幕快照 2018-08-25 下午12.35.13.png
3、使用友盟的AppKey初始化服务
UMConfigure.initWithAppkey(UMKey, channel: "App Store")
4、使用在各平台申请的Appkey初始化对应平台的服务
func confitUShareSettings() {
UMSocialGlobal.shareInstance().isUsingHttpsWhenShareContent = false
//设置微信的appkey和appsecret
UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: wxAppKey, appSecret: wxAppSecret, redirectURL: XQURLString)
//设置QQ的APPkey和appsecret
UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: QQAppKey, appSecret: nil, redirectURL: XQURLString)
//设置支付宝APPkey和appsecret
UMSocialManager.default().setPlaform(UMSocialPlatformType.alipaySession, appKey: alipayAppKey, appSecret: "", redirectURL: XQURLString)
}
5、在需要的地方调用获取第三方平台信息的方法:
UMSocialManager.default().getUserInfo(with: platformType, currentViewController: nil) { (result, error) in
guard error == nil else{
let nsError = error! as NSError
print("错误是什么:\(nsError)")
let dic = nsError.userInfo
// print("错误:\(error?.localizedDescription)")
let ac = UIAlertController.init(title: "第三方登录失败", message: dic["message"] as? String, preferredStyle: UIAlertControllerStyle.alert)
ac.addAction(UIAlertAction.init(title: "确定", style: UIAlertActionStyle.default, handler: nil))
self.present(ac, animated: true, completion: nil)
return
}
let res = result as! UMSocialUserInfoResponse
//第三方登录信息
print("uid"+res.uid)
print("openid:"+res.openid)
print("accessToken:"+res.accessToken)
print("refreshToken:"+res.refreshToken)
print("expiration:\(res.expiration)")
//用户数据
print("name:"+res.name)
print("iconurl:"+res.iconurl)
print("gender:"+res.unionGender)
//第三方平台SDK原始数据
print("originalResponse:\(res.originalResponse)")
}
}
注:上述方法我封装在下述方法中:
func getUserInfoForPlatform(platformType:UMSocialPlatformType) {
UMSocialManager.default().getUserInfo(with: platformType, currentViewController: nil) { (result, error) in
guard error == nil else{
let nsError = error! as NSError
print("错误是什么:\(nsError)")
let dic = nsError.userInfo
// print("错误:\(error?.localizedDescription)")
let ac = UIAlertController.init(title: "第三方登录失败", message: dic["message"] as? String, preferredStyle: UIAlertControllerStyle.alert)
ac.addAction(UIAlertAction.init(title: "确定", style: UIAlertActionStyle.default, handler: nil))
self.present(ac, animated: true, completion: nil)
return
}
let res = result as! UMSocialUserInfoResponse
//第三方登录信息
print("uid"+res.uid)
print("openid:"+res.openid)
print("accessToken:"+res.accessToken)
print("refreshToken:"+res.refreshToken)
print("expiration:\(res.expiration)")
//用户数据
print("name:"+res.name)
print("iconurl:"+res.iconurl)
print("gender:"+res.unionGender)
//第三方平台SDK原始数据
print("originalResponse:\(res.originalResponse)")
}
}
