ios - 如何覆盖包含 block 的objC中的方法?

标签 ios objective-c push-notification overriding

我知道如何覆盖 Objective-C 中的方法。我按照以下步骤操作,一切正常。问题是当原始方法包含一个 block 时,我不知道该怎么做。如果您能在下面修复/添加我的代码,我将不胜感激。

在下面的代码中,我用我不知道如何覆盖的 block 注释掉了方法。仅供引用:我正在覆盖与推送通知服务相关的代码。

    id delegate = [[UIApplication sharedApplication] delegate];
    Class objectClass = object_getClass(delegate);
    NSString *newClassName = [NSString stringWithFormat:@"Custom_%@", NSStringFromClass(objectClass)];
    Class modDelegate = NSClassFromString(newClassName);

    if (modDelegate == nil)
    {
        modDelegate = objc_allocateClassPair(objectClass, [newClassName UTF8String], 0);

        SEL selectorToOverride4 = @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:);
        SEL selectorToOverride5 = @selector(application:didFailToRegisterForRemoteNotificationsWithError:);
        SEL selectorToOverride6 = @selector(application:didReceiveRemoteNotification:);
        //SEL selectorToOverride7 = @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:);

        // get the info on the method we're going to override
        Method m4 = class_getInstanceMethod(objectClass, selectorToOverride4);
        Method m5 = class_getInstanceMethod(objectClass, selectorToOverride5);
        Method m6 = class_getInstanceMethod(objectClass, selectorToOverride6);
        //Method m7 = class_getInstanceMethod(objectClass, selectorToOverride7);

        // add the method to the new class
        class_addMethod(modDelegate, selectorToOverride4, (IMP)didRegisterSuccess, method_getTypeEncoding(m4));
        class_addMethod(modDelegate, selectorToOverride5, (IMP)didRegisterFailure, method_getTypeEncoding(m5));
        class_addMethod(modDelegate, selectorToOverride6, (IMP)didReceiveRemoteNotification, method_getTypeEncoding(m6));
        //class_addMethod(modDelegate, selectorToOverride7, (IMP)didReceiveRemoteNotificationFetchCompletionHandler, method_getTypeEncoding(m7));

        // register the new class with the runtime
        objc_registerClassPair(modDelegate);
    }

    // change the class of the object
    object_setClass(delegate, modDelegate);

void didRegisterSuccess(id self, SEL _cmd, UIApplication *application, NSData *deviceToken)
{
    [base didRegisterSuccess:application deviceToken:deviceToken];
}

void didRegisterFailure(id self, SEL _cmd, UIApplication *application, NSError *error)
{
    [base didRegisterFailure:application error:error];
}

void didReceiveRemoteNotification(id self, SEL _cmd, UIApplication *application, NSDictionary *userInfo)
{
    [base didReceiveRemoteNotification:application userInfo:userInfo];
}

/*
void didReceiveRemoteNotificationFetchCompletionHandler(id self, SEL _cmd, UIApplication *application, NSDictionary *userInfo, void(^)(UIBackgroundFetchResult)handler)
{

}
 */

最佳答案

block 只是 Objective-C 对象,因此您可以简单地使用 id 作为 block 的类型:

void didReceiveRemoteNotificationFetchCompletionHandler(id self, SEL _cmd, UIApplication *application, NSDictionary *userInfo, id handler)
{

}

或者你可以为你的 block 定义你自己的类型,并使用它:

typedef void (^YourBlockType)(UIBackgroundFetchResult);

void didReceiveRemoteNotificationFetchCompletionHandler(id self, SEL _cmd, UIApplication *application, NSDictionary *userInfo, YourBlockType handler)
{

}

关于ios - 如何覆盖包含 block 的objC中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32972630/

相关文章:

objective-c - 我如何制作一个简单的基于 View 的 NSOutlineView?

ios - 我怎么知道还没有提供接收推送通知?

ios - 选择一个后从通知中心清除推送通知

ios - UIButton 标题在 View 出现之前未设置

ios - 如何缩放图形,使用 CGContextRef 创建

ios - 如何在将数据传递给 viewController 时实例化 navigationController?

javascript - 如何转义 JSON 对象以提供给 JavaScript 函数?

ios - UIButton 重叠并意外隐藏在 UITableViewCell 中

iphone - 在 Objective-C 中初始化为类变量时,在哪种内存上分配 C 结构

javascript - 即使状态为 200,WNS 推送通知也不起作用