1

iOS 13 has a new way of sending app lifecycle events:

@implementation SceneDelegate


- (void)sceneDidDisconnect:(UIScene *)scene {
}


- (void)sceneDidBecomeActive:(UIScene *)scene {
}


- (void)sceneWillResignActive:(UIScene *)scene {
}


- (void)sceneWillEnterForeground:(UIScene *)scene {
}


- (void)sceneDidEnterBackground:(UIScene *)scene {

}


@end

SWIZZLE code:

@implementation UIScene (SWIZZLE)

- (void)my_setDelegate:(id <UISceneDelegate>)delegate {
    // do custom work
    [self my_setDelegate:delegate];
}

+ (void)load {
    [self swizzle:@selector(setDelegate:) with:@selector(my_setDelegate:)];
}

@end

typedef IMP *IMPPointer;

BOOL class_swizzleMethodAndStore(Class class, SEL original, IMP replacement, IMPPointer store) {
    IMP imp = NULL;
    Method method = class_getInstanceMethod(class, original);
    if (method) {
        const char *type = method_getTypeEncoding(method);
        imp = class_replaceMethod(class, original, replacement, type);
        if (!imp) {
            imp = method_getImplementation(method);
        }
    }
    if (imp && store) { *store = imp; }
    return (imp != NULL);
}

@implementation NSObject (RuntimeAdditions)
+ (BOOL)swizzle:(SEL)original with:(IMP)replacement store:(IMPPointer)store {
    return class_swizzleMethodAndStore(self, original, replacement, store);
}
@end

Document:

#pragma mark - Delegate
// UIScene is strongly retained by UIKit like UIApplication, however, unlike UIApplication, the delegate may not need to live for the whole lifetime of the process.
// A strong ref here relieves clients of the responsibility of managing the delegate lifetime directly.
@property (nullable, nonatomic, strong) id<UISceneDelegate> delegate;

that is no't work ,how to do swizzle UIWindowScene delegate ?

iHTCboy
  • 2,715
  • 21
  • 20

0 Answers0