1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // CustomAppController.mm #import "UnityAppController.h" @interface CustomAppController : UnityAppController @end IMPL_APP_CONTROLLER_SUBCLASS (CustomAppController) @implementation CustomAppController - (BOOL)applicationUIApplication*)application didFinishLaunchingWithOptionsNSDictionary*)launchOptions {<!-- --> [super application:application didFinishLaunchingWithOptions:launchOptions]; // TODO return YES; } @end |
1 2 3 4 5 6 7 8 9 10 11 | // WXApiManager.h #import <foundation foundation.h=""> #import "WXApi.h" @interface WXApiManager : NSObject<wxapidelegate> + (instancetype)sharedManager; @end </wxapidelegate></foundation> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #import "WXApiManager.h" @implementation WXApiManager // 单例 +(instancetype)sharedManager {<!-- --> static dispatch_once_t onceToken; static WXApiManager *instance; dispatch_once(&onceToken, ^{<!-- --> instance = [[WXApiManager alloc] init]; }); return instance; } - (void)onRespBaseResp *)resp {<!-- --> // TODO 微信回调,调用微信SDK的sendReq,会回调此方法,登录、分享等都是回调到这里 } - (void)onReqBaseReq *)req {<!-- --> // TODO 微信回调,从微信端主动发送过来的请求 } @end |
1 | #import "WXApiManager.h" |
1 2 3 4 5 6 7 8 9 10 11 12 13 | - (BOOL)applicationUIApplication *)application handleOpenURLNSURL *)url {<!-- --> return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]]; } - (BOOL)applicationUIApplication *)application openURLNSURL *)url sourceApplicationNSString *)sourceApplication annotationid)annotation {<!-- --> return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]]; } - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<uiuseractivityrestoring>> * __nullable restorableObjects))restorationHandler {<!-- --> return [WXApi handleOpenUniversalLink:userActivity delegate:[WXApiManager sharedManager]]; } </id<uiuseractivityrestoring> |
1 | [WXApi registerApp"你的APPID" universalLink"你的UniversalLink"]; |
1 2 3 4 5 6 7 8 | - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {<!-- --> [super application:application didFinishLaunchingWithOptions:launchOptions]; [WXApi registerApp"你的APPID" universalLink"你的UniversalLink"]; return YES; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // WeChatUnity.mm #import <foundation foundation.h=""> #import "WXApiManager.h" #define UNITY_CS_API extern "C" static NSString *mWXAppid = nil; // 将c字符串const char* 转为 oc字符串NSString static inline NSString * str_c2ns(const char*s) {<!-- --> if (s) return [NSString stringWithUTF8String: s]; else return [NSString stringWithUTF8String: ""]; } // 初始化 UNITY_CS_API void UnityWeChatInit(const char* appId, const char* universalLink) {<!-- --> mWXAppid = str_c2ns(appId); [WXApi registerApp:mWXAppid universalLink:str_c2ns(universalLink)]; } </foundation> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // WeChatSDK.cs using UnityEngine; public class WeChatSDK {<!-- --> public static void Init() {<!-- --> #if UNITY_IOS UnityWeChatInit("你的APPID", "你的UniversalLink"); #elif UNITY_ANDROID // TODO Android的调用 #endif } #if UNITY_IOS [DllImport("__Internal")] static extern void UnityWeChatInit(string appId, string universalLink); #endif } |
1 2 3 4 5 6 7 8 9 10 11 12 | // WeChatUnity.mm UNITY_CS_API void UnityWeChatLogin(const char* state) {<!-- --> NSLog(@"UnityWeChatLogin"); SendAuthReq* req = [[SendAuthReq alloc] init]; req.scope = @"snsapi_userinfo";; req.state = str_c2ns(state); [WXApi sendReq:req completion:nil]; // 此时会拉起微信,授权后会回调WXApiManager的onResp方法 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // WeChatSDK.cs public static void Login() {<!-- --> #if UNITY_IOS // "app_wechat"后期改为随机数加session来校验 UnityWeChatLogin("app_wechat"); #elif UNITY_ANDROID // TODO Android的调用 #endif } #if UNITY_IOS [DllImport("__Internal")] static extern void UnityWeChatLogin(string state); #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | // WXApiManager.mm - (void)onResp:(BaseResp *)resp {<!-- --> if ([resp isKindOfClass:[SendAuthResp class]]) {<!-- --> NSLog(@"微信授权回调"); if (resp.errCode == 0) {<!-- --> // 通过code去交换token,此步需要用到APPSecret,这个字段非常敏感 // 实际项目此步是放在服务端执行,这里仅作为客户端演示 [self requestWxToken:((SendAuthResp *)resp).code]; } else {<!-- --> // 失败,回调给Unity UnitySendMessage("SDKCallBack", "WeChatLoginCallback", ""); } } } // 通过code调换token,此步实际项目是放在服务端执行 -(void)requestWxToken:(NSString*)code {<!-- --> NSURL *url = [NSURL URLWithString:[NSString stringWithFormat"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", mWXAppid, "你的APPSecret", code]]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {<!-- --> if (error == nil) {<!-- --> NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; [self requestWxUserInfo:[dict valueForKey"access_token"]]; } }]; [dataTask resume]; } // 通过token查询用户信息,此步实际项目是放在服务端执行 -(void)requestWxUserInfo:(NSString*)token{<!-- --> NSURL *url = [NSURL URLWithString:[NSString stringWithFormat"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token, mWXAppid]]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {<!-- --> if (error == nil) {<!-- --> NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; NSString *jsonStr = [self DataTOjsonString:dict]; // 回调给Unity UnitySendMessage("SDKCallBack", "WeChatLoginCallback",[jsonStr UTF8String]); } }]; [dataTask resume]; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // SDKCallBack.cs using UnityEngine; public class SDKCallBack : MonoBehaviour {<!-- --> /// <summary> /// 登录回调 /// </summary> /// <param name="callBackInfo"> public void WechatLoginCallback(string callBackInfo) {<!-- --> // openid 平凡用户的标识,对当前开辟者帐号唯一 // nickname 平凡用户昵称 // sex 平凡用户性别,1为男性,2为女性 // province 平凡用户个人资料填写的省份 // city 平凡用户个人资料填写的都会 // country 国家,如中国为CN // headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空 // privilege 用户特权信息,json数组,如微信沃卡用户为(chinaunicom) // unionid 用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。多app数据互通生存该值 // access_token 用户当前临时token值,自主添加的值 if(string.IsNullOrEmpty(callBackInfo)) {<!-- --> // TODO 登录失败,请重试 } else {<!-- --> // 解析字段,登录进游戏服务器 } } } |
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |