From 14a6760d7206f3d71cf6ece027017090ca447c87 Mon Sep 17 00:00:00 2001 From: pengtaopt <46016818+pengtaopt@users.noreply.github.com> Date: Tue, 30 Apr 2019 15:45:36 +0800 Subject: [PATCH] fix ios call native component method before the plugin register (#2387) --- ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.h | 2 +- .../WeexSDK/Sources/Bridge/WXCoreBridge.mm | 47 +++++++++++++++++-- weex_core/Source/core/bridge/eagle_bridge.cpp | 6 +++ weex_core/Source/core/bridge/eagle_bridge.h | 2 +- .../Source/core/bridge/platform_bridge.h | 1 + 5 files changed, 51 insertions(+), 7 deletions(-) diff --git a/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.h b/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.h index dace1acabc..70b57e3c91 100644 --- a/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.h +++ b/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.h @@ -58,7 +58,7 @@ namespace WeexCore void CallNativeComponent(const char* pageId, const char* ref, const char *method, const char *arguments, int argumentsLength, const char *options, int optionsLength) override; std::unique_ptr RegisterPluginModule(const char *name, const char *class_name, const char *version) override; - + std::unique_ptr RegisterPluginComponent(const char *name, const char *class_name, const char *version) override; void SetTimeout(const char* callbackID, const char* time) override ; void NativeLog(const char* str_array) override ; diff --git a/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.mm b/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.mm index 50aa50257f..e39c901c7d 100644 --- a/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.mm +++ b/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.mm @@ -34,7 +34,7 @@ #import "WXComponentMethod.h" #import "WXExceptionUtils.h" #import "WXModuleFactory.h" - +#import "WXComponentFactory.h" #include "base/core_constants.h" #include "base/time_utils.h" #include "core/manager/weex_core_manager.h" @@ -178,6 +178,47 @@ static void MergeBorderWidthValues(NSMutableDictionary* dict, return result; } + std::unique_ptr IOSSide::RegisterPluginComponent(const char *pcstr_name, const char *pcstr_class_name, const char *pcstr_version) { + ValueWithType *returnValue = new ValueWithType(); + memset(returnValue, 0, sizeof(ValueWithType)); + returnValue->type = ParamsType::VOID; + do { + if (!pcstr_class_name) { + break; + } + NSString *className = [NSString stringWithUTF8String:pcstr_class_name]; + Class clazz = NSClassFromString(className); + if (!clazz) { + break; + } + if (!pcstr_name) { + break; + } + NSDictionary *properties = @{ @"append" : @"tree" }; + NSString *name = [NSString stringWithUTF8String:pcstr_name]; + [WXComponentFactory registerComponent:name withClass:clazz withPros:properties]; + NSMutableDictionary *info = [WXComponentFactory componentMethodMapsWithName:name]; + if (![info isKindOfClass:[NSDictionary class]]) { + break; + } + NSArray *methods = info[@"methods"]; + if (![methods isKindOfClass:[NSArray class]] || !methods.count) { + break; + } + info[@"type"] = name; + NSMutableDictionary *props = [properties mutableCopy]; + [props addEntriesFromDictionary:info]; + NSString *componentsInfo = [WXUtility JSONString:@[props]]; + if (componentsInfo.length > 0) { + returnValue->type = ParamsType::BYTEARRAYSTRING; + const char *pcstr_utf8 = [componentsInfo UTF8String]; + returnValue->value.byteArray = generator_bytes_array(pcstr_utf8, componentsInfo.length); + } + + } while (0); + + return std::unique_ptr(returnValue); + } std::unique_ptr IOSSide::RegisterPluginModule(const char *pcstr_name, const char *pcstr_class_name, const char *pcstr_version) { ValueWithType *returnValue = new ValueWithType(); @@ -196,10 +237,6 @@ static void MergeBorderWidthValues(NSMutableDictionary* dict, break; } NSString *name = [NSString stringWithUTF8String:pcstr_name]; - NSString *version = @"0"; - if (pcstr_version) { - version = [NSString stringWithUTF8String:pcstr_version]; - } NSString *moduleName = [WXModuleFactory registerModule:name withClass:clazz]; if (!moduleName.length) { break; diff --git a/weex_core/Source/core/bridge/eagle_bridge.cpp b/weex_core/Source/core/bridge/eagle_bridge.cpp index 82dcf6e03c..7f2640dc9e 100644 --- a/weex_core/Source/core/bridge/eagle_bridge.cpp +++ b/weex_core/Source/core/bridge/eagle_bridge.cpp @@ -158,6 +158,12 @@ namespace WeexCore { ->platform_side() ->RegisterPluginModule(name.c_str(), class_name.c_str(), version.c_str()); } + std::unique_ptr EagleBridge::WeexCoreHandler::RegisterPluginComponent(const std::string &name, const std::string &class_name, const std::string &version) { + return WeexCoreManager::Instance() + ->getPlatformBridge() + ->platform_side() + ->RegisterPluginComponent(name.c_str(), class_name.c_str(), version.c_str()); + } void EagleBridge::WeexCoreHandler::PostTaskOnComponentThread(const weex::base::Closure& closure) { WeexCoreManager::Instance()->getPlatformBridge()->platform_side()->PostTaskOnComponentThread(closure); diff --git a/weex_core/Source/core/bridge/eagle_bridge.h b/weex_core/Source/core/bridge/eagle_bridge.h index 7ae2c2373b..f119e6494c 100644 --- a/weex_core/Source/core/bridge/eagle_bridge.h +++ b/weex_core/Source/core/bridge/eagle_bridge.h @@ -107,7 +107,7 @@ namespace WeexCore { void PostTaskToMsgLoop(const weex::base::Closure& closure); #if OS_IOS std::unique_ptr RegisterPluginModule(const std::string &name, const std::string &class_name, const std::string &version); - + std::unique_ptr RegisterPluginComponent(const std::string &name, const std::string &class_name, const std::string &version); void PostTaskOnComponentThread(const weex::base::Closure& closure); #endif }; diff --git a/weex_core/Source/core/bridge/platform_bridge.h b/weex_core/Source/core/bridge/platform_bridge.h index b9948472ff..593e4b1095 100644 --- a/weex_core/Source/core/bridge/platform_bridge.h +++ b/weex_core/Source/core/bridge/platform_bridge.h @@ -172,6 +172,7 @@ class PlatformBridge { int options_length) = 0; #if OS_IOS virtual std::unique_ptr RegisterPluginModule(const char *name, const char *class_name, const char *version) = 0; + virtual std::unique_ptr RegisterPluginComponent(const char *name, const char *class_name, const char *version) = 0; virtual void PostTaskOnComponentThread(const weex::base::Closure closure) = 0; #endif virtual void SetTimeout(const char* callback_id, const char* time) = 0;