0

Today I went to job interview, I encountered three questions now.they hope i provide some Solutions or

Thinking. 1.when user press Home button,app don't go back the main interface or delay 5 seconds to go back the

main interface. 2. when the screen is black ,how to auto activation screen(how to catch power button press event) . 3.how direct launch my app when iPhone is start-up

My english is very bad, i hope everyone can understand what i said above.Thank you very much

giorashc
  • 13,691
  • 3
  • 35
  • 71
Ryder.Cheng
  • 21
  • 1
  • 3
  • 2
    Frankly, by not immediately answering "you can't do these things", in the interview itself, you've probably already shown the interviewer you haven't done much iOS development. This is the likely reason they ask these questions. – ceejayoz Apr 19 '12 at 16:45

4 Answers4

4

None of these are possible on an iOS device, unless it's jailbroken.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
2

Q1 and Q2: not possible

Q3: The closest you can get to desired behaviour is kiosk (store demo) mode of operation.

See more here:

Lock-down iPhone/iPod/iPad so it can only run one app

It's not exactly what you're looking for - it limits the device to use only certain app - but to my knowledge the only way to auto-start an app without jailbraking the device.

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
2

I know that this question is old, but there are easy work arounds for every question on here that work for at least iOS 7+, although there is no way to do question 2 without using private APIs. You can successfully answer questions 1 and 3 with public API answers (although they are admittedly hacky)!!

1. When user presses home button, how do you delay 5 seconds before returning to the main screen?

Oscar Gomez's answer was spot on. While you cannot delay the UI from returning to the home screen without blocking the core run loop (which will get you rejected from the app store), you can use some background process techniques if needed to get your extra 5 seconds, just not with UI.

2. How do you catch the Power Button presses?

This is for sure using a private API, and while you can PROBABLY get into the App Store, you probably won't last long once Apple gets wind of you doing this. It is also a hack. You cannot listen directly to the power button, but you CAN listen to the screen going on or off, or both at once. Here is a small code snippet that I have in an Enterprise App:

Inside UIAppDelegate

static int const DisplayOnOffObserver = 54321876;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, &PLDisplayOnOff, CFSTR("com.apple.springboard.hasBlankedScreen"), NULL, 0);
    ...
    return YES;
}

static void PLDisplayOnOff(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
    ...
    DO SOME MAGIC
    ...
}

- (void)applicationWillTerminate:(UIApplication *)application {
    ...
    CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), &DisplayOnOffObserver);
    ...
}

Obviously you have to have the application ALWAYS running in the background (which can get tricky) or you have to only care to do it within the time that you ask for to run in the background (which isn't hard)

First step is listening to the darwin notification for the screen being toggled on/off. Second step is implementing the callback. Third step is stopping listening for the call back. NOTE: DisplayOnOffObserver is a random number (not very random in my case)

3. How can you directly launch your app when the iPhone starts up?

Assuming that by directly launch, you mean launch in the background, there is actually an Apple supported way of doing this. You first have to enable Background Services for location. Second, you have to start listening for significant location changes. I don't know how well this is documented, but as soon as the iPhone boots, it tries to get it's location. When it does this, it goes from not having a location to having one (which is a significant change). Your app will launch in the background which includes calling the application:didFinishLaunchingWithOptions: method, where you can ask for more time and start other processes to permanently run in the background until the user kills your application manually. If you do permanently run in the background using this method, you do stand a chance of being rejected by Apple.

Hope any of this helps someone! If anyone needs more information, just leave a comment and I will update my answer.

Dan VanWinkle
  • 991
  • 11
  • 20
1

When user press Home button,app don't go back the main interface or delay 5 seconds to go back the

You can't, unless you don't want your app in the appstore... The only thing you CAN do is ask for more time to save your data before your application is terminated, but the UI experience will still be the same - the iphone will go back to the main interface.

when the screen is black ,how to auto activation screen(how to catch power button press event) .

Not possible.

3. How direct launch my app when iPhone is start-up

I don't think you can do this even, with private APIs, and of course with your app not accepted in the appstore.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118