m (→Late hooking: Subdivided to link directly to examples.) |
(→UI usage: Expand example with alert implementations.) |
||
Line 58: | Line 58: | ||
==== Using callbacks ==== | ==== Using callbacks ==== | ||
<source lang="objc"> | <source lang="objc"> | ||
static void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { | static void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { | ||
// UI is available, use UIKit here | // UI is available, use UIKit here | ||
#if BEFORE_IOS_8 | |||
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" | |||
message:@"This is an alert." | |||
delegate:nil | |||
cancelButtonTitle:@"I'm ok with this" | |||
otherButtonTitles:nil]; | |||
[alert show]; | |||
#else | |||
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Alert" | |||
message:@"This is an alert." | |||
preferredStyle:UIAlertControllerStyleAlert]; | |||
[alert addAction:[UIAlertAction actionWithTitle:@"I'm ok with this" | |||
style:UIAlertActionStyleDefault | |||
handler:^(UIAlertAction * action) {}]]; | |||
[[UIWindow keyWindow].rootViewController presentViewController:alert animated:YES completion:^{}]; | |||
#endif | |||
} | } | ||
static void * observer = NULL; | |||
%ctor { | %ctor { | ||
CFNotificationCenterAddObserver( | CFNotificationCenterAddObserver( | ||
CFNotificationCenterGetLocalCenter(), | CFNotificationCenterGetLocalCenter(), | ||
&observer, | |||
notificationCallback, | notificationCallback, | ||
(CFStringRef)UIApplicationDidFinishLaunchingNotification, | (CFStringRef)UIApplicationDidFinishLaunchingNotification, | ||
NULL, CFNotificationSuspensionBehaviorCoalesce); | NULL, | ||
CFNotificationSuspensionBehaviorCoalesce | |||
); | |||
} | |||
// Remove observer upon unloading the dylib | |||
%dtor { | |||
CFNotificationCenterRemoveObserver( | |||
CFNotificationCenterGetLocalCenter(), | |||
&observer, | |||
(CFStringRef)UIApplicationDidFinishLaunchingNotification, | |||
NULL | |||
); | |||
} | } | ||
</source> | </source> | ||
==== Using blocks ==== | ==== Using blocks ==== | ||
<source lang="objc"> | <source lang="objc"> | ||
static id observer; | static id observer; | ||
Line 80: | Line 115: | ||
usingBlock:^(NSNotification *notification) { | usingBlock:^(NSNotification *notification) { | ||
// UI is available, use UIKit here | // UI is available, use UIKit here | ||
#if BEFORE_IOS_8 | |||
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" | |||
message:@"This is an alert." | |||
delegate:nil | |||
cancelButtonTitle:@"I'm ok with this" | |||
otherButtonTitles:nil]; | |||
[alert show]; | |||
#else | |||
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Alert" | |||
message:@"This is an alert." | |||
preferredStyle:UIAlertControllerStyleAlert]; | |||
[alert addAction:[UIAlertAction actionWithTitle:@"I'm ok with this" | |||
style:UIAlertActionStyleDefault | |||
handler:^(UIAlertAction * action) {}]]; | |||
[[UIWindow keyWindow].rootViewController presentViewController:alert animated:YES completion:^{}]; | |||
#endif | |||
} | } | ||
]; | ]; | ||
} | } | ||
// | // Remove observer upon unloading the dylib | ||
%dtor { | %dtor { | ||
[[NSNotificationCenter defaultCenter] removeObserver:observer]; | [[NSNotificationCenter defaultCenter] removeObserver:observer]; |
Revision as of 05:00, 30 January 2018
Find me on saurik's IRC server, Twitter, Reddit, Github, StackOverflow or send me an email.
Made UnlockEvents and FlipNC.
If you are looking for notifications within a process, give NotificationExplorer a look.
And a repository there: uroboro.github.io/repo.
DISCLAIMER: All code written in this page is to be considered experimental and untested. The author shall not be held responsible or liable for any undesired consequences, including, but not limited to, data loss, property damage, time travel, the singularity, suffered by you as a result of any error in such code. You also agree that you will not use these code snippets for any purposes prohibited by United States law, including, without limitation, the development, design, manufacture, or production of nuclear, missile, or chemical or biological weapons.
"Interesting" links
Hooking everything
While previously it was possible to provide no filter or an empty one, this is no longer the case, both by Cydia Substrate requiring one and not it being empty. So the solution is based on another requirement that is not very documented: Cydia Substrate will only hook binaries that are linked with Security.framework. The following filter will suffice for this purpose but please reduce the hooking target of your tweak as much as possible.
|
|
Late hooking
UI usage
If you need to use UI elements, you have to wait until the application is ready.
Using callbacks
static void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
// UI is available, use UIKit here
#if BEFORE_IOS_8
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"This is an alert."
delegate:nil
cancelButtonTitle:@"I'm ok with this"
otherButtonTitles:nil];
[alert show];
#else
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"I'm ok with this"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}]];
[[UIWindow keyWindow].rootViewController presentViewController:alert animated:YES completion:^{}];
#endif
}
static void * observer = NULL;
%ctor {
CFNotificationCenterAddObserver(
CFNotificationCenterGetLocalCenter(),
&observer,
notificationCallback,
(CFStringRef)UIApplicationDidFinishLaunchingNotification,
NULL,
CFNotificationSuspensionBehaviorCoalesce
);
}
// Remove observer upon unloading the dylib
%dtor {
CFNotificationCenterRemoveObserver(
CFNotificationCenterGetLocalCenter(),
&observer,
(CFStringRef)UIApplicationDidFinishLaunchingNotification,
NULL
);
}
Using blocks
static id observer;
%ctor {
observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification
object:nil queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *notification) {
// UI is available, use UIKit here
#if BEFORE_IOS_8
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"This is an alert."
delegate:nil
cancelButtonTitle:@"I'm ok with this"
otherButtonTitles:nil];
[alert show];
#else
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"I'm ok with this"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}]];
[[UIWindow keyWindow].rootViewController presentViewController:alert animated:YES completion:^{}];
#endif
}
];
}
// Remove observer upon unloading the dylib
%dtor {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
Dynamic bundle loading
Same applies if a class you want to hook is dynamically loaded by a bundle.
To do: read this and make this procedure more robust.
Using callbacks
static void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
if ([((NSDictionary *)userInfo)[NSLoadedClasses] containsObject:@"targetClass"]) {
// Target class has been loaded
}
}
%ctor {
CFNotificationCenterAddObserver(
CFNotificationCenterGetLocalCenter(), NULL,
notificationCallback,
(CFStringRef)NSBundleDidLoadNotification,
NULL, CFNotificationSuspensionBehaviorCoalesce);
}
Using blocks
static id observer;
%ctor {
observer = [[NSNotificationCenter defaultCenter] addObserverForName:NSBundleDidLoadNotification
object:nil queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *notification) {
if ([notification.userInfo[NSLoadedClasses] containsObject:@"targetClass"]) {
// Target class has been loaded
}
}
];
}
//if a destructor existed or you don't need to track the notification anymore
%dtor {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
Collaboration and cross-platform compilation
When doing group projects you might find that each contributor has a different platform and possibly a different Theos installation path. To prevent make errors caused by the theos symlink, some may consider blacklisting it from the revision control software (git or svn). It doesn't matter if you do or not, the threat of a missing or incorrect symlink still exists.
Having worked with other developers with an Apple computer, while myself working with my iPhone5,1, I found myself "fighting" this annoyance. After a couple iterations I've made this snippet to fix this "problem":
__SYMLINK := /$(if $(filter $(shell uname -s)-$(shell uname -p),Darwin-arm Darwin-arm64),var,opt)/theos
T := $(shell [[ "$(__SYMLINK)" != `readlink theos` ]] && { rm -f theos; ln -s $(__SYMLINK) theos; })
To use it, place it anywhere before all includes, be it at the top of the Makefile or just before include theos/makefiles/common.mk
.
Alternatively, add this instead, only in the main Makefile:
T := $(shell sh symlinkfixer.sh)
And save this as symlinkfixer.sh:
find . -name Makefile -exec dirname {} \; | sed 's_$_/theos_' | xargs -I SL bash -c 'platform=$(uname -s)-$(uname -p); _THEOS=/`[[ $platform == Darwin-arm || $platform == Darwin-arm64 ]] && echo var || echo opt`/theos; [[ $(readlink SL) != $_THEOS ]] && { rm -f SL; ln -s $_THEOS SL; }'
Other Stuff
Similar Wanted Pages:
Test Zone
Wondering what looks better:
To "wikitable"...
NeXTSTEP style | XML style |
---|---|
{
entry = {
cell = PSSwitchCell;
defaults = "com.your.tweak";
label = "Your Tweak";
key = enabled;
default = 1;
icon = "/Applications/Preferences.app/[email protected]";
PostNotification = "com.your.tweak/preferences.changed";
};
}
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>entry</key>
<dict>
<key>cell</key>
<string>PSSwitchCell</string>
<key>defaults</key>
<string>com.your.tweak</string>
<key>label</key>
<string>Your Tweak</string>
<key>key</key>
<string>enabled</string>
<key>default</key>
<true/>
<key>icon</key>
<string>/Applications/Preferences.app/[email protected]</string>
<key>PostNotification</key>
<string>com.your.tweak/preferences.changed</string>
</dict>
</dict>
</plist>
|
or not to "wikitable"...
NeXTSTEP style | XML style |
---|---|
{
entry = {
cell = PSSwitchCell;
defaults = "com.your.tweak";
label = "Your Tweak";
key = enabled;
default = 1;
icon = "/Applications/Preferences.app/[email protected]";
PostNotification = "com.your.tweak/preferences.changed";
};
}
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>entry</key>
<dict>
<key>cell</key>
<string>PSSwitchCell</string>
<key>defaults</key>
<string>com.your.tweak</string>
<key>label</key>
<string>Your Tweak</string>
<key>key</key>
<string>enabled</string>
<key>default</key>
<true/>
<key>icon</key>
<string>/Applications/Preferences.app/[email protected]</string>
<key>PostNotification</key>
<string>com.your.tweak/preferences.changed</string>
</dict>
</dict>
</plist>
|
Maybe collapsing?
|
|