美文网首页
IOS SDK内捕获AppDelegate代理方法

IOS SDK内捕获AppDelegate代理方法

作者: 蜗牛1992 | 来源:发表于2018-11-14 18:04 被阅读29次

最近项目中遇到了SDK内需要横屏,而工程必须竖屏的情况,解决方法有两个:
1.最保险的方法,工程AppDelegate.h添加

@property (nonatomic, strong) NSNumber* allowRotation;  // 横竖屏
//AppDelegate.m处实现
//////横屏方式
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//    NSLog(@"application");
//    if ([_allowRotation boolValue]) {
//        return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
//    }else{
//        return (UIInterfaceOrientationMaskPortrait);
//    }
//}

2.SDK捕获AppDelegate方法,只要工程实现了-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window代理方法就可以了。方法没完善,就只写主要部分了。

//调用处
[self hookMehod:@selector(SDKApplication:supportedInterfaceOrientationsForWindow:) andDef:@selector(defaultApplication:supportedInterfaceOrientationsForWindow:) andNew:@selector(application:supportedInterfaceOrientationsForWindow:)];
//方法:
-(void)hookMehod:(SEL)oldSEL andDef:(SEL)defaultSEL andNew:(SEL)newSEL {
    
    Class oldClass = objc_getClass([@"AppDelegate" UTF8String]);
    Class newClass = [self class];
    id delegate = [UIApplication sharedApplication].delegate;
    SEL selector = NSSelectorFromString(@"application:supportedInterfaceOrientationsForWindow:");
    if ([delegate respondsToSelector:selector]) {
        NSLog(@"respondsToSelector=YES");
        //把方法加给原Class
        class_addMethod(oldClass, newSEL, class_getMethodImplementation(newClass, newSEL), nil);
        class_addMethod(oldClass, oldSEL, class_getMethodImplementation(newClass, defaultSEL),nil);
        
        Method oldMethod = class_getInstanceMethod(oldClass, oldSEL);
        assert(oldMethod);
        Method newMethod = class_getInstanceMethod(oldClass, newSEL);
        assert(newMethod);
        method_exchangeImplementations(oldMethod, newMethod);
    }else{
        NSLog(@"AppDelegate未实现application:supportedInterfaceOrientationsForWindow:");
//此处强制添加实现的方法,我没加。有兴趣添加的麻烦评论区告诉我下。
    }
}

-(UIInterfaceOrientationMask)defaultApplication:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSLog(@"defaultApplication");
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

-(UIInterfaceOrientationMask)SDKApplication:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSLog(@"SDKDelegatesetAllowRotation");
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

相关文章

  • IOS SDK内捕获AppDelegate代理方法

    最近项目中遇到了SDK内需要横屏,而工程必须竖屏的情况,解决方法有两个:1.最保险的方法,工程AppDelegat...

  • SDK开发中捕获AppDelegate通知

    1.需要WMAppDelegateProxy类 @interfaceWMAppDelegateProxy :NSP...

  • iOS 集成PayPal付款

    1、使用cocoapods pod'PayPal-iOS-SDK' 2、AppDelegate 中key 的设置...

  • AppDelegate

    AppDelegate为整个应用的一个代理,提供程序启动、退出等类似监控的接口。 IOS AppDelegate方...

  • 防止页面上多个按钮同时点击

    AppDelegate 的 代理方法didFinishLaunchingWithOptions 中添加 [[UI...

  • iOS开发:崩溃捕获

    崩溃的捕获 需要在AppDelegate中注册并实现方法即可。 1:注册 - (BOOL)application:...

  • 练习代码day1

    //应用程序代理类 //AppDelegate中的方法都是UIApplicationDelegatez的协议方法 ...

  • Appdelegate中代理方法

    最近项目要加上一个手势解锁,然而手机解锁的逻辑十分混乱,需要与应用程序启动、应用前后台切换执行的delegate ...

  • 集成CC视频sdk时关于连麦集成

    一 SDK连麦相关方法介绍 SDK连麦代理五个,调用连麦的SDK公有方法五个。详细介绍如下: SDK连麦代理 1....

  • iOS内存警告

    1.当iOS系统内存不足,会发出内存警告的通知。首先调用的是 AppDelegate 里重新的内存警告代理方法 2...

网友评论

      本文标题:IOS SDK内捕获AppDelegate代理方法

      本文链接:https://www.haomeiwen.com/subject/koojfqtx.html