bash 用于运行脚本的 iOS 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19431614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS app to run script
提问by Rhys
I would appreciate help in creating an iOS app to run a specified script at /var/mobile/script.sh. The script I have written opens a number of specified apps "all at once" on my jailbroken 6.1.2 iphone. Currently I run the script by starting MobileTerminal and manually running the script.sh file from the command line. I would like to be able to create an app that I could start via the Springboard which would run /var/mobile/script.sh directly.
我很感激在创建 iOS 应用程序以在 /var/mobile/script.sh 中运行指定脚本方面的帮助。我编写的脚本在我越狱的 6.1.2 iphone 上“一次性”打开了许多指定的应用程序。目前,我通过启动 MobileTerminal 并从命令行手动运行 script.sh 文件来运行脚本。我希望能够创建一个可以通过 Springboard 启动的应用程序,它可以直接运行 /var/mobile/script.sh。
I do not have a Mac and so I am unable to compile an app myself. I also do not have much experience of programming so I would appreciate any help on offer. I have seen an answer which is similar: iOS execute shell script from Activator
我没有 Mac,所以我无法自己编译应用程序。我也没有太多的编程经验,所以我很感激能提供任何帮助。我看过一个类似的答案:iOS execute shell script from Activator
The answer provided above is VERY close to what I am looking for. However I have two problems with the Script.app as it stands:
上面提供的答案非常接近我正在寻找的。但是,我对 Script.app 有两个问题:
1) As the script I have written opens a number of other apps which takes around two minutes to complete, the Script.app is automatically killed by iOS after about 20 seconds. I'm not sure if a flag "didFinishLaunchingWithOptions" needs to be set to allow Script.app to continue running so it can finish running my script? I have installed a package via Cydia called "Background Manager" - [which as I understand it makes all apps effectively think they are running in the foreground].
1) 由于我编写的脚本会打开许多其他应用程序,大约需要两分钟才能完成,因此 Script.app 会在大约 20 秒后自动被 iOS 杀死。我不确定是否需要设置标志“didFinishLaunchingWithOptions”以允许 Script.app 继续运行,以便它可以完成我的脚本的运行?我已经通过 Cydia 安装了一个名为“后台管理器”的包 - [据我所知,它使所有应用程序有效地认为它们在前台运行]。
2) I have previously tested Script.app listed above on iOS 5 and the Script.app did run for around 20 seconds bringing the apps to the foreground as listed in my script.sh. On iOS 6.1.2 the Script.app runs certain commands in script.sh [e.g. killall commands] but when it comes to opening a specified app via the script it won't open any app [there is a blank black screen for 20 seconds and then the Script app closes without having opened any apps via the script.sh file]. However in iOS 6.1.2 if Script.app is started and immediately the home button is pressed to return you to the Springboard the Script.app will open apps via my script.sh for the first 20 seconds, so there appears to be a difference between the way the Script.app runs on iOS5 vs iOS6. I would like if Script.app could open apps via the script.sh without me having to immediately press the home button after Script.app starts on iOS 6
2) 我之前在 iOS 5 上测试过上面列出的 Script.app,并且 Script.app 确实运行了大约 20 秒,将应用程序带到了我的 script.sh 中列出的前台。在 iOS 6.1.2 上,Script.app 在 script.sh 中运行某些命令 [例如 killall 命令],但是当通过脚本打开指定的应用程序时,它不会打开任何应用程序 [有 20 秒的空白黑屏然后脚本应用程序关闭,而没有通过 script.sh 文件打开任何应用程序]。但是,在 iOS 6.1.2 中,如果 Script.app 已启动并立即按下主页按钮将您返回到 Springboard,则 Script.app 将在前 20 秒内通过我的 script.sh 打开应用程序,因此似乎有所不同在 Script.app 在 iOS5 和 iOS6 上运行的方式之间。我想如果 Script.app 可以通过脚本打开应用程序。
In my script file I use a command line package called "open" to specific which app to open via its CFBundleIdentifier: http://moreinfo.thebigboss.org/moreinfo/depiction.php?file=openData
在我的脚本文件中,我使用一个名为“open”的命令行包来指定通过其 CFBundleIdentifier 打开哪个应用程序:http://moreinfo.thebigboss.org/moreinfo/depiction.php?file=openData
Thanks for your help
谢谢你的帮助
回答by Nate
Here's a technique that should work for you. It's not necessarily the way I'd solve it, but the solution is designed to work around you not having a Mac / Xcode.
这是一种应该适合您的技术。这不一定是我解决它的方式,但该解决方案旨在解决您没有 Mac / Xcode 的问题。
One problem is that iOS expects normal "apps" to startup quickly, and begin responding to normal UI application events. If you use my original script launcher, it does not actually create a UIApplication
, but simply calls system()
from the main program. Assuming the call to system()
completes quickly, there's no problem. But, if yourscript takes 20 seconds to run, that's too long, and iOS will kill the app.
一个问题是 iOS 期望正常的“应用程序”快速启动,并开始响应正常的 UI 应用程序事件。如果您使用我的原始脚本启动器,它实际上不会创建UIApplication
,而只是system()
从主程序调用。假设调用 tosystem()
很快完成,没有问题。但是,如果您的脚本需要 20 秒才能运行,那就太长了,iOS 将终止该应用程序。
You couldbuild a normal UI application, calling UIApplicationMain()
, and starting your long-running script after the app finishes startup. But really, you're not building an app. You simply want to use a SpringBoard icon to kick off a script. So, I don't think that solution makes a lot of sense.
您可以构建一个普通的 UI 应用程序,UIApplicationMain()
在应用程序完成启动后调用并启动长时间运行的脚本。但实际上,您不是在构建应用程序。您只想使用 SpringBoard 图标来启动脚本。所以,我认为这个解决方案没有多大意义。
What you probably want is a non-graphical daemonto run your long script for you. For this, you could build a Launch Daemon, and that might be how I solved the problem. But, without Xcode, that's tough. Another option is to take advantage of the privileged daemon that SBSettings has. You can use it to run your script. Then, your "app" need only fire off a notification to tell SBSettings to run that script. It can then exit, almost immediately.
您可能想要的是一个非图形守护程序来为您运行长脚本。为此,您可以构建一个Launch Daemon,这可能就是我解决问题的方法。但是,如果没有 Xcode,那就很难了。另一种选择是利用 SBSettings 拥有的特权守护进程。 您可以使用它来运行您的脚本。然后,您的“应用程序”只需要发出一个通知来告诉 SBSettings 运行该脚本。然后它几乎可以立即退出。
Here is a pre-built binary that invokes a script named com.mycompany.script(get version 1.1). Install this app on your iPhone (it can be installed normally, in the sandbox area).
这是一个预构建的二进制文件,它调用名为 com.mycompany.script(获取版本 1.1)的脚本。在你的iPhone上安装这个应用程序(它可以正常安装,在沙盒区域)。
Then, you'll need to put your script at (this exactpath and filename):
然后,您需要将脚本放在(这个确切的路径和文件名):
/var/mobile/Library/SBSettings/Commands/com.mycompany.script
For example, its contents might be:
例如,它的内容可能是:
#!/bin/sh
/usr/bin/open com.mycompany.AppOne
/usr/bin/open com.mycompany.AppTwo
/usr/bin/open com.mycompany.AppThree
exit 0;
where you're passing app bundle IDs to open
. You can put whatever you want inside it. Make sure to login to your iPhone and set the right permissions on your script:
您将应用程序包 ID 传递到open
. 你可以在里面放任何你想要的东西。确保登录到您的 iPhone 并在您的脚本上设置正确的权限:
cd /var/mobile/Library/SBSettings/Commands/
chmod 755 com.mycompany.script
chown mobile.mobile com.mycompany.script
and then reboot (re-springing might be enough to get SBSettings to recognize your new script).
然后重新启动(重新启动可能足以让 SBSettings 识别您的新脚本)。
Note: for those who arecoders, the main program of this app basically just contains this code:
注:对于那些谁是编码器,这个应用程序的主程序基本上只是包含以下代码:
#include <notify.h>
int main(int argc, char *argv[])
{
notify_post("com.mycompany.script");
return 0;
}