Skip to content

Instantly share code, notes, and snippets.

@ndfred
Created December 10, 2012 18:53
Show Gist options
  • Save ndfred/4252500 to your computer and use it in GitHub Desktop.
Save ndfred/4252500 to your computer and use it in GitHub Desktop.
Fix iOS 5 not finding iPad / iPhone specific resources on iPhone-only apps running on the iPad
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import "MainViewController.h"
@interface NSBundle (TPPathForResourceFix)
- (NSString *)tp_pathForResource:(NSString *)name ofType:(NSString *)ext;
@end
@implementation NSBundle (TPPathForResourceFix)
- (NSString *)tp_pathForResource:(NSString *)name ofType:(NSString *)ext {
NSString *path = [self tp_pathForResource:name ofType:ext];
if (path == nil) {
NSString *suffix = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) ? @"~iphone" : @"~ipad";
name = [name stringByAppendingString:suffix];
path = [self tp_pathForResource:name ofType:ext];
}
return path;
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
if ([UICollectionView class]) {
// Fix iOS 5 not finding paths correctly on weird unversal apps that have been de-universalized
// See https://devforums.apple.com/message/764312
Method origMethod = class_getInstanceMethod([NSBundle class], @selector(pathForResource:ofType:));
Method overrideMethod = class_getInstanceMethod([NSBundle class], @selector(tp_pathForResource:ofType:));
method_exchangeImplementations(origMethod, overrideMethod);
}
return UIApplicationMain(argc, argv, nil, NSStringFromClass([MainViewController class]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment