How can I improve the startup time of my app? - iphone

Why iPhone app's splash screen takes more time for loading?
Can we set the time for few second only? it takes almost one minute at the moment.

Well the splash screen is shown as long as your app is starting, meaning not returning from the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.
If you are doing a lot of thing in this method then you should spilt up that code to do then at an other point in the start up of your app.
You should keep the startup time of your app as short as possible, start maintance code in separate thread, try lazy loading for every thing else.

The splash screen stays up until your app finishes initialising. Either speed up your initialisation code, or move the slow bits into a background task.

Are you timing this while in XCode? If you are then don't - XCode has to attach debuggers and all sorts of things before the app starts up.
To get a real time, build a release version and put it on a device. Then disconnect the device and start your app. that should give you a real-world indication of how long it will take.

Related

how to catch the power button and home button on iPhone?

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
None of these are possible on an iOS device, unless it's jailbroken.
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.
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.
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.

iPhone download / parse xml file on app launch?

I'm working on a storyboard app in which one of it's tabbed views is a mapView that downloads points from an xml file on a server. Everytime the app is launched and that tab in the tab bar controller is selected, it takes a second to download the file and parse it. Worse still, if there is no access to internet, the app crashes :/
I would like to try to use my getDataService (which downloads and parses) during the splash screen of the app instead of on ViewDidLoad of the viewcontroller (is this in appdelegate under didFinishLaunchingWithOptions?) and let it do its thing then, or in the background, and keep working in the background if the app is closed.
Then I'd like the array to be retained for future use (does this have to do with dictionaries?) and only re-download and parse the xml when the app is fully restarted.
How can I do these things? If anyone has examples or links to tutorials and examples that do these things, I'd be very grateful. I've been searching for a while, but I don't know what to search for.
Actually DO NOT do this during didFinishLaunchingWithOptions:
The reason is that iOS will kill any application that takes too long to load. I don't have the relevant documentation to hand, but iOS expects your app to finish launching within a specific period of time (I believe it's around 3 seconds or so) and if this method is not finished within that time frame you app is deemed to have hung and iOS will kill it.
The recommended technique is that if you have long running code is to start a background thread with the code on it.
The whole idea is to get the user to a usable interface as quickly as possible. Note that the debugger disables iOS's kill function, it's only active when your code is on a device and no you cannot disable it programmatically. So your code will appear to be fine when developing but fail when you run it for real.
If you need to display something whilst loading, I'd recommend putting the long running code on a background thread and continuing on to a temporary view which is basically a copy of the splash screen. then when you data is available, load up your interface.
If you do this from the AppDelegate applicationDidFinishLoading function, and assign it to a property that you define for you AppDelegate, your loading screen will still be visible while your data is being downloaded and parsed.
- (BOOL)application:(UIApplicatioN *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"loading!");
sleep(5); //delay to show you that the log happens before the loading screen goes away
return YES;
}
Make the first screen that shows when the app launches (after the Default.png splash screen) be the same or a very similar image, but give the user feedback that something is happening -- e.g. a UIActivityIndicator.
If your app doesn't fail gracefully when launched with no internet connection, Apple will reject it. Show a relevant message in this situation which advises the user something like either of these:
1) No internet connection, so functionality of this app may be limited
2) No internet connection, and this app will not function with it

Running code in a timer while application is in the background

Is it possible for this situation to happen:
My application enters the background, I want a NSTimer to run in the
- (void)applicationDidEnterBackground:(UIApplication *)application
method, every two or so seconds. I know how to initiate the timer, however what I want to know is if I can run code every 2 seconds or whatever I choose in the background? Or is it once the application has entered the background code cannot be run. I know with android if applications are left open but minimised you can run code as they continue to run in the background.
You've doubted it correct. The application that enters background can not run. So, you can not execute your code while the app in background. I'd suggest you to go through the Apple's doc Executing Code in the Background. It begins with,
"Most applications that enter the background state are moved to the suspended state shortly thereafter. While in this state, the application does not execute any code and may be removed from memory at any time."
But the services audio, location and voip are allowed to run in background. For those services the background execution must be declared in advance by the application that uses them.
If you move your application to background and declare the application as audio it will run.

How to decrease the time to go main controller after application is launched in iPhone

My application has Splash Screen in which the image taken is Default.png. Splash screen has
delay of 4 sec.The time taken to start the main Controller after launching the application is
10 sec in my Device. How can I reduce the time to come to mainScreen after the application
launch.
Can Anyone please suggest the solution for this.
Thanks in Advance.
Profile using Instruments so that you understand why your app is taking so long to start. Try to avoid doing anything that's not absolutely necessary at launch, particularly those tasks that take the longest. Defer those tasks until they're actually needed, or move them to a background thread, or both. Jobs like downloading data can usually be done quite effectively in the background.

What happens before applicationDidFinishLaunching is invoked?

I'm doing performance testing on my iphone app and I'm noticing that sometimes a good 3-4 secs elapse at startup before I start seeing my NSLogs from applicationDidFinishLaunching. I've optimized what happens once the code enters applicationDidFinishLaunching but I'm not sure how to optimize what goes on before that. I'm using a Default.png splash screen so it basically just stalls on that screen before it enters applicationDidFinishLaunching and starts doing something.
Just to give you guys some context, I have no nib files and I'm using core animation, if that makes any difference. I have about 10 different controllers and my total bundle size is just under 2MBs.
Try running it without the debugger attached - one of the things it does on startup is wait to connect to your mac ;)
If you just run it in the device without your mac connected do you still see the delay?
I did some experimentation a little while back and found that default start time for most apps is 3 seconds or more.
I was annoyed that the voice memo apps took up to 10 seconds to launch by which time I had forgotten what I was going to say. I tried to write one that would launch faster and by using a default.png and starting the recording automatically I got a recorder that constantly launched in 1-1.5 seconds. To get that launch time, I had to start the recorder before the interface became active.
However, in this research I found that most apps e.g. an app compiled straight from an Xcode template without modifications, launched in 3-5 seconds.
Based on that fact, I would say you really don't have a problem. The startup time falls in the expected range. deanWombourne's observation about the debugger is most likely correct and it probably adds a second or two to your startup time.