views:

844

answers:

6

Hi there.

I am creating a project in which i have to change the main.m file, so that UIApplication doesnt appear straight away, so i deleted the following line from main.m

int retVal = UIApplicationMain(argc, argv, nil, nil);

and deleted these lines from AppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
    [window makeKeyAndVisible];
}

And i have added some lines of mine. Now UIWindow doesnt appear by default, and rightly so. But now after my code is executed, i want to create a Window and display some message.

How to create a UIWindow when there is no UIApplication in main.m?

+1  A: 

Most likely, not starting UIKit is not the solution you are looking for. Try just not showing any UI (don't return from applicationDidFinishLaunching:) and do what you need to do in there (or in something called from that). Or, you could just show a nice loading screen with a UIActivityIndicatorView.

Also, note that if your application has not fully launched within 20 seconds of startup (showing some sort of UI and responding to events), SpringBoard or the OS will automatically quit your application. In addition, users don't like to wait :).

Edit: Since you are not making a UIKit app, stop dreaming of being able to start UIKit in the middle: you can't. This requires a separate component hooking SpringBoard to accomplish.

chpwn
Thanks for the reply.Infact i am creating a LaunchDaemon. So i was thinking if i create a UIApplication, it will take lots of memory as it is running in the background all the time, so i wrote my own main.m.Any suggestion on how to get it going?
raziiq
You can't create a background daemon as an application on iPhone OS. You'd need to jailbreak the phone.
Nimrod
And thats completely fine with me, but if you are then you shouldn't be doing ANY UIKit at ALL.
chpwn
Ofcourse when i am talking about Daemons, i am developing for Jailbroken iPhone.
raziiq
A: 

BTW i have got this code in main.m

#import <UIKit/UIKit.h>
#import "FirstAppDelegate.h"

int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    FirstAppDelegate *obj = [[FirstAppDelegate alloc] init];

    NSDate *now = [[NSDate alloc] init];
    NSTimer *timer;
    timer= [[NSTimer alloc] initWithFireDate:now
        interval:30 target:obj selector:@selector(startIt:) userInfo:nil
        repeats:YES];

    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
    [runLoop run];


    [pool release];

    return 0;
}

This is my AppDelegate.h

#import <UIKit/UIKit.h>

@interface FirstAppDelegate : NSObject 
{

}

-(void) startIt:(NSTimer *) timer;
@end

And the AppDelegate.m

import "FirstAppDelegate.h"

import

@implementation FirstAppDelegate //@synthesize window;

-(void) startIt:(NSTimer *) timer

{
    //My Counter Code

      // If a condition is true, this is where i want to call a UIWindow, but dont know how??

}

- (void)dealloc 
{
    [super dealloc];
}

Any Suggestions on this?

raziiq
A: 

WHy are you trying to open a window from a daemon? It seems like a super-bad idea, and as you note hard to actually do.

Instead, create an app to do what you want to do and launch it from the daemon when you want to show a window - either via URL handling or some other means. But basically that app and your daemon can communicate once it's up and running, and it can do all the UI stuff away from the domain of the daemon.

Kendall Helmstetter Gelner
Or hook SpringBoard.
chpwn
Any Example on how to Hook with Springboard?
raziiq
A: 

ya thats what i am upto now after researching.

BTW is it possible to Launch an App from Daemon? If yes How?

There is one more idea i have got is to make ShellScript a Daemon and then launch my app from ShellScript, but still dont know how to launch an app from ShellScript.

Anybody on this?

raziiq
+1  A: 

If you want to launch an app from a daemon, use SBSLaunchApplicationWithIdentifier.

BOOL SBSLaunchApplicationWithIdentifier(NSString* identifier,
                                        BOOL      please_pass_NO_here);

If you want to launch a URL, use -[UIApplication openURL:] or the lower-level GSEventSendApplicationOpenURL.

mach_port_t GSGetPurpleSystemEventPort();
void GSEventSendApplicationOpenURL(CFURLRef url, mach_port_t port);

If you simply want to display an alert, use CFUserNotification. (Yes it works on iPhoneOS.)

http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFUserNotificationRef/Reference/reference.html

KennyTM
Waoo thats something really encouraging. I ll give it a try for sure. One thing i wanna ask here is that, infact i have written my LaunchDaemon as a bash Script which works fine if i simply log a file with some input, but whenever i try to open an app using "/Applications/Appname.app/AppName" , nothing happens. Is there anyway i can open an app using Bash Script in iPhoneOS?
raziiq
You need to pass the identifier e.g. @"com.yourcompany.appname".
KennyTM
Thanks for the reply. Yes i know i have to pass the identifier to the class you told me earlier, i ll surely give it a try, but in this case i was asking as if there is anyway in bash Scripting to open an app using some bash scripting command?
raziiq
No. (You can find the bundle ID from the Info.plist in that path, though.)
KennyTM
ya thanks, i can try that.BTW do you know any example using above stuff you mentioned?
raziiq
Use `NSBundle` to getting the bundle ID, just google for how to do it; how to call `SBSLaunchApplicationWithIdentifier` is trivial from the function prototype.
KennyTM
All right. So in a nutshell you are saying to create an app in xcode, where i use the Same UIApplication logic(Same as using WindowBased Template) and then use the SBSLaunchApplicationWithIdentifier to launch the app or in my case my App. So basically i ll have two apps, one as LaunchDaemon and one that LaunchDaemon will launch, right?
raziiq
Right. `SBSLaunchApplicationWithIdentifier` can be called from anything, but the launched app must be a UIKit-based app.
KennyTM
hmm Alright. Means even if i write my own main, as i just wrote in my 1st post, i can access SBSLaunchApplicationWithIdentifier? If this is the case, then its not gonna be that tough.
raziiq
Yes. But remember to add the entitlement `com.apple.springboard.launchapplications` to your console app otherwise you can't use `SBSLaunchApplicationWithIdentifier`.
KennyTM
What do you mean by this "But remember to add the entitlement com.apple.springboard.launchapplications to your console app otherwise you can't use SBSLaunchApplicationWithIdentifier"How to do this? Sorry for that much questions, please bear with me.
raziiq
http://www.telesphoreo.org/pipermail/iphone-python/2008-September/000179.html (think "python" there as your console app which is going to launch an app.)
KennyTM
Thank you so much for your help. I am currently writing my app and i ll post my feedback. Hoping for your kind help in Future.
raziiq
A: 

i achieved what i was trying to do, thanks to KennyTM for his great advices.

This is what i did

1) i created my app in which i did all the stuff for my app and then wrote all those settings to a text file and made my app create and place that txt file to /private/var/mobile/SomeFile.txt (This is the place where as a mobile user, your app can write without messing with permissions)

2) Then i created another app in xcode, (Window Based), deleted delegate (h/m) files and wrote my own main function, in which i read from the file my other app create and wrote at in the 1st step (/private/var/mobile/SomeFile.txt).

3) I created a plist (You can find help on creating a LaunchDaemon here http://www.tuaw.com/2008/02/21/tuaw-responds-iphone-lojack/)

4) I made that plist to read my app in step 2 every 60 sec(1 min) and if condition is true, the app in the 2nd step will display an Alert using CFUserNotificationDisplayAlert (thanks to KennyTM for his guidance).

The only problem i am currently having is that i have to place that launchDaemon in /Library/LaunchDaemons directory manually but SSH using root, because i cant / my app cant write to that directory.

raziiq