You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RCTBridgeWrapper.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright @ 2017-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "RCTBridgeWrapper.h"
  17. /**
  18. * Wrapper around RCTBridge which also implements the RCTBridgeDelegate methods,
  19. * allowing us to specify where the bundles are loaded from.
  20. */
  21. @implementation RCTBridgeWrapper
  22. - (instancetype)initWithLaunchOptions:(NSDictionary *)launchOptions {
  23. self = [super init];
  24. if (self) {
  25. _bridge
  26. = [[RCTBridge alloc] initWithDelegate:self
  27. launchOptions:launchOptions];
  28. }
  29. return self;
  30. }
  31. #pragma mark helper methods for getting the packager URL
  32. #if DEBUG
  33. static NSURL *serverRootWithHost(NSString *host) {
  34. return
  35. [NSURL URLWithString:
  36. [NSString stringWithFormat:@"http://%@:8081/", host]];
  37. }
  38. - (BOOL)isPackagerRunning:(NSString *)host {
  39. NSURL *url
  40. = [serverRootWithHost(host) URLByAppendingPathComponent:@"status"];
  41. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  42. NSURLResponse *response;
  43. NSData *data = [NSURLConnection sendSynchronousRequest:request
  44. returningResponse:&response
  45. error:NULL];
  46. NSString *status = [[NSString alloc] initWithData:data
  47. encoding:NSUTF8StringEncoding];
  48. return [status isEqualToString:@"packager-status:running"];
  49. }
  50. - (NSString *)guessPackagerHost {
  51. static NSString *ipGuess;
  52. static dispatch_once_t dispatchOncePredicate;
  53. dispatch_once(&dispatchOncePredicate, ^{
  54. NSString *ipPath
  55. = [[NSBundle bundleForClass:self.class] pathForResource:@"ip"
  56. ofType:@"txt"];
  57. ipGuess
  58. = [[NSString stringWithContentsOfFile:ipPath
  59. encoding:NSUTF8StringEncoding
  60. error:nil]
  61. stringByTrimmingCharactersInSet:
  62. [NSCharacterSet newlineCharacterSet]];
  63. });
  64. NSString *host = ipGuess ?: @"localhost";
  65. if ([self isPackagerRunning:host]) {
  66. return host;
  67. }
  68. return nil;
  69. }
  70. #endif
  71. #pragma mark RCTBridgeDelegate methods
  72. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  73. #if DEBUG
  74. // In debug mode, try to fetch the bundle from the packager, or fallback to
  75. // the one inside the framework. The IP address for the packager host is
  76. // fetched from the ip.txt file inside the framework.
  77. //
  78. // This duplicates some functionality present in RCTBundleURLProvider, but
  79. // that mode is not designed to work inside a framework, because all
  80. // resources are loaded from the main bundle.
  81. NSString *host = [self guessPackagerHost];
  82. if (host != nil) {
  83. NSString *path = @"/index.ios.bundle";
  84. NSString *query = @"platform=ios&dev=true&minify=false";
  85. NSURLComponents *components
  86. = [NSURLComponents componentsWithURL:serverRootWithHost(host)
  87. resolvingAgainstBaseURL:NO];
  88. components.path = path;
  89. components.query = query;
  90. return components.URL;
  91. }
  92. #endif
  93. return [[NSBundle bundleForClass:self.class] URLForResource:@"main"
  94. withExtension:@"jsbundle"];
  95. }
  96. @end