From 9076b71ac307d2a45b30efec41414862638c6aeb Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 29 Sep 2015 10:33:43 -0700 Subject: [PATCH] Use bundleForClass instead of mainBundle to find resources Reviewed By: @jspahrsummers Differential Revision: D2485109 --- .../RCTConvert_NSURLTests.m | 2 +- Libraries/Image/RCTAssetBundleImageLoader.m | 7 ++-- Libraries/Image/RCTImageDownloader.m | 3 +- React/Base/RCTConvert.m | 40 ++++++++----------- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTConvert_NSURLTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTConvert_NSURLTests.m index a42d587b0..0de322a88 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTConvert_NSURLTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTConvert_NSURLTests.m @@ -36,7 +36,7 @@ } \ #define TEST_BUNDLE_PATH(name, _input, _expectedPath) \ -TEST_PATH(name, _input, [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:_expectedPath]) +TEST_PATH(name, _input, [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingPathComponent:_expectedPath]) // Basic tests TEST_URL(basic, @"http://example.com", @"http://example.com") diff --git a/Libraries/Image/RCTAssetBundleImageLoader.m b/Libraries/Image/RCTAssetBundleImageLoader.m index fb9aaa057..3ac517c41 100644 --- a/Libraries/Image/RCTAssetBundleImageLoader.m +++ b/Libraries/Image/RCTAssetBundleImageLoader.m @@ -21,7 +21,7 @@ RCT_EXPORT_MODULE() return nil; } - NSString *resourcesPath = [NSBundle mainBundle].resourcePath; + NSString *resourcesPath = [NSBundle bundleForClass:[self class]].resourcePath; NSString *requestPath = requestURL.absoluteURL.path; if (requestPath.length < resourcesPath.length + 1) { return nil; @@ -37,8 +37,9 @@ RCT_EXPORT_MODULE() return NO; } - if ([[NSBundle mainBundle] URLForResource:imageName withExtension:nil] || - [[NSBundle mainBundle] URLForResource:imageName withExtension:@"png"]) { + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + if ([bundle URLForResource:imageName withExtension:nil] || + [bundle URLForResource:imageName withExtension:@"png"]) { return YES; } diff --git a/Libraries/Image/RCTImageDownloader.m b/Libraries/Image/RCTImageDownloader.m index c2aa89f73..e299b1328 100644 --- a/Libraries/Image/RCTImageDownloader.m +++ b/Libraries/Image/RCTImageDownloader.m @@ -39,7 +39,8 @@ RCT_EXPORT_MODULE() // Have to exclude 'file://' from the main bundle, otherwise this would conflict with RCTAssetBundleImageLoader return [requestURL.scheme compare:@"http" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 4)] == NSOrderedSame || - ([requestURL.scheme caseInsensitiveCompare:@"file"] == NSOrderedSame && ![requestURL.path hasPrefix:[NSBundle mainBundle].resourcePath]) || + ([requestURL.scheme caseInsensitiveCompare:@"file"] == NSOrderedSame && + ![requestURL.path hasPrefix:[NSBundle bundleForClass:[self class]].resourcePath]) || [requestURL.scheme caseInsensitiveCompare:@"data"] == NSOrderedSame; } diff --git a/React/Base/RCTConvert.m b/React/Base/RCTConvert.m index aa99b3e44..79efea631 100644 --- a/React/Base/RCTConvert.m +++ b/React/Base/RCTConvert.m @@ -101,7 +101,7 @@ RCT_CUSTOM_CONVERTER(NSData *, NSData, [json dataUsingEncoding:NSUTF8StringEncod path = path.stringByExpandingTildeInPath; } else if (!path.absolutePath) { // Assume it's a resource path - path = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:path]; + path = [[NSBundle bundleForClass:[self class]].resourcePath stringByAppendingPathComponent:path]; } return [NSURL fileURLWithPath:path]; } @@ -426,28 +426,22 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[ NSURL *URL = [self NSURL:path]; NSString *scheme = URL.scheme.lowercaseString; - if (path && [scheme isEqualToString:@"file"]) { - if (RCT_DEBUG || [NSThread currentThread] == [NSThread mainThread]) { - if ([URL.path hasPrefix:[NSBundle mainBundle].resourcePath]) { - // Image may reside inside a .car file, in which case we have no choice - // but to use +[UIImage imageNamed] - but this method isn't thread safe - static NSMutableDictionary *XCAssetMap = nil; - if (!XCAssetMap) { - XCAssetMap = [NSMutableDictionary new]; - } - NSNumber *isAsset = XCAssetMap[path]; - if (!isAsset || isAsset.boolValue) { - image = [UIImage imageNamed:path]; - if (RCT_DEBUG && image) { - // If we succeeded in loading the image via imageNamed, and the - // method wasn't called on the main thread, that's a coding error - RCTAssertMainThread(); - } - } - if (!isAsset) { - // Avoid calling `+imageNamed` again in future if it's not needed. - XCAssetMap[path] = @(image != nil); - } + if (URL && [scheme isEqualToString:@"file"]) { + RCTAssertMainThread(); + if ([URL.path hasPrefix:[NSBundle bundleForClass:[self class]].resourcePath]) { + // Image may reside inside a .car file, in which case we have no choice + // but to use +[UIImage imageNamed] - but this method isn't thread safe + static NSMutableDictionary *XCAssetMap = nil; + if (!XCAssetMap) { + XCAssetMap = [NSMutableDictionary new]; + } + NSNumber *isAsset = XCAssetMap[path]; + if (!isAsset || isAsset.boolValue) { + image = [UIImage imageNamed:URL.path]; + } + if (!isAsset) { + // Avoid calling `+imageNamed` again in future if it's not needed. + XCAssetMap[path] = @(image != nil); } }