Question

Does anyone has some tips on creating a console tool in Xcode for iOS. I want to run it via launchd and it should send request to a server.

But I can´t find any way to compile a "application" without UI in Xcode for ARM.

Thx

Was it helpful?

Solution

I agree with the other answer, that installing iOSOpenDev is usually the right way to solve this problem. Once it's installed, it adds new templates to Xcode. You can then add a new target to your project with File -> New -> Target.... Choose from an iOSOpenDev template, the one named Command-line Tool.

However, if this isn't an option, or you want to know how to do it another way, it's not too difficult.

Simply create a new Xcode Project. For the project type, start with the simplest one ... probably an iOS -> Application -> Single View Application.

Once the project is created, simply delete any View, ViewController, or .xib files, as you won't use them. In your list of Frameworks (Project Settings -> Build Phases), you can delete UIKit.framework, or anything else a non-graphical tool doesn't need.

Then, simply go into the generated main.m file, and remove its call to UIApplicationMain(). Instead, I usually create a main daemon class, and then start it with something like this, from main.m:

#import "HelloDaemon.h"

int main(int argc, char *argv[]) {
   @autoreleasepool {
      HelloDaemon* daemon = [[HelloDaemon alloc] init];
      
      // start a timer so that the process does not exit.
      NSTimer* timer = [[NSTimer alloc] initWithFireDate: [NSDate date]
                                                interval: 1.0
                                                  target: daemon
                                                selector: @selector(run:)
                                                userInfo: nil
                                                 repeats: NO];
      
      NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
      [runLoop addTimer: timer forMode: NSDefaultRunLoopMode];
      [runLoop run];
   }
   
   return 0;
}

where my daemon class contains a run: method:

-(void) run:(NSTimer *) timer;

When you build this "app" for an iOS Device (not the simulator!), it will stick it in a build output directory, such as:

./Build/Products/Debug-iphoneos/HelloDaemon.app/HelloDaemon

The executable is the HelloDaemon file under the .app directory. Copy that to your iPhone, and use it from the command-line, as a launch daemon, or however else you'd like.

References

Chris Alvares' old, but good tutorial on launch daemons

OTHER TIPS

iOSOpenDev Provides exactly what you are described.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top