Unity IOS微信SDK接入

原创 2015年07月08日 09:29:22

参考:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=&lang=zh_CN

1、将“WeiXin”文件夹,放到Xcode根目录;

2、在Info配置文件中添加URL Types; Identifier:weixin;URL Schemes:xxxxxxxxxxxxxxxxxx
3、添加必要的引用包:SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib
4、修改UnityAppController.h头文件:添加引用:#import "WeiXin/WXApi.h”;添加Delegate:@interface UnityAppController : NSObject<UIApplicationDelegate,WXApiDelegate>
5、在UnityAppController.mm文件中添加如下代码片段:
#pragma mark - WXApiDelegate

#define WeiXinID @"xxxxxxxxxxxxxxxxxx"

#define WeiXinSecret @"5ace7e016a14e913478cdc4219ace9e7"

#define ksendAuthRequestNotification @"ksendAuthRequestNotification"

#define GameObjectName "AndriodClass"

#define MethodName "Weixincallback_LoginSuccess"

#define ShareMethod "Weixincallback_shareSuccess"

extern "C"
{
    bool isWXAppInstalled()
    {
        return [WXApi isWXAppInstalled];
    }
    bool isWXAppSupportApi()
    {
        return [WXApi isWXAppSupportApi];
    }
    // 给Unity3d调用的方法
    void weixinLoginByIos()
    {
        // 登录
        [[NSNotificationCenter defaultCenter] postNotificationName:ksendAuthRequestNotification object:nil];
    }
    void ShareByIos(const char* title,const char*desc,const char*url)
    {
        NSString *titleStr=[NSString stringWithUTF8String:title];
        NSString *descStr=[NSString stringWithUTF8String:desc];//0416aa28b5d2ed1f3199083b3806c6bl
        NSString *urlStr=[NSString stringWithUTF8String:url];
        NSLog(@"ShareByIos titleStr:%@",titleStr);
                NSLog(@"ShareByIos descStr:%@",descStr);
                NSLog(@"ShareByIos urlStr:%@",urlStr);
//        UIImage *img=[UIImage imageNamed:@"AppIcon72x72"];
//                        NSLog(@"ShareByIos img:%@",img);
        // 分享
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = titleStr;
        message.description = descStr;
        [message setThumbImage:[UIImage imageNamed:@"AppIcon72x72"]];
        
        WXWebpageObject *ext = [WXWebpageObject object];
        ext.webpageUrl = urlStr;//@"http://tech.qq.com/zt2012/tmtdecode/252.htm";
        
        message.mediaObject = ext;
        message.mediaTagName = @"WECHAT_TAG_SHARE";
        
        SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
        req.bText = NO;
        req.message = message;
        req.scene = WXSceneTimeline;
        [WXApi sendReq:req];
    }
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:self];
}

- (void)onReq:(BaseReq *)req // 微信向第三方程序发起请求,要求第三方程序响应
{
    
}

- (void)onResp:(BaseResp *)resp // 第三方程序向微信发送了sendReq的请求,那么onResp会被回调
{
    if([resp isKindOfClass:[SendAuthResp class]]) // 登录授权
    {
        SendAuthResp *temp = (SendAuthResp*)resp;
        if(temp.code!=nil)UnitySendMessage(GameObjectName, MethodName, [temp.code cStringUsingEncoding:NSUTF8StringEncoding]);

//        [self getAccessToken:temp.code];
    }
    else if([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        // 分享
        if(resp.errCode==0)
        {
            NSString *code = [NSString stringWithFormat:@"%d",resp.errCode]; // 0是成功 -2是取消
            NSLog(@"SendMessageToWXResp:%@",code);
            UnitySendMessage(GameObjectName, ShareMethod, [code cStringUsingEncoding:NSUTF8StringEncoding]);
        }
    }
}

#pragma mark - Private

- (void)sendAuthRequest

{

    SendAuthReq* req = [[[SendAuthReq alloc] init] autorelease];

    req.scope = @"snsapi_userinfo";

    req.state = @"only123";

    

    [WXApi sendAuthReq:req viewController:_rootController delegate:self];

}



- (void)getAccessToken:(NSString *)code

{

    NSString *path = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WeiXinID,WeiXinSecret,code];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:

     ^(NSURLResponse *response,NSData *data,NSError *connectionError)

    {

         if (connectionError != NULL)

         {

         }
         else

         {

             if (data != NULL)

             {

                 NSError *jsonParseError;

                 NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonParseError];

                                 NSLog(@"#####responseData = %@",responseData);

                 if (jsonParseError != NULL)

                 {

                     //                    NSLog(@"#####responseData = %@",jsonParseError);

                 }

                 NSString *accessToken = [responseData valueForKey:@"access_token"];

                 NSString *openid = [responseData valueForKey:@"openid"];

                 [self getUserInfo:accessToken withOpenID:openid];

             }

         }

     }];

}

