简体   繁体   English

obj-c中有什么方法可以挂接到应用程序委托方法

[英]is there any way in obj-c to hook into application delegate methods

The problem is : 问题是

I use Marmalade SDK ( http://www.madewithmarmalade.com/ ). 我使用Marmalade SDK( http://www.madewithmarmalade.com/ )。

Marmalade SDK offers opportunity to write platform-specific plugins (extension in their terminology). Marmalade SDK提供了编写平台特定插件(其术语的扩展)的机会。

Every extension is compiled as static-library and linked to the main application (Loader in their terminology) with special deploy tool. 每个扩展都被编译为静态库,并使用特殊的部署工具链接到主应用程序(在其术语中为Loader)。

Sources of the main application are not opened, thus we have no ability to implement/override app delegate methods. 主应用程序的源未打开,因此我们无法实现/覆盖应用程序委托方法。

I need an extension for managing remote push notifications via Upsight SDK (formerly PLayhaven). 我需要一个扩展程序,用于通过Upsight SDK(以前称为PLayhaven)管理远程推送通知。

ios 8 came and requires some additional efforts for remote push notifications registration. ios 8出现了,它需要一些额外的精力来注册远程推送通知。 In particular, didRegisterForRemoteNotificationsWithDeviceToken and didRegisterUserNotificationSettings should be implemented. 特别是, didRegisterForRemoteNotificationsWithDeviceTokendidRegisterUserNotificationSettings应该被实现。

The questions are : 问题是

How can I inject into application delegate methods from static library (having no access to the sources of the main application)? 如何从静态库(无法访问主应用程序的源)注入应用程序委托方法?

The thoughts were : 当时的想法是

1) Using categories. 1)使用类别。 I tried to add new methods to UIApplication class, then exchanged them with original ones. 我尝试将新方法添加到UIApplication类,然后将其与原始方法交换。 My new methods did their work and then invoked original methods. 我的新方法完成了工作,然后调用了原始方法。 But it seems not working. 但似乎不起作用。 My obj-c experience is pretty poor. 我的obj-c经验很差。 Either my class extension was wrong implemented or the original idea was incorrect: 我的类扩展实现不正确,或者原始想法不正确:

   @interface UIApplication(MyDelegate)
    - (void)application:(UIApplication *)application newDidRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
    @end

    @implementation UIApplication(MyDelegate)
    - (void)application:(UIApplication *)application newDidRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
    {
        NSLog(@"PLAYHAVEN: didRegisterUserNotificationSettings:%@", notificationSettings);
        [[PHPushProvider sharedInstance] registerUserNotificationSettings:notificationSettings];
    }
    @end

    s3eResult s3ePlayhavenInit_platform()
    {

        // Add any platform-specific initialisation code here
        myDelegate = [[MyDeleagte alloc] init];

        oldHandler1 = class_getInstanceMethod([UIApplication class], @selector(application:didRegisterUserNotificationSettings:));
        newHandler1 = class_getInstanceMethod([UIApplication class], @selector(application:newDidRegisterUserNotificationSettings:));
        method_exchangeImplementations(oldHandler1, newHandler1);

        ...
        return S3E_RESULT_SUCCESS;
    }

2) Using NSNotificationCenter listen to the corresponding notifications: 2)使用NSNotificationCenter监听相应的通知:

[[NSNotificationCenter defaultCenter] addObserver:myDelegate selector:@selector(didRegisterUserNotificationSettings:) name:@"UIApplicationDidRegisterUserNotificationSettings" object:nil];

But I'm not sure that the notification with such name will be posted at all. 但是我不确定带有这样名称的通知是否会发布。 I did not managed to find any relevant constans in UIApplication class. 我没有在UIApplication类中找到任何相关的常量。 As result, this also doesn't work. 结果,这也不起作用。

The final question is: 最后一个问题是:

It's possible at all? 有可能吗? Any ideas? 有任何想法吗?

Thanks all. 谢谢大家

This technique (1) would work apart from didRegisterUserNotificationSettings function is a part of UIApplicationDelegate . 该技术(1)可以与didApplicationUserNotificationSettings函数分开工作, 函数是UIApplicationDelegate的一部分。 The trick to get the right delegate is to hook setDelegate function of UIApplication and then do method swizzling on delegate class. 获取正确的委托的技巧是,钩住UIApplication的 setDelegate函数,然后对委托类进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM