-
-
Save XueshiQiao/14c66f5d2bfe902676b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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