由于项目是与Unity3D合作,因此在Unity导出的Xcode项目中使用微信登录踩过坑,特此记录
首先保证Unity导出的Xcode可以运行,手动导入WechatOpenSDK或者省事直接pod管理。本人采用的是pod统一管理三方库,注意pod的时候项Taget为target 'UnityFramework' do,其他的Taget也能pod成功,但是项目编译的时候会报错提示找不到pod的三方库。
微信登录主要有5个步骤,前4步小白都会,重要的是第5步,与正常的OC或者Swift项目配置有所不同:
1、配置微信key和Unlink;
2、配置白名单,不熟悉的可百度
3、配置Associated Domains
image.png
4、在Info中添加type
image.png
5、重点来了
在UnityAppController.mm中引入头文件#import <WechatOpenSDK/WXApi.h>,遵守协议WXApiDelegate,由于在UnityAppController.h中引入头文件会报错,可直接在UnityAppController.mm中声明并遵守协议
@interface UnityAppController ()<WXApiDelegate>
@end
之后修改函数- (BOOL)application:(UIApplication)app openURL:(NSURL)url options:(NSDictionary<NSString, id>)options;
修改之前
- (BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary<NSString*, id>*)options
{
id sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey], annotation = options[UIApplicationOpenURLOptionsAnnotationKey];
NSMutableDictionary<NSString*, id>* notifData = [NSMutableDictionary dictionaryWithCapacity: 3];
if (url)
{
notifData[@"url"] = url;
UnitySetAbsoluteURL(url.absoluteString.UTF8String);
}
if (sourceApplication) notifData[@"sourceApplication"] = sourceApplication;
if (annotation) notifData[@"annotation"] = annotation;
AppController_SendNotificationWithArg(kUnityOnOpenURL, notifData);
return YES;
}
修改之后
- (BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary<NSString*, id>*)options
{
id sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey], annotation = options[UIApplicationOpenURLOptionsAnnotationKey];
NSMutableDictionary<NSString*, id>* notifData = [NSMutableDictionary dictionaryWithCapacity: 3];
if (url)
{
notifData[@"url"] = url;
UnitySetAbsoluteURL(url.absoluteString.UTF8String);
}
if (sourceApplication) notifData[@"sourceApplication"] = sourceApplication;
if (annotation) notifData[@"annotation"] = annotation;
AppController_SendNotificationWithArg(kUnityOnOpenURL, notifData);
return [WXApi handleOpenURL:url delegate:[BBShowAdTools sharedManger]];
}
此时,配置前4步没错的情况下即可拉起微信并进行登录,然后返回,没有第“5”步,会登录失败。
网友评论