微信分享c++接口&OC实现

工作需要为Unity游戏提供c++接口,参考了一些大神的代码之后改了改,话不多说,上代码。

//
//  WXShareUnityLibs.h
//  WXShareUnityLibs
//
//  Created by zhai chunlin on 15/10/26.
//  Copyright (c) 2015年 ZCL. All rights reserved.
//

#import 

#define WXSHARE         7

#define WXSHARE_FAILURE 700
#define WXSHARE_SUCCESS 701

#if defined(__cplusplus)
extern "C"{
#endif
    //分享结果回调函数类型声明
    typedef void (*WXShareCallback) (int callback, int resultStatus);
    
    //WXApi的成员函数,向微信终端程序注册第三方应用。
    extern BOOL iosWXRegisterApp(const char *appId);
    
    //处理微信通过URL启动App时传递的数据
    extern BOOL iosWXHandleOpenURL(const char *url);
    
    //检查微信是否已被用户安装
    extern BOOL iosWXIsWXAppInstalled();
    
    //判断当前微信的版本是否支持OpenApi
    extern BOOL iosWXIsWXAppSupportApi();
    
    //分享    shareMethod:    0:好友 1:朋友圈
    extern void iosWXShareToWinXinWithTitle(const char *title, const char *description, const char *imagePath, BOOL shareMethod);
    
    //注册回调函数
    extern void iosSetWXShareListener(WXShareCallback callback);
#if defined(__cplusplus)
}
#endif

//
//  WXShareUnityLibs.m
//  WXShareUnityLibs
//
//  Created by zhai chunlin on 15/10/26.
//  Copyright (c) 2015年 ZCL. All rights reserved.
//

#import "WXShareUnityLibs.h"
#import "WXShare.h"

@interface WXShareUnityLibs : NSObject

@end

#if defined(__cplusplus)
"C"{
#endif
    
    WXShareCallback wxShareCallback;
    
    WXShareUnityLibs *shareLibs;
    
    //字符串转化的工具函数
    
    NSString* _CreateNSString (const char* string)
    {
        if (string)
            return [NSString stringWithUTF8String: string];
        else
            return [NSString stringWithUTF8String: ""];
    }
    
    BOOL iosWXRegisterApp(const char *appId)
    {
        NSLog(@"WXAppId = %@",_CreateNSString(appId));
        
        if (!shareLibs) {
         
            shareLibs = [[WXShareUnityLibs alloc] init];
            
        }
        
        return [WXShare registerApp:_CreateNSString(appId)];
    }
    
    BOOL iosWXHandleOpenURL(const char *url)
    {
        NSLog(@"url = %@",_CreateNSString(url));
        
        NSString *aUrl = _CreateNSString(url);
        
        NSURL *nsUrl = [NSURL URLWithString:aUrl];
        
        NSLog(@"nsUrl = %@", nsUrl);
        
        return [WXShare handleOpenURL:nsUrl];
    }
    
    BOOL iosWXIsWXAppInstalled()
    {
        return [WXShare isWXAppInstalled];
    }
    
    BOOL iosWXIsWXAppSupportApi()
    {
        return [WXShare isWXAppSupportApi];
    }
    
    void iosWXShareToWinXinWithTitle(const char *title, const char *description, const char *imagePath, BOOL shareMethod)
    {
        NSLog(@"title = %@", _CreateNSString(title));
        NSLog(@"description = %@", _CreateNSString(description));
        NSLog(@"imagePath = %@", _CreateNSString(imagePath));
        NSLog(@"shareMethod = %d",shareMethod);
        
        [[WXShare shareInstance] shareToWinXinWithTitle:_CreateNSString(title) description:_CreateNSString(description) imagePath:_CreateNSString(imagePath) shareMethods:shareMethod];
    }
    
    void iosSetWXShareListener(WXShareCallback callback)
    {
        wxShareCallback = callback;
    }
    
#if defined(__cplusplus)
}
#endif

@implementation WXShareUnityLibs

- (instancetype)init
{
    if (self = [super init]) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WXShareResultNotification:) name:kWXShareResultNotification object:nil];
        
    }
    return self;
}

- (void)WXShareResultNotification:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
    
    BOOL result = [[dict objectForKey:@"errorCode"] boolValue];
    
    if (result == 0) {
        
        wxShareCallback(WXSHARE, WXSHARE_FAILURE);
        
    } else {
        
        wxShareCallback(WXSHARE, WXSHARE_SUCCESS);
        
    }
    
}

@end

WXShare.h文件是为了简化功能,又封装了一层OC接口,调用的是微信的SDK。

你可能感兴趣的:(游戏开发,iOS平台开发)