您可以混淆应用程序:didReceiveRemoteNotification: [英] Can you swizzle application:didReceiveRemoteNotification:

查看:62
本文介绍了您可以混淆应用程序:didReceiveRemoteNotification:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发需要混淆AppDelegate应用程序的产品:didReceiveRemoteNotification :(我不想在appDelegate自身中调用我的新方法).

I'm currently working on a product that needs to swizzle the AppDelegate's application:didReceiveRemoteNotification: (I don't want to call my new method in the appDelegate itself).

问题是:混乱根本不起作用.在成功之前,我已经多次反复使用方法,而这次,根本没有调用替换实现.我想知道这是否是因为appDelegate方法的某些特殊性,因为这些方法是由系统而不是由应用程序调用的.

The thing is: the swizzling simply doesn't work. I've already swizzled methods several times before with success, and this time, the replacing implementation is simply not called. I was wondering if that was because of some specificity of the appDelegate's methods, since these are called by the system, and not the application.

推荐答案

我将假设您的问题中遗漏了大多数东西,这是一个好主意,总是在可能的情况下始终将问题与代码示例一起发布.

I am going to assume most of the things which are missing in your question, Its a great idea to always post the questions with code samples whenever possible.

  1. 您需要确保您要针对UIApplicationDelegate的特定实现而不是UIApplicationDelegate本身来使用方法.对于例如

  1. You need to make sure that you are swizzling your methods on the specific implementation of UIApplicationDelegate and NOT the UIApplicationDelegate itself. For Eg

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end

在这种情况下,您需要调整AppDelegate类.

In this case you need to swizzle the AppDelegate Class.

此外,如果您尝试编写静态库/框架,则可能根本不知道此类的名称.在这种情况下,最简单/最安全的方法是询问App的AppDelegate名称,并使用该名称通过NSClassFromString()检索特定的类实例,或者您也可以通过蛮力找到所需的类(因为通常有一个AppDelegate类).

Also, if you are trying to write a static library/framework you may not know the name of this class at all. In such cases simplest/safest way is to ask for App's AppDelegate name and use that to retrieve a specific class instance via NSClassFromString() or you can just brute force to find the class you need (because you usually have a Single AppDelegate class).

    unsigned int numberOfClasses = 0;
    Class *classes = objc_copyClassList(&numberOfClasses);
    Class appDelegateClass = nil;
    for (unsigned int i = 0; i < numberOfClasses; ++i) {
        if (class_conformsToProtocol(classes[i], @protocol(UIApplicationDelegate))) {
            appDelegateClass = classes[i];
        }
}

编辑

上述方法有一些缺点,

  1. 它会遍历您的应用程序代码可访问的所有类,遍历所有这些类都不是一种有效的解决方案.

  1. It iterates over all the classes accessible by your application code, looping through all of these classes is not a performant solution.

许多著名的SDK都会混乱或动态扩展您的 AppDelegate ,因为很多有趣的事情都可以在此地方被截获.这也意味着您的应用程序可能具有 UIApplicationDelegate 协议的多个实现,并且上面的代码可能只是选择了其中的任何实现,从而导致了一些严重的问题.

Many famous SDKs will swizzle or dynamically extend your AppDelegate, because a lot of interesting things can be intercepted at this single place. This also means that your application may have more than one implementation of UIApplicationDelegate protocol and the code above may just pick any implementation of it, causing some serious issues.

克里斯在下面的评论中建议,仅使用 [[UIApplication sharedApplication] .delegate类] 更为安全和高效.在调用这一行代码时,应该可以为您提供iOS应用程序已知的确切AppDelegate实现.

As, Chris suggested in comments below, its much safer and performant to just use [[UIApplication sharedApplication].delegate class]. which should give you the exact AppDelegate implementation known to iOS application at the moment of calling this line of code.

这篇关于您可以混淆应用程序:didReceiveRemoteNotification:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