使用MobileSubstrate挂钩应用程序代表以向所有应用程序添加新方法

时间:2013-03-23 16:45:19

标签: ios jailbreak uiapplicationdelegate proximity cydia-substrate

我正在使用Logos构建一个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

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

我正在考虑使用UIApplicationDelegate -(void)applicationDidFinishLaunching:(id)applicationUIApplication在应用程序中实现与SpringBoard相同的功能,但无法弄清楚如何执行此操作。

这种方法的想法来自from this project

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

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

此外,full source for this is on my GitHub

1 个答案:

答案 0 :(得分:2)

原来这不是正确的方法。相反,

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

做了这个伎俩。