-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix BatteryLevel not available (#165)
* Fix BatteryLevel not available * Moved method
- Loading branch information
Showing
3 changed files
with
110 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// BatteryLevel.h | ||
// PlayTools | ||
// | ||
// Created by Edoardo C. on 07/08/24. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface NSObject (SwizzleBattery) | ||
|
||
- (void)swizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// BatteryLevel.m | ||
// PlayTools | ||
// | ||
// Created by Edoardo C. on 07/08/24. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <objc/runtime.h> | ||
#import <PlayTools/PlayTools-Swift.h> | ||
#import "BatteryLevel.h" | ||
|
||
__attribute__((visibility("hidden"))) | ||
@interface BatteryLevelLoader : NSObject | ||
@end | ||
|
||
@implementation NSObject (SwizzleBattery) | ||
|
||
- (void) swizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector | ||
{ | ||
Class cls = [self class]; | ||
// If current class doesn't exist selector, then get super | ||
Method originalMethod = class_getInstanceMethod(cls, origSelector); | ||
Method swizzledMethod = class_getInstanceMethod(cls, newSelector); | ||
|
||
// Add selector if it doesn't exist, implement append with method | ||
if (class_addMethod(cls, | ||
origSelector, | ||
method_getImplementation(swizzledMethod), | ||
method_getTypeEncoding(swizzledMethod)) ) { | ||
// Replace class instance method, added if selector not exist | ||
// For class cluster, it always adds new selector here | ||
class_replaceMethod(cls, | ||
newSelector, | ||
method_getImplementation(originalMethod), | ||
method_getTypeEncoding(originalMethod)); | ||
|
||
} else { | ||
// SwizzleMethod maybe belongs to super | ||
class_replaceMethod(cls, | ||
newSelector, | ||
class_replaceMethod(cls, | ||
origSelector, | ||
method_getImplementation(swizzledMethod), | ||
method_getTypeEncoding(swizzledMethod)), | ||
method_getTypeEncoding(originalMethod)); | ||
} | ||
} | ||
|
||
- (bool) pm_return_true { | ||
return true; | ||
} | ||
|
||
- (float) pm_return_battery_full { | ||
return 1.0; | ||
} | ||
|
||
- (UIDeviceBatteryState) pm_return_fullCharging { | ||
return UIDeviceBatteryStateFull; | ||
} | ||
@end | ||
|
||
@implementation BatteryLevelLoader | ||
+ (void)load { | ||
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; | ||
[objc_getClass("UIDevice") swizzleInstanceMethod:@selector(isBatteryMonitoringEnabled) withMethod:@selector(pm_return_true)]; | ||
[objc_getClass("UIDevice") swizzleInstanceMethod:@selector(batteryState) withMethod:@selector(pm_return_fullCharging)]; | ||
[objc_getClass("UIDevice") swizzleInstanceMethod:@selector(batteryLevel) withMethod:@selector(pm_return_battery_full)]; | ||
} | ||
@end |