Shell: Keep app running after closing lid - swift

I need to run pmset disable sleep 1 in a Swift app to keep the app running for a few minutes when the lid is closed. However, as we know, pmset requires to be run as root. I've looked into privileged helpers but the documentation on that is so poor that I've almost given up on that approach.
I know you can leverage AuthServices but that seems to be long-depreciated. Does anyone know if any alternatives to pmset disable sleep 1 that can keep the app running after the lid is closed?
Someone suggested this has been answered here. However, that only targets the instance when the Mac's lid is open. When the lid is closed, the Mac still goes to sleep. I want to target that instance, so the Mac doesn't go to sleep when the lid is closed.

Related

Detect sleep date in background on MacOS

I have a problem about detecting sleep date and saving it. The thing is I want to run a counter, when you open the app it always count how much time passed and based on that calculates something. The thing is I want to stop counting if the computer is going to sleep. Is there any way to do this in background if the actual desktop app is not running?
I have tried NSWorkspace.willSleepNotification, but its not called if the app is not running, I also tried to do this in a menu bar app if its only an Agent its also not called, maybe its not possible to do.
You need to improve your question by showing us some code, so that we can help you with what you are doing wrong. I have a background app without a menu bar and I do get these notifications. And yes, you will ONLY get this notification if your app is running. What I usually do I create a background-only app to register those notifications, which I will pass to the main app, via a file or an Apple Event.

iOS app to run script

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.
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
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:
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].
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
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
Thanks for your help
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.
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 your script takes 20 seconds to run, that's too long, and iOS will kill the app.
You could build 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.
What you probably want is a non-graphical daemon to 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.
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).
Then, you'll need to put your script at (this exact path 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:
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).
Note: for those who are coders, 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;
}

logs on second run of an ios application in the simulator

I have a question which seems like it should be a fairly common occurrence. I am trying to test code which only runs the first time an ios application is installed. While in principle this is very easy, I'm having trouble with it behaving properly the second time the app runs (note, this is not crash related).
I have been force running the app for a second time using the "Application does not run in background" flag in the plist, and then just hitting the home button. This works great, except i stop getting log data after the first run.
Does anyone know how to test for this situation in a way where you can continue seeing the log information after the app closes? For example, if there is a way to attach the xcode logger to the new process id? Or just another way to force an app reboot without losing the logging feature?
Thanks in advance!
Use the debugger menu:
Product>Attach To Process
(Results may vary with different versions of Xcode).
Also, you might need to insert a sleep at the very start of your launch to catch the very early log messages.
EDIT: If you're on a device then you can use the Device Organizer (cmd+shift+2) to watch all logs on a device.

Rejected App, Watchdog timer or missing entitlement

So I have an app that has twice been rejected from the app store and I really could use some help!
It has been rejected for the Watchdog timer forcing it to quit when it is first opened. However I have not been able to reproduce this on any of my devices. It does take a long time to build to a device when using xcode (i know that when built from xcode the watchdog is disconnected). But I have a couple questions that might help me fix this.
Does the watchdog take into account only didFinishLaunchingWithOptions: or are their other methods that it calls?
Does the number of frameworks used by the program have anything to do with it? and if so is 11 frameworks just too many?
Once the app is installed to a device, and I unplug said device is the watchdog timer re-enabled?
What are some other things that can add to the watchdog timer?
Apple said that another possibility could be Another possibility could be a missing entitlement. Could someone please explain what this means, I hav'nt seen this before with my other apps but thought maybe it was something new? And does it have to be installed on all apps for the appstore?
Time Profile for first seconds
You are blocking the UI thread for too long. To solve this problem, first you'll need to figure out what code took that long. It might be worthwhile to profile your app. Note that the simulator doesn't emulate the device, it only emulates the API. It could run much faster than the actual device because your computer is more powerful than an iPhone. Try click and hold Run at top left of Xcode, and choose Profile. Choose Time Profiler and run for a few seconds. Stop the profiler to analyze timing in function calls. Note that you may need to dig deeper, find some tutorials or books to really understand profiling.
When you know what parts have been running slow. You can put them into a thread. The easy way would be to use Grand Central Dispatch. To get started, visit this tutorial. You can skip the first half and focus on the actual thread blocks. The APIs are dispatch_queue_create, dispatch_async and dispatch_release.
This will unblock your UI.
Do you do a lot of processing in
application:didFinishLaunchingWithOptions,
applicationDidBecomeActive,
applicationDidEnterBackground,
applicationWillResignActive or
applicationWillTerminate?
Things like multiple file access, loading/saving data, synchronous network access can cause your application to freeze on startup or when the user exits, and could cause the watchdog to kill your app.
This Apple Technical note suggests that the watchdog is only looking at the launch, suspend, resume and terminate user interface events, so I would say you should look into these first, and maybe post back some more details of what your app does in the methods listed above.
http://developer.apple.com/library/ios/#qa/qa1693/_index.html
11 frameworks is fine. I have used 11 in an app no problem.
If you start the app up by tapping the icon on the home screen then the watchdog timers will be active and monitoring your app. If you start the app up by hitting run or debug in Xcode, then the watchdog will not be monitoring your app.

Kill screensaver and unlock a mac using appleScript

I'm trying to unlock my mac using my iPhone via bluetooth proximity, the code I'm using:
tell application "System Events"
tell security preferences
set require password to wake to false
end tell
end tell
tell application "ScreenSaverEngine" to quit
It kills the screensaver, doesn't show the password box but all I get is a black screen. All applications still running but I'm hovering the mouse on this black screen.
I'm running two monitors on a 10.6.7 build.
What's wrong? Thanks.
lifehacker article http://lifehacker.com/5816791/use-a-bluetooth-phone-or-device-to-lock-and-unlock-your-mac-when-youre-near
You could try an alternative way to kill the screen saver, such as a shell script with
#!/bin/sh
killall ScreenSaverEngine
And calling it directly, or writing an applescript to call it if you'd like that for some reason to do with the lifehacker article.