Skip to content

Commit 802e49a

Browse files
1. Add Respring function
2. Add when no permission disable button.
1 parent 45e12b0 commit 802e49a

5 files changed

Lines changed: 99 additions & 31 deletions

File tree

DeviceController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
@interface DeviceController : NSObject
44

55
- (BOOL) RebootDevice;
6+
- (void) Respring;
67

78
@end

DeviceController.m

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ - (BOOL) RebootDevice
3131
return NO;
3232
}
3333

34+
// @See https://github.com/opa334/TrollStore/blob/main/Shared/TSUtil.m#L297
35+
- (void) Respring
36+
{
37+
killall(@"SpringBoard", YES);
38+
exit(0);
39+
}
40+
3441
// @See https://github.com/opa334/TrollStore/blob/main/Shared/TSUtil.m#L79
3542
int spawnRoot(NSString* path, NSArray* args, NSString** stdOut, NSString** stdErr)
3643
{
@@ -182,4 +189,72 @@ int fd_is_valid(int fd)
182189
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
183190
}
184191

192+
// @See https://github.com/opa334/TrollStore/blob/main/Shared/TSUtil.m#L279
193+
void killall(NSString* processName, BOOL softly)
194+
{
195+
enumerateProcessesUsingBlock(^(pid_t pid, NSString* executablePath, BOOL* stop)
196+
{
197+
if([executablePath.lastPathComponent isEqualToString:processName])
198+
{
199+
if(softly)
200+
{
201+
kill(pid, SIGTERM);
202+
}
203+
else
204+
{
205+
kill(pid, SIGKILL);
206+
}
207+
}
208+
});
209+
}
210+
211+
void enumerateProcessesUsingBlock(void (^enumerator)(pid_t pid, NSString* executablePath, BOOL* stop))
212+
{
213+
static int maxArgumentSize = 0;
214+
if (maxArgumentSize == 0) {
215+
size_t size = sizeof(maxArgumentSize);
216+
if (sysctl((int[]){ CTL_KERN, KERN_ARGMAX }, 2, &maxArgumentSize, &size, NULL, 0) == -1) {
217+
perror("sysctl argument size");
218+
maxArgumentSize = 4096; // Default
219+
}
220+
}
221+
int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL};
222+
struct kinfo_proc *info;
223+
size_t length;
224+
int count;
225+
226+
if (sysctl(mib, 3, NULL, &length, NULL, 0) < 0)
227+
return;
228+
if (!(info = malloc(length)))
229+
return;
230+
if (sysctl(mib, 3, info, &length, NULL, 0) < 0) {
231+
free(info);
232+
return;
233+
}
234+
count = length / sizeof(struct kinfo_proc);
235+
for (int i = 0; i < count; i++) {
236+
@autoreleasepool {
237+
pid_t pid = info[i].kp_proc.p_pid;
238+
if (pid == 0) {
239+
continue;
240+
}
241+
size_t size = maxArgumentSize;
242+
char* buffer = (char *)malloc(length);
243+
if (sysctl((int[]){ CTL_KERN, KERN_PROCARGS2, pid }, 3, buffer, &size, NULL, 0) == 0) {
244+
NSString* executablePath = [NSString stringWithCString:(buffer+sizeof(int)) encoding:NSUTF8StringEncoding];
245+
246+
BOOL stop = NO;
247+
enumerator(pid, executablePath, &stop);
248+
if(stop)
249+
{
250+
free(buffer);
251+
break;
252+
}
253+
}
254+
free(buffer);
255+
}
256+
}
257+
free(info);
258+
}
259+
185260
@end

Resources/en.lproj/Localizable.strings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
"Install_With_TrollStore_text" = "Already installed via TrollStore";
33
"Need_Install_With_TrollStore_text" = "Requires TrollStore installation";
44
"Reboot_Device_text" = "Reboot Device";
5+
"Respring_text" = "Respring";
56
"Show_Alert_Before_Starting_text" = "Show alert before starting";

Resources/zh-Hans.lproj/Localizable.strings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
"Install_With_TrollStore_text" = "已经通过TrollStore安装";
33
"Need_Install_With_TrollStore_text" = "需要使用TrollStore安装";
44
"Reboot_Device_text" = "重启设备";
5+
"Respring_text" = "注销";
56
"Show_Alert_Before_Starting_text" = "开始操作前显示提醒";

