Skip to content

Instantly share code, notes, and snippets.

@onevcat
Created October 28, 2014 02:43
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save onevcat/3fd4a08cc6f36d88376b to your computer and use it in GitHub Desktop.
Save onevcat/3fd4a08cc6f36d88376b to your computer and use it in GitHub Desktop.
Swizzle to solve Landscape mode in a Portrait app. Can be used in third party framework. If you are working on an app, you can only modify the app delegate to achieve it.
#pragma mark - Magic
// Need for supporting orientation when not supported in host app plist.
// Swizzle original -application:supportedInterfaceOrientationsForWindow: to change supported orientation in runtime.
-(void) swizzleSupportedInterfaceOrientationsForWindow
{
Class applicationDelegateClass = [[UIApplication sharedApplication].delegate class];
Class sdkClass = [self class];
SEL originalSelector = @selector(application:supportedInterfaceOrientationsForWindow:);
SEL swizzledSelector = @selector(las_application:supportedInterfaceOrientationsForWindow:);
Method originalMethod = class_getInstanceMethod(applicationDelegateClass, originalSelector);
if (originalMethod == NULL) {
SEL defaultSelector = @selector(las_default_application:supportedInterfaceOrientationsForWindow:);
Method defaultMethod = class_getInstanceMethod(sdkClass, defaultSelector);
class_addMethod(applicationDelegateClass, originalSelector, method_getImplementation(defaultMethod), method_getTypeEncoding(defaultMethod));
originalMethod = class_getInstanceMethod(applicationDelegateClass, originalSelector);
// We should reset the delegate to let application call correct method after adding method
self.appDelegate = [UIApplication sharedApplication].delegate;
[UIApplication sharedApplication].delegate = nil;
[UIApplication sharedApplication].delegate = self.appDelegate;
}
Method swizzledMethod = class_getInstanceMethod(sdkClass, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
}
-(NSUInteger)xxx_application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([XXXSdk sharedManager].showing) {
return UIInterfaceOrientationMaskAll;
} else {
return [[XXXSdk sharedManager] xxx_application:application supportedInterfaceOrientationsForWindow:window];
}
}
-(NSUInteger)xxx_default_application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return [[XXXSdk sharedManager] plistSupportedOrientation];
}
-(NSUInteger) plistSupportedOrientation {
if (_plistSupportedOrientation == 0) {
NSArray *supported = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
[supported enumerateObjectsUsingBlock:^(NSString *string, NSUInteger idx, BOOL *stop) {
if ([string caseInsensitiveCompare:@"UIInterfaceOrientationPortrait"] == NSOrderedSame) {
_plistSupportedOrientation |= UIInterfaceOrientationMaskPortrait;
} else if ([string caseInsensitiveCompare:@"UIInterfaceOrientationPortraitUpsideDown"] == NSOrderedSame) {
_plistSupportedOrientation |= UIInterfaceOrientationMaskPortraitUpsideDown;
} else if ([string caseInsensitiveCompare:@"UIInterfaceOrientationLandscapeLeft"] == NSOrderedSame) {
_plistSupportedOrientation |= UIInterfaceOrientationMaskLandscapeLeft;
} else if ([string caseInsensitiveCompare:@"UIInterfaceOrientationLandscapeRight"] == NSOrderedSame) {
_plistSupportedOrientation |= UIInterfaceOrientationMaskLandscapeRight;
}
}];
// No orientation written in plist. It means all orientations are supported.
if (_plistSupportedOrientation == 0) {
_plistSupportedOrientation = NSUIntegerMax;
}
}
return _plistSupportedOrientation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment