Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RCTBridgeWrapper.m 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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)init {
  23. self = [super init];
  24. if (self) {
  25. _bridge
  26. = [[RCTBridge alloc] initWithDelegate:self
  27. launchOptions:nil];
  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 = [serverRootWithHost(host) URLByAppendingPathComponent:@"status"];
  40. NSURLSession *session = [NSURLSession sharedSession];
  41. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  42. __block NSURLResponse *response;
  43. __block NSData *data;
  44. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  45. [[session dataTaskWithRequest:request
  46. completionHandler:^(NSData *d,
  47. NSURLResponse *res,
  48. __unused NSError *err) {
  49. data = d;
  50. response = res;
  51. dispatch_semaphore_signal(semaphore);
  52. }] resume];
  53. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  54. NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  55. return [status isEqualToString:@"packager-status:running"];
  56. }
  57. - (NSString *)guessPackagerHost {
  58. static NSString *ipGuess;
  59. static dispatch_once_t dispatchOncePredicate;
  60. dispatch_once(&dispatchOncePredicate, ^{
  61. NSString *ipPath
  62. = [[NSBundle bundleForClass:self.class] pathForResource:@"ip"
  63. ofType:@"txt"];
  64. ipGuess
  65. = [[NSString stringWithContentsOfFile:ipPath
  66. encoding:NSUTF8StringEncoding
  67. error:nil]
  68. stringByTrimmingCharactersInSet:
  69. [NSCharacterSet newlineCharacterSet]];
  70. });
  71. NSString *host = ipGuess ?: @"localhost";
  72. if ([self isPackagerRunning:host]) {
  73. return host;
  74. }
  75. return nil;
  76. }
  77. #endif
  78. #pragma mark RCTBridgeDelegate methods
  79. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  80. #if DEBUG
  81. // In debug mode, try to fetch the bundle from the packager, or fallback to
  82. // the one inside the framework. The IP address for the packager host is
  83. // fetched from the ip.txt file inside the framework.
  84. //
  85. // This duplicates some functionality present in RCTBundleURLProvider, but
  86. // that mode is not designed to work inside a framework, because all
  87. // resources are loaded from the main bundle.
  88. NSString *host = [self guessPackagerHost];
  89. if (host != nil) {
  90. NSString *path = @"/index.bundle";
  91. NSString *query = @"platform=ios&dev=true&minify=false";
  92. NSURLComponents *components
  93. = [NSURLComponents componentsWithURL:serverRootWithHost(host)
  94. resolvingAgainstBaseURL:NO];
  95. components.path = path;
  96. components.query = query;
  97. return components.URL;
  98. }
  99. #endif
  100. return [[NSBundle bundleForClass:self.class] URLForResource:@"main"
  101. withExtension:@"jsbundle"];
  102. }
  103. @end