RootViewController.swift

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,23 @@ class RootViewController: UIViewController {
5858
rebootButton.translatesAutoresizingMaskIntoConstraints = false
5959
// 添加点击事件
6060
rebootButton.addTarget(self, action: #selector(onClickRebootButton), for: .touchUpInside)
61-
61+
rebootButton.isEnabled = enable // 无权限的时候不允许点击
62+
63+
// Respring
64+
let respringButton = UIButton(type: .system)
65+
respringButton.setTitle(NSLocalizedString("Respring_text", comment: ""), for: .normal)
66+
respringButton.translatesAutoresizingMaskIntoConstraints = false
67+
respringButton.addTarget(self, action: #selector(onClickRespringButton), for: .touchUpInside)
68+
respringButton.isEnabled = enable // 无权限的时候不允许点击
69+
6270
// 添加设置项
63-
let showAlertSwitch = UISwitch()
64-
showAlertSwitch.translatesAutoresizingMaskIntoConstraints = false
65-
66-
let showAlertLabel = UILabel()
67-
showAlertLabel.text = NSLocalizedString("Show_Alert_Before_Starting_text", comment: "")
68-
showAlertLabel.textColor = UIColor.label
69-
showAlertLabel.translatesAutoresizingMaskIntoConstraints = false
70-
71-
// 添加子视图
72-
let showAlertSubView = UIView()
73-
showAlertSubView.translatesAutoresizingMaskIntoConstraints = false
74-
// 将开关和标签添加到子视图中
75-
showAlertSubView.addSubview(showAlertLabel)
76-
showAlertSubView.addSubview(showAlertSwitch)
77-
// TODO 暂时隐藏 后期直接整合到设置项里
78-
showAlertSubView.isHidden = true
71+
let settingButton = UIButton(type: .system)
7972

8073
// 向View中添加控件
8174
self.view.addSubview(iconImageView)
8275
self.view.addSubview(checkPermissionLabel)
8376
self.view.addSubview(rebootButton)
84-
self.view.addSubview(showAlertSubView)
77+
self.view.addSubview(respringButton)
8578

8679

8780
// AutoLayout
@@ -102,19 +95,11 @@ class RootViewController: UIViewController {
10295
rebootButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 50), // 左侧边距
10396
rebootButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50), // 右侧边距
10497

105-
showAlertSubView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
106-
showAlertSubView.topAnchor.constraint(equalTo: rebootButton.bottomAnchor, constant: 30),
107-
// 动态设置容器的宽度和高度
108-
showAlertSubView.leadingAnchor.constraint(equalTo: showAlertLabel.leadingAnchor),
109-
showAlertSubView.trailingAnchor.constraint(equalTo: showAlertSwitch.trailingAnchor),
110-
showAlertSubView.heightAnchor.constraint(equalTo: showAlertSwitch.heightAnchor),
111-
// 标签和开关在容器中水平排列
112-
showAlertLabel.leadingAnchor.constraint(equalTo: showAlertSubView.leadingAnchor),
113-
showAlertLabel.centerYAnchor.constraint(equalTo: showAlertSubView.centerYAnchor),
114-
115-
showAlertSwitch.leadingAnchor.constraint(equalTo: showAlertLabel.trailingAnchor, constant: 10),
116-
showAlertSwitch.centerYAnchor.constraint(equalTo: showAlertSubView.centerYAnchor),
117-
showAlertSwitch.trailingAnchor.constraint(equalTo: showAlertSubView.trailingAnchor)
98+
respringButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), // 水平居中
99+
respringButton.heightAnchor.constraint(equalToConstant: 50),
100+
respringButton.topAnchor.constraint(equalTo: rebootButton.bottomAnchor, constant: 20),
101+
respringButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 50), // 左侧边距
102+
respringButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50) // 右侧边距
118103
])
119104
}
120105

@@ -128,6 +113,11 @@ class RootViewController: UIViewController {
128113
let deviceController = DeviceController()
129114
deviceController.rebootDevice()
130115
}
116+
117+
@objc func onClickRespringButton() {
118+
let deviceController = DeviceController()
119+
deviceController.respring()
120+
}
131121
}
132122

133123
extension UIColor {

0 commit comments

Comments
 (0)