powerfun-unity/Assets/Plugins/iOS/WechatNativeBridge.m

111 lines
4.0 KiB
Mathematica
Raw Permalink Normal View History

2021-08-23 09:28:14 +08:00
//
// NativeBridge.m
// Unity-iPhone
//
#import <Foundation/Foundation.h>
#import "WXApi.h"
//appid
void RegisterApp(const char* appid)
{
NSString *weichatId = [NSString stringWithFormat:@"%s", appid];
BOOL installed = [WXApi isWXAppInstalled];
NSLog(@"installed result: %@", installed?@"true":@"false");
BOOL result = [WXApi registerApp:weichatId universalLink:(@"https://wx.powerfun.com.cn/pf2/")];
NSLog(@"result: %@", result?@"true":@"false");
}
//
void LaunchMiniProgram(const char* miniProgramID, const char* path){
WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
launchMiniProgramReq.userName = [NSString stringWithUTF8String:miniProgramID]; //username
launchMiniProgramReq.path = [NSString stringWithUTF8String:path]; //// query "?foo=bar"
launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; //
[WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {}];
}
//
void ShareImgToWX(int scene, UInt8 *msgByteArrayData, int arrayLength, const char* title, const char* description){
WXImageObject *imageObject = [WXImageObject object];
imageObject.imageData = [[NSData alloc] initWithBytes:msgByteArrayData length:arrayLength];
WXMediaMessage *message = [WXMediaMessage message];
message.title = [NSString stringWithUTF8String:title];
message.description = [NSString stringWithUTF8String:description];
// APPIcon
NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
[message setThumbImage:[UIImage imageNamed:icon]];
message.mediaObject = imageObject;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = scene;
[WXApi sendReq:req completion:nil];
}
//
void ShareUrlToWX(int scene, const char* url, const char* title, const char* description){
WXWebpageObject *webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = [NSString stringWithUTF8String:url];
WXMediaMessage *message = [WXMediaMessage message];
message.title = [NSString stringWithUTF8String:title];
message.description = [NSString stringWithUTF8String:description];
// APPIcon
NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
[message setThumbImage:[UIImage imageNamed:icon]];
message.mediaObject = webpageObject;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = scene;
[WXApi sendReq:req completion:nil];
}
//
bool IsWechatInstalled_iOS()
{
return [WXApi isWXAppInstalled];
}
//
void WechatLogin(const char* scope,const char* state)
{
//
SendAuthReq* req = [[SendAuthReq alloc] init];
req.scope = [NSString stringWithUTF8String:scope];
req.state = [NSString stringWithUTF8String:state];
[WXApi sendReq:req completion:nil];
}
//(appid)
void OpenWXApp()
{
[WXApi openWXApp];
2021-08-26 15:08:02 +08:00
}
bool checkAPPIsExist(const char * URLScheme)
2021-08-26 15:08:02 +08:00
{
NSURL* url;
NSString * urls = [NSString stringWithUTF8String:URLScheme];
if ([urls containsString:@"://"])
2021-08-26 15:08:02 +08:00
{
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",urls]];
2021-08-26 15:08:02 +08:00
}
else
{
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@://",urls]];
2021-08-26 15:08:02 +08:00
}
if([[UIApplication sharedApplication] canOpenURL:url])
{
return YES;
}
else
{
return NO;
}
2021-08-23 09:28:14 +08:00
}