2020-07-31 14:47:29 +08:00
|
|
|
|
#import "RNWechat.h"
|
|
|
|
|
|
2020-08-01 18:05:40 +08:00
|
|
|
|
// Define error messages
|
|
|
|
|
#define INVOKE_FAILED (@"WeChat API invoke returns false.")
|
|
|
|
|
|
|
|
|
|
@implementation RNWechat {
|
|
|
|
|
BOOL *_api;
|
|
|
|
|
}
|
2020-07-31 14:47:29 +08:00
|
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
2020-08-01 18:05:40 +08:00
|
|
|
|
- (instancetype)init {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOpenURL:) name:@"RCTOpenURLNotification" object:nil];
|
2020-08-05 12:42:05 +08:00
|
|
|
|
// 在register之前打开log, 后续可以根据log排查问题
|
|
|
|
|
[WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) {
|
|
|
|
|
NSLog(@"WeChatSDK: %@", log);
|
|
|
|
|
}];
|
2020-08-01 18:05:40 +08:00
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
|
{
|
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)handleOpenURL:(NSNotification *)aNotification
|
|
|
|
|
{
|
|
|
|
|
NSString * aURLString = [aNotification userInfo][@"url"];
|
|
|
|
|
NSURL * aURL = [NSURL URLWithString:aURLString];
|
|
|
|
|
|
|
|
|
|
if ([WXApi handleOpenURL:aURL delegate:self])
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
} else {
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-31 14:47:29 +08:00
|
|
|
|
|
2020-08-01 18:05:40 +08:00
|
|
|
|
- (dispatch_queue_t)methodQueue
|
2020-07-31 14:47:29 +08:00
|
|
|
|
{
|
2020-08-01 18:05:40 +08:00
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (BOOL)requiresMainQueueSetup {
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 注册 appid
|
2020-08-05 12:42:05 +08:00
|
|
|
|
RCT_REMAP_METHOD(registerApp, appid:(NSString *)appid universalLink:(NSString*)universalLink resolver: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
|
@try {
|
2020-08-01 19:44:52 +08:00
|
|
|
|
self.appId = appid;
|
2020-08-05 12:42:05 +08:00
|
|
|
|
resolve(@([WXApi registerApp: appid universalLink: universalLink]));
|
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
|
reject(@"-10404", [NSString stringWithFormat:@"%@ %@", exception.name, exception.userInfo], nil);
|
2020-08-01 18:05:40 +08:00
|
|
|
|
}
|
2020-07-31 14:47:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 19:44:52 +08:00
|
|
|
|
// 检查微信是否已被用户安装, 微信已安装返回YES,未安装返回NO。
|
2020-08-02 02:04:01 +08:00
|
|
|
|
RCT_EXPORT_METHOD(isWXAppInstalled: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2020-08-01 19:44:52 +08:00
|
|
|
|
if ([WXApi isWXAppInstalled]) {
|
|
|
|
|
resolve(@YES);
|
|
|
|
|
} else {
|
|
|
|
|
resolve(@NO);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-01 19:47:56 +08:00
|
|
|
|
|
2020-08-05 12:42:05 +08:00
|
|
|
|
/*! @brief 打开微信
|
|
|
|
|
* @return 成功返回YES,失败返回NO。
|
|
|
|
|
*/
|
|
|
|
|
RCT_EXPORT_METHOD(openWXApp: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
|
if ([WXApi openWXApp]) {
|
|
|
|
|
resolve(@YES);
|
|
|
|
|
} else {
|
|
|
|
|
resolve(@NO);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 19:47:56 +08:00
|
|
|
|
// 判断当前微信的版本是否支持OpenApi,支持返回YES,不支持返回NO。
|
2020-08-02 02:04:01 +08:00
|
|
|
|
RCT_EXPORT_METHOD(isWXAppSupportApi: (RCTPromiseResolveBlock)resolve :(RCTPromiseRejectBlock)reject) {
|
2020-08-01 19:47:56 +08:00
|
|
|
|
if ([WXApi isWXAppSupportApi]) {
|
|
|
|
|
resolve(@YES);
|
|
|
|
|
} else {
|
|
|
|
|
resolve(@NO);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-02 02:04:01 +08:00
|
|
|
|
// 获取当前微信SDK的版本号
|
|
|
|
|
RCT_EXPORT_METHOD(getApiVersion: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
|
resolve([WXApi getApiVersion]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 14:47:29 +08:00
|
|
|
|
@end
|