RNDevManager.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // RNDevManager.m
  3. // taroDemo
  4. //
  5. // Created by 谢伟 on 2021/7/23.
  6. //
  7. #import "RNDevManager.h"
  8. #import <React/RCTBundleURLProvider.h>
  9. #import <React/RCTBridge+Private.h>
  10. #import <React/RCTReloadCommand.h>
  11. @implementation RNDevManager
  12. @synthesize bridge = _bridge;
  13. RCT_EXPORT_MODULE();
  14. + (BOOL)requiresMainQueueSetup
  15. {
  16. return YES;
  17. }
  18. RCT_EXPORT_METHOD(loadBundleByBundleUrl:(NSString *)bundleUrl bundleRoot:(NSString *)bundleRoot)
  19. {
  20. if (bundleUrl.length <= 0 || bundleRoot.length <= 0) {
  21. [self setDefaultJSBundle];
  22. return;
  23. }
  24. [RCTBundleURLProvider sharedSettings].jsLocation = bundleUrl;
  25. if (_bridge) {
  26. NSURL *bundleURL = [[RCTBundleURLProvider sharedSettings]
  27. jsBundleURLForBundleRoot:bundleRoot];
  28. _bridge.bundleURL = bundleURL;
  29. RCTTriggerReloadCommandListeners(@"Dev menu - apply changes");
  30. }
  31. }
  32. RCT_EXPORT_METHOD(loadDefaultBundle)
  33. {
  34. [self setDefaultJSBundle];
  35. }
  36. - (void)setDefaultJSBundle
  37. {
  38. [[RCTBundleURLProvider sharedSettings] resetToDefaults];
  39. self->_bridge.bundleURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForFallbackExtension:nil];
  40. RCTTriggerReloadCommandListeners(@"Dev menu - reset to default");
  41. }
  42. /**
  43. * This code was copied from "react-native-bundle-loader"
  44. * <https://github.com/jusbrasil/react-native-bundle-loader/blob/master/ios/BundleLoader.m>.
  45. * The use of the source code of this file is also subject to the terms
  46. * and consitions of the license of "react-native-bundle-loader" (MIT, see
  47. * </licenses/LICENSE-react-native-bundle-loader>).
  48. */
  49. RCT_EXPORT_METHOD(load:(NSURL*) url) {
  50. if ([NSThread isMainThread]) {
  51. [self loadBundle:url];
  52. } else {
  53. dispatch_sync(dispatch_get_main_queue(), ^{
  54. [self loadBundle:url];
  55. });
  56. }
  57. return;
  58. }
  59. - (void)loadBundle:(NSURL *)url
  60. {
  61. [_bridge setValue:url forKey:@"bundleURL"];
  62. RCTTriggerReloadCommandListeners(@"reload");
  63. }
  64. /**
  65. * license statement end
  66. */
  67. @end