- (void)getUserInfo:(NSString *)accessToken withOpenID: (NSString *)openid

{

    NSString *path = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",accessToken,openid];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:

     ^(NSURLResponse *response,NSData *data,NSError *connectionError) {

         if (connectionError != NULL) {

             

         } else {

             if (data != NULL) {

                 NSError *jsonError;

                 NSString *responseData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];

                 NSLog(@"#####responseData = %@",responseData);

                 NSString *jsonData = [NSString stringWithFormat:@"%@",responseData];

                 UnitySendMessage(GameObjectName, MethodName, [jsonData cStringUsingEncoding:NSUTF8StringEncoding]);

                 if (jsonError != NULL) {

                     //                     NSLog(@"#####responseData = %@",jsonError);

                 }

             }

         }

     }];

}
#pragma mark -

6、重写handleOpenURL和openURL方法:

将openURL方法中return YES替换为return [WXApi handleOpenURL:url delegate:self];

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:self];
}
7、didFinishLaunchingWithOptions方法中添加:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendAuthRequest) name:ksendAuthRequestNotification object:nil]; // 微信

//向微信注册
[WXApi registerApp:WeiXinID];

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/h5q8n2e7/article/details/46799157

unity实现微信分享

在unity中实现微信分享功能需要在mob官网上下载一个sharesdk把下载好的unity插件包导入到unity中unity中的脚本using System.Collections;using Sy...
  • qq_36559726
  • qq_36559726
  • 2018-03-21 23:58:20
  • 38

unity iOS 集成ShareSDK步骤

1.ShareSDK各社交平台申请APPkey 的网址及申请流程汇总:http://bbs.mob.com/forum.php?mod=viewthread&tid=275&page=1&extra=...
  • qq372072753
  • qq372072753
  • 2016-04-14 11:15:50
  • 1827

用ShareSDK for Unity分享屏幕截图(android和ios平台)

分享截屏已经是很多游戏应用必备的功能了,找到了一个国内的插件,虽然用起来还行,但是,还是想吐槽下,跟老外的插件比,真的有差距啊有差距啊有差距啊,啊啊啊。 ShareSDK的官方网站:http://ww...
  • wuyt2008
  • wuyt2008
  • 2016-07-22 18:16:10
  • 8761

Unity3d ios 微信链接分享好友,朋友圈的一些坑

我主要是微信链接分享 1.首先是图标大小限制, android: http://blog.csdn.net/dl10210950/article/details/53125589 ios参考:...
  • atgczcl
  • atgczcl
  • 2018-02-13 23:38:57
  • 170

unity接入微信支付-Ios篇

近段时间都在搞unity接sdk,上篇记了unity接入微信支付android端,URL:unity接入微信支付-Android篇   这几天又在搞ios端,各种坑啊。。。。。C。。。OC。。。作...
  • qq_24529085
  • qq_24529085
  • 2017-06-09 14:59:29
  • 2602

关于unity接iosSDK(微信登录) 的一些坑

1. 编译报错:   duplicate symbol XXX.a 官方文档中提到: [4] 在你的工程文件中选择Build Setting,在"Other Linker Flags"中加入"-Obj...
  • AXuan_K
  • AXuan_K
  • 2017-03-19 00:20:20
  • 3681

unity3d利用sharesdk分享内容至微信、新浪微博、qq空间等国内主流社交平台(ios篇)

unity3d 利用集成化分享插件sharesdk,分享到ios平台 微信、新浪微博、qq空间等平台的步骤...
  • qq15233635728
  • qq15233635728
  • 2015-01-28 10:16:39
  • 9445

Unity接入微信SDK——iOS(接入微信SDK)

Unity接入微信SDK——iOS(接入微信SDK)
  • u014605105
  • u014605105
  • 2017-11-22 10:14:55
  • 1912

unity iso 微信SDK使用

Unity ios微信SDK使用工具准备: 1.官方下载微信SDK 2.去申请一个app需要微信的appID 3.xcode IDE 4.等等开发者必备工具,具...
  • imsjw
  • imsjw
  • 2017-12-09 14:38:36
  • 278

【Unity】中实现微信、QQ、微博帐号登录(Android环境)(官方SDK)

对于一些没有过安卓开发经验的开发者来说,为了实现安卓第三方登陆肯定是费尽了脑汁,不知道从何着手解决,为了让开发者少走弯路,下面就给大家介绍下Unity中使用官方SDK实现微信、QQ、微博帐号登录的方法...
  • swj524152416
  • swj524152416
  • 2017-02-16 17:38:28
  • 3498
收藏助手
不良信息举报
您举报文章:Unity IOS微信SDK接入
举报原因:
原因补充:

(最多只允许输入30个字)