unity3D接入微信登录SDKIOS版

unity想跑IOS。必须拿真机测试。
错误总结
1. Permission denied的解决方法:chmod +x /Users/majiang/Desktop/U3DIOS/MapFileParser.sh
2. 必须将libWeChatSDK.a文件放到项目的Libraries文件夹下,然后在导入
3. 在unity的playerSetting中把auto graphics api的钩去除把metal删除
4. 如果爆不知名的错请把build settings 中的oc automatic这项选成YES
5. 把SendAuthReq中的autorelease去掉

extern "C"{
    void WeiXinLoginByIos(){
        NSLog(@"111");
        //构造SendAuthReq结构体
        SendAuthReq* req =[[SendAuthReq alloc ] init ];
        req.scope = @"snsapi_userinfo" ;
        req.state = @"123" ;
        //第三方向微信终端发送一个SendAuthReq消息结构
        [WXApi sendReq:req];
    }
}
//重写微信方法(必要步骤)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [WXApi handleOpenURL:url delegate:self];
}

//重写微信方法(必要步骤)
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    return [WXApi handleOpenURL:url delegate:self];
}

-(void) onReq:(BaseReq *)req{}

#define WeiXinID @"你的APPID"
#define WeiXinSecret @"你的APPSecret"
#define GameObjectName "UI_Root"
#define MethodName "json_UserInfo"

-(void) onResp:(BaseResp *)resp{
    if (resp.errCode==0) {
        NSLog(@"微信登录返回成功!");

    }else{
        NSLog(@"微信登录失败! %d",resp.errCode);
    }
    /*
     enum  WXErrCode {
     WXSuccess           = 0,    成功
     WXErrCodeCommon     = -1,  普通错误类型
     WXErrCodeUserCancel = -2,    用户点击取消并返回
     WXErrCodeSentFail   = -3,   发送失败
     WXErrCodeAuthDeny   = -4,    授权失败
     WXErrCodeUnsupport  = -5,   微信不支持
     };
     */
    // 返回成功,获取Code
    SendAuthResp *sendResp = resp;
    NSString *code = sendResp.code;
    NSLog(@"code=%@",sendResp.code);
    // 根据Code获取AccessToken(有限期2个小时)
    // https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code 

    //
    // 发起GET请求
    // 2.1.设置请求路径
    NSString *urlStr = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code 

",WeiXinID,WeiXinSecret,code];

    // 转码
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // URL里面不能包含中文
    NSURL *url = [NSURL URLWithString:urlStr];
    // 2.2.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 默认就是GET请求
    request.timeoutInterval = 5; // 设置请求超时
    // 2.3.发送请求
    [self sendAsync:request];

    NSLog(@"---------已经发出请求");
}
//重写微信方法(必要步骤)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [WXApi handleOpenURL:url delegate:self];
}

//重写微信方法(必要步骤)
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    return [WXApi handleOpenURL:url delegate:self];
}

-(void) onReq:(BaseReq *)req{}

#define WeiXinID @"你的APPID"
#define WeiXinSecret @"你的APPSecret"
#define GameObjectName "UI_Root"
#define MethodName "json_UserInfo"

-(void) onResp:(BaseResp *)resp{
    if (resp.errCode==0) {
        NSLog(@"微信登录返回成功!");

    }else{
        NSLog(@"微信登录失败! %d",resp.errCode);
    }
    /*
     enum  WXErrCode {
     WXSuccess           = 0,    成功
     WXErrCodeCommon     = -1,  普通错误类型
     WXErrCodeUserCancel = -2,    用户点击取消并返回
     WXErrCodeSentFail   = -3,   发送失败
     WXErrCodeAuthDeny   = -4,    授权失败
     WXErrCodeUnsupport  = -5,   微信不支持
     };
     */
    // 返回成功,获取Code
    SendAuthResp *sendResp = resp;
    NSString *code = sendResp.code;
    NSLog(@"code=%@",sendResp.code);
    // 根据Code获取AccessToken(有限期2个小时)
    // https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code 

    //
    // 发起GET请求
    // 2.1.设置请求路径
    NSString *urlStr = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code 

",WeiXinID,WeiXinSecret,code];

    // 转码
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // URL里面不能包含中文
    NSURL *url = [NSURL URLWithString:urlStr];
    // 2.2.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 默认就是GET请求
    request.timeoutInterval = 5; // 设置请求超时
    // 2.3.发送请求
    [self sendAsync:request];

    NSLog(@"---------已经发出请求");
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值