2013-07-17 23 views
3

我正在編寫一個掛鉤,它會在系統中記錄所有達爾文通知。我鉤入以下功能:爲所有達爾文通知掛鉤CFNotificationCenter - iOS

CFNotificationCenterPostNotification 
CFNotificationCenterPostNotificationWithOptions 
NSNotificationCenter::postNotification 
NSNotificationCenter::postNotificationName 

我看到很多的日誌。示例當我解鎖屏幕時,它顯示我SBDeviceLockStateChangedNotification。

但我希望喜歡的活動 - 「com.apple.springboard.lockcomplete」或其他類似事件here

不知道爲什麼我不能夠捕捉到達爾文樣的通知。任何幫助讚賞。下面是審覈

#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); 

} 

回答

3

因此,代碼,一些想法:

  1. 首先,你能解釋一下爲什麼要使用方法交叉混合,或掛鉤來檢測這些通知?您是否試圖實際攔截通知,在您的軟件中處理它們,並且停止它們被傳遞到系統的其餘部分?如果這就是你想要的,那麼我明白你的方法。如果你只是想收到這些通知,那麼我會建議只註冊達爾文事件(所有這些,或通過名稱)的正常方法。 See here for an example

  2. 這只是com.apple.springboard.lockcomplete事件的問題嗎?因爲那個事件不會在您設備解鎖時生成。只有在鎖定時纔會發佈。要檢測解鎖事件,您可以see this answer

  3. 如果您仍然遇到問題,也許這是因爲事件可以以多種方式發佈。你可能會喜歡CFNotificationCenterPostNotification()CFNotificationCenterPostNotificationWithOptions(),但它們不是發佈活動的唯一方式。低級別notify_post() API也可以使用。我的猜測是CFNotificationCenterPostNotification()實際上會使用notify_post()調用來發布事件。因此,如果您嘗試檢測某個事件,尤其是未公開的,則iOS可能會直接使用notify_post()發佈事件,這可以解釋您爲什麼沒有看到它。

希望這會有所幫助。

+0

Nate - 非常感謝您的詳細解答。 1)我只想收到每一條達爾文通知。但是,您提到的示例僅在我的應用處於前景時纔會收到通知。 (或者我正在打扮)。 2)&3)notify_post做了詭計。現在我可以掛接到notify_post並捕獲系統中的每個通知。感謝您的幫助和時間。 – Jailbroken

+0

順便說一句,目的不是阻止他們,而是記錄他們,並建立一個所有可能的通知數據庫,以便有需求時,我們可以通過主列表,並找出它是如何實現的... – Jailbroken

+1

@Bugivore,我現在不在我的Mac上,所以我無法運行。當您的應用在後臺時,您應該能夠收到通知。但是,您可能需要執行一些步驟,以便在後臺保持應用程序完全*活動狀態。對於一個普通的UIApplication,iOS會在後臺禁用相當多的功能。您可以通過將您的應用程序設置爲守護程序或通過在Info.plist中使用未記錄的背景模式('continuous'和'unboundedTaskCompletion')來解決此問題。我現在需要去睡覺,但我明天可以發佈更多信息。或者,你可以搜索我的舊帖子... – Nate