BundleUtil.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "BundleUtil.h"
  21. @implementation BundleUtil
  22. + (NSBundle *)frameworkBundle {
  23. NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:THREEMA_FRAMEWORK_IDENTIFIER];
  24. return frameworkBundle;
  25. }
  26. + (NSBundle *)mainBundle {
  27. return [NSBundle mainBundle];
  28. }
  29. + (id)objectForInfoDictionaryKey:(NSString *)key {
  30. id value = [[self frameworkBundle] objectForInfoDictionaryKey:key];
  31. if (value == nil) {
  32. // fall back to main bundle
  33. value = [[NSBundle mainBundle] objectForInfoDictionaryKey:key];
  34. }
  35. return value;
  36. }
  37. + (NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type {
  38. NSString *path = [[self frameworkBundle] pathForResource:resource ofType:type];
  39. if (path == nil) {
  40. // fall back to main bundle
  41. path = [[NSBundle mainBundle] pathForResource:resource ofType:type];
  42. }
  43. return path;
  44. }
  45. + (NSURL *)URLForResource:(NSString *)resourceName withExtension:(NSString *)extension {
  46. NSURL *url = [[self frameworkBundle] URLForResource:resourceName withExtension:extension];
  47. if (url == nil) {
  48. // fall back to main bundle
  49. url = [[NSBundle mainBundle] URLForResource:resourceName withExtension:extension];
  50. }
  51. return url;
  52. }
  53. + (UIImage *)imageNamed:(NSString *)imageName {
  54. NSBundle *frameworkBundle = [self frameworkBundle];
  55. UIImage *image;
  56. if ([UIImage respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)])
  57. {
  58. image = [UIImage imageNamed:imageName inBundle:frameworkBundle compatibleWithTraitCollection:nil];
  59. } else {
  60. image = [UIImage imageNamed:[NSString stringWithFormat:@"ThreemaFramework.bundle/%@", imageName]];
  61. }
  62. if (image) {
  63. return image;
  64. }
  65. image = [UIImage imageNamed:imageName];
  66. if (image) {
  67. return image;
  68. }
  69. NSString *path = [frameworkBundle pathForResource:imageName ofType:nil];
  70. if ([path length] > 0) {
  71. image = [UIImage imageWithContentsOfFile:path];
  72. }
  73. return image;
  74. }
  75. + (NSString *)localizedStringForKey:(NSString *)key {
  76. NSString *value = [[self frameworkBundle] localizedStringForKey:key value:nil table:nil];
  77. if (value && [value isEqualToString:key] == NO) {
  78. return value;
  79. } else {
  80. return NSLocalizedString(key, nil);
  81. }
  82. }
  83. + (UIView *)loadXibNamed:(NSString *)name {
  84. NSBundle *bundle = [NSBundle mainBundle];
  85. NSArray *nibs = [bundle loadNibNamed:name owner:self options:nil];
  86. if (nibs == nil) {
  87. // fall back to main bundle
  88. nibs = [[NSBundle mainBundle] loadNibNamed:name owner:self options:nil];
  89. }
  90. UIView *view = [nibs objectAtIndex:0];
  91. return view;
  92. }
  93. @end