Unity IOS SDK 集成 微信登录

xzhuan原创稿件,转载请注明出处!
用到的地址:注
微信开放平台 获取微信官方文档、以及案例Demo

需求:Unity 项目 IOS端 微信登录
1.下载官网SDK文件,列表如下,存于 Assets/Plugins/iOS目录下
libWeChatSDK.a
WXApi.h
WechatAuthSDK.h
WXApiObject.h

2.IOS目录下新建文件夹WXApiManager
3.WXApiManager目录下新建代码
WXApiManager.h
WXApiManager.mm

 4.1WXApiManager.h代码内容如下
//  WXApiManager.h
#import 
#import "Libraries/Plugins/iOS/WXApi.h"
NS_ASSUME_NONNULL_BEGIN
@interface WXApiManager : UIResponder
+ (instancetype)shareManager;
@end
NS_ASSUME_NONNULL_END

 4.2WXApiManager.mm代码内容如下
//  WXApiManager.m
#import "WXApiManager.h"
@implementation WXApiManager
+(instancetype) shareManager
{
   static dispatch_once_t onceToken;
   static WXApiManager *instance;
   dispatch_once(&onceToken, ^{
       instance = [[WXApiManager alloc] init];
   });
   return instance;
}
// 微信发送请求到第三方应用时,会回调到该方法
-(void) onReq:(BaseReq *)req {}
// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
-(void) onResp:(BaseResp *)resp
{
    NSLog(@"微信响应");
   if([resp isKindOfClass:[SendAuthResp class]])
   {
       SendAuthResp *temp = (SendAuthResp*)resp;
       int errorCode = temp.errCode;
       switch (errorCode) {
           case 0:
               {
                   printf("登录成功-xcode");
                   NSLog(@"code %@",temp.code);
                   //UnitySendMessage(objName, funName, [NSUTF8StringEncoding temp.code]);
                   break;
               }
           case -2:
               printf("用户取消");
               UnitySendMessage("LaunchCanvas", "MessageError", "用户取消");
               break;
           case -4:
               printf("用户拒绝授权");
               UnitySendMessage("LaunchCanvas", "MessageError", "用户拒绝授权");
               break;
           default:
               printf("登录失败");
               UnitySendMessage("LaunchCanvas", "MessageError", "登录失败");
               break;
       }
   }
}
@end

5.IOS目录下 新建Unity和IOS交互代码文件
Native.h
Native.mm

 5.1 Native.h代码如下
 #include "UI/UnityViewControllerBase.h"
#include "UnityAppController+ViewHandling.h"
@interface Native:NSObject
@end
extern "C" {
   void WechatLogin();
}

 5.2 Native.mm代码如下
#import 
#import "Native.h"
#import "Libraries/Plugins/iOS/WXApi.h"
@interface Native ()
@end
@implementation Native :NSObject
//判断是否安装微信
bool _isWechatInstalled()
{
   return [WXApi isWXAppInstalled];
}
//微信登录
void WechatLogin()
{
   NSLog(@"微信登录");

   //向微信注册appid: 填写自己微信开放平台的appid信息
   NSString *weichatId = [NSString stringWithFormat:@"%s", appid];
   [WXApi registerApp:weichatId];

   if([WXApi isWXAppInstalled]==false)
   {
     NSLog(@"请先安装微信客户端");
     //UnitySendMessage("LaunchCanvas", "MessageError", "请先安装微信客户端");
     return;
   }
   
   //登录
   SendAuthReq* req = [[SendAuthReq alloc] init];
   req.scope = @"snsapi_userinfo";
   req.state = [NSString stringWithFormat:@"%s", "wechat_sdk_demo_test"];
   [WXApi sendReq:req];
}
//打开微信(注意:需要先注册appid)
void OpenWXApp()
{
 [WXApi openWXApp];
}
@end

6.unity build Xcode工程
6.1 包名 需与微信开放平台填写的Bundle ID一致
6.2 Player-Settings 取消勾选 Script Engine Code*
6.3 Api Compatibility Level* 选择 .NET 2.0 Subset

7.打开Xcode工程,具体设置如下
 7.1 Build Settings - Enable Bitcode = No
 7.2 Build Settings - Other Linker Flags 添加 -Objc 和 -all_load
 7.3 (白名单设置)Info-Custom iOS Target Properties 添加 LSApplicationQueriesSchemes,设置子项 weixin 和 wechat
 7.4 (设置支持HTTP请求)Info-Custom iOS Target Properties 添加 App Transport Security Settings 设置 Allow Arbitrary Loads = YES
 7.5 (设置URL Types)Info-URL Types 设置Identlfler= weixin,URL Schemes= Appid(微信开放平台申请的Appid),Role= Editor

8.添加依赖库
Build Phases - Link Binary With Libraries 添加库如下
SystemConfiguration.framework
libz.tbd
libsqlite3.0.tbd
libc++.tbd
Security.framework
CoreTelephony.framework
CFNetwork.framework
libWeChatSDK.a(检查不存在,也添加上)

9.编写部分代码
根据项目目录 Classes下找到UnityAppController.mm代码文件 添加
#import "Libraries/Plugins/iOS/WXApi.h"
#import "Libraries/Plugins/iOS/WXApiManager/WXApiManager.h"

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
   return  [WXApi handleOpenURL:url delegate:[WXApiManager shareManager]];
}
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
      //return YES; 此函数将这句注释,改为下面那句
      return  [WXApi handleOpenURL:url delegate:[WXApiManager shareManager]];
}

注意:IOS9.0以上 请继续添加下面这个方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options {
   return [WXApi handleOpenURL:url delegate:[WXApiManager shareManager]];
}

10.最后是Unity调用的代码如下
   /// 
   /// 微信登录
   /// 
   [DllImport("__Internal")]
   private static extern void WechatLogin();
   private void OnGUI()
   {
      if (GUI.Button("微信登录"))
       {
           WechatLogin();
       }
   }

   /// 
   /// code信息  
   /// 
   private void funName(string code)
   {
       Debug.Log("code=  " + code);
   }

11.code信息拿到后,就是2次HTTP的事了、可以在IOS处理、也可以在C#处理、也可以服务器处理

12.注意事项:
*微信自身有一个bug,如果未登录微信返回后没有回调接口,无法判断到回调

不明白的私信哦,哇咔咔~~~

你可能感兴趣的:(Unity IOS SDK 集成 微信登录)