- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一个钩子(Hook),用于记录系统中的所有 Darwin 通知。我 Hook 到以下功能:
CFNotificationCenterPostNotification
CFNotificationCenterPostNotificationWithOptions
NSNotificationCenter::postNotification
NSNotificationCenter::postNotificationName
我看到了很多日志。例如,当我解锁屏幕时,它会显示 SBDeviceLockStateChangedNotification。
但我期待像“com.apple.springboard.lockcomplete”这样的事件或像here这样的其他事件
不确定为什么我无法捕获类似 Darwin 的通知。任何帮助表示赞赏。这里是审查的代码
#include <notify.h>
#include <substrate.h>
#include <sqlite3.h>
#include <string.h>
#import <CoreFoundation/CFNotificationCenter.h>
//#include "CPDistributedMessagingCenter.h"
#import <CoreFoundation/CoreFoundation.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
//#import <SpringBoard/SpringBoard.h>
// init CFNotificationCenterPostNotification hook
void (*orig_CFNotificationCenterPostNotification) (
CFNotificationCenterRef center,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo,
Boolean deliverImmediately
);
void replaced_CFNotificationCenterPostNotification (
CFNotificationCenterRef center,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo,
Boolean deliverImmediately
){
NSLog(@"CFNotificationCenterPostNotification: %@", name );
orig_CFNotificationCenterPostNotification(center,name,object,userInfo,deliverImmediately);
}
void (*orig_CFNotificationCenterPostNotificationWithOptions) (
CFNotificationCenterRef center,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo,
CFOptionFlags options
);
void replaced_CFNotificationCenterPostNotificationWithOptions (
CFNotificationCenterRef center,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo,
CFOptionFlags options
)
{
NSLog(@"CFNotificationCenterPostNotificationWithOptions: %@", name );
orig_CFNotificationCenterPostNotificationWithOptions(center,name,object,userInfo,options);
}
%hook SpringBoard
-(void)applicationDidFinishLaunching:(id)application {
%orig;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome"
message:@"Welcome to my iPhone!"
delegate:nil
cancelButtonTitle:@"Thanks"
otherButtonTitles:nil];
[alert show];
//[alert release];
}
%end
%hook NSNotificationCenter
- (void)postNotification:(NSNotification *)notification{
NSLog(@"NSNotificationCenterpostNotification: %@",[notification name]);
%orig;
}
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo{
NSLog(@"NSNotificationCenterpostNotificationName: %@",aName);
%orig;
}
%end
__attribute__((constructor)) void notificationinit() {
%init;
MSHookFunction(CFNotificationCenterPostNotification, replaced_CFNotificationCenterPostNotification, &orig_CFNotificationCenterPostNotification);
MSHookFunction(CFNotificationCenterPostNotificationWithOptions, replaced_CFNotificationCenterPostNotificationWithOptions, &orig_CFNotificationCenterPostNotificationWithOptions);
}
最佳答案
所以,一些想法:
首先,您能解释一下为什么您要使用方法调配或 Hook 来检测这些通知吗?您是否试图真正拦截通知,在您的软件中处理它们,并阻止将它们传送到系统的其余部分?如果那是你想要的,那么我理解你的方法。如果您只是想接收这些通知,那么我建议您使用常规方法,即只注册 Darwin 事件(所有事件,或按名称)。 See here for an example .
这只是 com.apple.springboard.lockcomplete
事件的问题吗?因为当您解锁 设备时不会生成该事件。它仅在锁定 时发布。要检测解锁事件,您可以 see this answer .
如果您仍然遇到问题,这可能是因为事件可以通过多种方式发布。您可能正在连接 CFNotificationCenterPostNotification()
和 CFNotificationCenterPostNotificationWithOptions()
,但它们并不是发布事件的唯一方式。低级notify_post() API也可以使用。我的猜测是 CFNotificationCenterPostNotification()
实际上会使用 notify_post()
调用来发布事件。因此,如果您尝试检测事件,尤其是未记录的,iOS 可能会直接使用 notify_post()
发布事件,这可以解释原因你没有看到它。
希望这对您有所帮助。
关于ios - 为所有 Darwin 通知 Hook CFNotificationCenter - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17701228/
截至Apple Docs它的签名和详细信息如下: CFNotificationCallback 在发布通知时为通知的每个观察者调用的回调函数。 声明 typealias CFNotificationC
我对此还是很陌生,但借助示例,我的学习速度非常快。我目前正在研究将通知从一个正在运行的程序发布到另一个程序,而 CFNotificationCenter 是前进的方向。唯一的问题是,我无法使用它,而且
我有一个 View Controller ,一旦它的父 View Controller 从 View 层次结构中删除并被释放,它就永远不会被释放。它在内存图中的每个实例看起来都一样,因为它有一个对 C
我正在编写一个钩子(Hook),用于记录系统中的所有 Darwin 通知。我 Hook 到以下功能: CFNotificationCenterPostNotification CFNotificati
我是一名优秀的程序员,十分优秀!