将App Delegate与mobilessubstrate挂钩,为所有App添加新方法



我正在使用Logos构建一个mobilessubstrate调整,并且我正在尝试添加一个新方法来将设备锁定到设备上的每个应用程序中,这将在接近更改通知后运行。到目前为止,我的代码是

#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

在SpringBoard中按我的要求工作,但在设备上安装的任何其他应用程序中都没有-测试时发生的所有事情都是在触发接近传感器时关闭显示,并且不会锁定设备。

我正在考虑使用UIApplicationDelegate的-(void)applicationDidFinishLaunching:(id)applicationUIApplication在应用程序中实现与我使用SpringBoard相同的功能,但不知道如何做到这一点。

这种方法的想法来自于这个项目

我是否需要添加相同的代码,我在SpringBoard运行到UIApplication下的新方法/s ?

我是否需要为每个应用程序重新设置接近监控,以及我如何在收到接近更改通知后调用这些新方法来运行?

同样,完整的源代码在我的GitHub上

事实证明这不是正确的方法。相反,

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

相关内容

  • 没有找到相关文章

最新更新