我的问题是我想在 SpringBoard、Safari、Settings、Notes 等应用程序中使用一个对象。我使用的是单例模式。但是当我转储这些对象时,它们有自己的分配。因此,我认为它们是不同的对象。有没有办法让对象在应用程序中保持活动状态?我有root权限。
这是我在 HomeScreen (SpringBoard)、Safari 和设置上的日志:
Oct 20 17:05:5 MyPhone SpringBoard[982] <Warning>: MyClass: <MyClass: 0x17f72680>
Oct 20 17:06:29 MyPhone MobileSafari[1001] <Warning>: MyClass: <MyClass: 0x15da9b60>
Oct 20 17:06:34 MyPhone Preferences[1002] <Warning>: MyClass: <MyClass: 0x175864c0>
我的类.h
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
+ (instancetype)sharedInstance;
- (void)doSomething;
@end
我的班级.m
#import "MyClass.h"
@implementation MyClass
- (instancetype)init {
self = [super init];
return self;
}
+ (instancetype)sharedInstance {
static dispatch_once_t p = 0;
__strong static id _sharedSelf = nil;
dispatch_once(&p, ^{
_sharedSelf = [[self alloc] init];
});
return _sharedSelf;
}
- (void)doSomething {
NSLog(@"MyClass: %@", self);
}
@end
调整.xm
#import <SpringBoard/SpringBoard.h>
#import <MyClass.h>
%hook SpringBoard
- (void)applicationDidFinishLaunching:(UIApplication *)application {
%orig;
MyClass *myClass = [MyClass sharedInstance];
[myClass doSomething];
}
%end
%hook UIViewController
- (void)viewDidLoad {
%orig;
MyClass *myClass = [MyClass sharedInstance];
[myClass doSomething];
}
%end