I have the problem is that I want to use an object across the applications such as SpringBoard, Safari, Settings, Notes, etc. I'm using Singleton pattern. But when I dump these objects, they have it own allocate. Therefore, I think they are different objects. Is there any way to keep an object alive across the applications? I have root permission.
Here's my log on HomeScreen (SpringBoard), Safari and Settings:
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>
MyClass.h
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
+ (instancetype)sharedInstance;
- (void)doSomething;
@end
MyClass.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
Tweak.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