2

我正在使用Logos構建MobileSubstrate調整,並且正在嘗試添加新方法以將設備鎖定到設備上的每個應用程序中,在接近度更改通知後運行。到目前爲止,我的代碼是使用MobileSubstrate掛鉤應用程序委託爲所有應用程序添加新方法

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
#import <SpringBoard/SpringBoard.h> 
#import <SpringBoard/UIApplicationDelegate.h> 
#import <GraphicsServices/GSEvent.h> 
#include <notify.h> 

@interface suspendresume : NSObject 

@property(nonatomic, readonly) BOOL proximityState; 

@end 

@implementation suspendresume 

BOOL tweakOn; 

@end 

static NSString *settingsFile = @"/var/mobile/Library/Preferences/com.matchstick.suspendresume.plist"; 

%hook SpringBoard 

-(void)applicationDidFinishLaunching:(id)application { 
    // Allow SpringBoard to initialise 
    %orig; 

    // Set up proximity monitoring 
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; 
    [[UIDevice currentDevice] proximityState]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChange:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil]; 
} 

%new 

// Add new code into SpringBoard 
-(void)proximityChange:(NSNotification*)notification { 
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; 

    // Check if tweak is on 
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:settingsFile]; 
    tweakOn = [[dict objectForKey:@"enabled"] boolValue]; 

    // Only run if tweak is on 
    if (tweakOn) { 

     // Get first proximity value 
     if ([[UIDevice currentDevice] proximityState] == YES) { 

      // Wait a few seconds TODO allow changing of wait interval from prefrences FIXME causes a lockup of interface whilst sleeping 
      [self performSelector:@selector(lockDeviceAfterDelay) withObject:nil afterDelay:1.0]; 
     } 
    } 
} 

%new 

-(void)lockDeviceAfterDelay { 

    // Second proximity value 
    if ([[UIDevice currentDevice] proximityState] == YES) { 

     // Lock device 
     GSEventLockDevice(); 
    } 
} 

%end 

,因爲我需要在跳板它的工作原理,但不是在設備上安裝的任何其他應用程序 - 所有這些發生在測試是在顯示屏關閉的接近傳感器被觸發時,並且不鎖定設備。

我想利用UIApplicationDelegate的 -(void)applicationDidFinishLaunching:(id)applicationUIApplication實現,因爲我有跳板應用程序相同的,但無法弄清楚如何做到這一點。

這種方法的想法來到from this project

我需要我在跳板上運行相同的代碼添加到一個新的方法/ s的UIApplication下?

是否需要爲每個應用程序重新設置接近度監視,以及在接收到接近度更改通知後如何調用這些新方法來運行?

此外,full source for this is on my GitHub

+0

我回復了它,因爲我覺得你的標題編輯並沒有完全概括這個問題是關於什麼 - 問題是關於特定的iOS協議,所以'應用程序委託'沒有完全表達這一點。非常感謝其他編輯! – 2013-03-24 11:00:08

+0

感謝您澄清 - 我沒有從這個角度看它,所以我回滾到您的編輯。 – 2013-03-25 16:11:09

回答

2

事實證明這不是這樣做的正確方法。相反,

// Get the topmost application 
SBApplication *runningApp = [(SpringBoard *)self _accessibilityFrontMostApplication]; 
// We're in application, resign app 
[runningApp notifyResignActiveForReason:1]; 

沒有辦法。