Is applicationDidBecomeActive called in every app? - ios

I'm making a jailbreak tweak for my iOS device and have run into a problem regarding application bundleIDs and multitasking.
I currently have it set up so that when an application icon is tapped, it grabs the application ID and stores it for later. I needed to get this for multitasking so I tried applicationDidBecomeActive with no luck as it was never called when I thought it would be. Is there any way to tell when an application loads from multitasking and/or app switching?

You can register notification in your viewDidLoad:
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(selectorYouWant)
name:UIApplicationDidBecomeActiveNotification
object:nil];

Here are all the UIApplicationDelegate methods that may be called. Upon first launch application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: will be called when initially loading the app when it is launching for the first time/is no longer in memory. As for becoming active while still in memory I would recommend you use applicationWillEnterForeground:.

Related

Why is my iOS app getting launched automatically?

I have an audio app that plays when being launched. It has CarPlay entitlements, plays from the network and naturally plays in the background.
Now, every one in a while the phone launchs the app in the background without notice. Music simply starts to play randomly. I can't determine the cause or the situation in which this reproduces. It might be related to Bluetooth connections, phone calls, CarPlay interactions, network changes or other reasons.
I do know the following things for sure:
The app is launched from scratch as I have examined remote logs and seen the app is being launched.
Pausing the music from control center will kill the app, and it will be relaunching after 2-3 minutes again (and again)
There are not too many clues about your issue.
But I would try to find some possible causes base on my experiences.
First of all, check the notifications/listeners that you use in the app.
Something would be like bellow:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
for this one, once the network status changed, you would get the notification, and you might do something after that(play the music).
Second, check out if there is Timer in the app or not, it would cause problem if
the timer was set improperly and you do something by the timer.

NSNotificationCenter Add Observer not working in iOS

I am working a two applications in which one application get invoked by other application. If one of the application has been quited (Neither not in foreground or background), [[NSNotificationCenter defaultCenter] postNotificationName: was not working for very time time. If the application running in background, then every thing working as expected. I dont what going on. Some one please give me guide lines.

Safely Pause/Resume Remote UIWebView WebSockets when the app enters Background/Active states

My app is primarily a full screen UIWebView. It does not currently have any method implemented to handle pausing/resuming the WebSockets activity inside the UIWebView.
In theory my app could stop the WebView from doing everything, and simply refresh the page? I don't know if that's the best method though, which is why I'm here.
The app crashes whenever I press the physical Home button, wait a while, and attempt to resume the app by pressing the App Icon and going into it. The app will usually just crash and resume back to the Home Screen again. When I tap the App Icon again the app starts over fresh like it was just opened for the first time after startup of the device.
During the crash I see things in the debug information like...
Thread 7 WebThread: EXC_BAD_ACCESS (code=1, address=0x5)
void std::_1::_push_heap_front(WebCore::TimerHeapIterator,
WebCore::TimerHeapIterator, WebCore::TimerHeapLessThanFunction&,
std::__1::iterator_traits::difference_type)
+ 194
I'm not sure what to do, I obviously have nothing setup in my app delegate for "applicationDidEnterBackground" or "applicationDidBecomeActive" or any of the other states, as I'm not sure what is the best method to somehow "pause" the content and "resume" it, or even HOW to do that.
You can stop it in JS.
From ObjC, listen to the app state change event (UIApplicationWillResignActiveNotification or UIApplicationDidEnterBackgroundNotification).
Inside the callback, you'll call stringByEvaluatingJavaScriptFromString to stop your websockets.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willResignActive) name:UIApplicationWillResignActiveNotification object:nil];
-(void)willResignActive
{
[m_webView stringByEvaluatingJavaScriptFromString:#"stopWS();"]; // stopWS is implemented in JS
}

How to perform last actions upon user exiting an iPhone app?

Is there a way to perform some last actions when the user kills the application on iPhone?
In UIApplicationDelegate there is applicationWillTerminate: but as I understand it's not guaranteed to get called when the application terminates. Is there another way?
You can't rely on applicationWillTerminate being called. From the documentation:
For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.
The proper place to save any state is when the app enters the background. Once that happens, there is no way to know if the app will return to the foreground or if it gets killed and then started from the beginning.
All methods concerning your app state are in your AppDelegate when you use one of the project templates.
Put the code in the applicationWillResignActive: method. It will get called if your app goes to an inactive state (terminating or no).
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
The "correct" place to save state is in both -applicationDidEnterBackground: and -applicationWillTerminate:. Don't worry about double-saving; generally only one of them is called (IME, -applicationWillTerminate: is not called when your app is killed in the background).
Caveat: -applicationDidEnterBackground: is not guaranteed to be called, since it is called after your app enters the background (and thus becomes eligible for killing without notice!). If the device is low on memory when your app is backgrounded, it might be killed. The best way to avoid this is to not use too much memory in the first place.
You could use
-applicationWillResignActive:, but I do not recommend this: apps become inactive quite frequently. An obvious is system dialogs (location/privacy prompts, Wi-Fi, notifications that show as alerts, alarms), TWTweetSheet, and I suspect MFMailComposeViewController, MFMessageComposeViewController, Notification Center, the app-switcher bar (e.g. to change tracks/enable orientation lock).
you can use applicationWillResignActive method in the appdelegate, or you can do the following, if you want to save stuff, but for some reason, you dont want to do it in the app delegate:
- (void) viewDidLoad/init {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(myApplicationWillResign)
name:UIApplicationWillResignActiveNotification
object:NULL];
}
- (void) myApplicationWillResign {
NSLog(#"About to die, perform last actions");
}

iPad App Crashes on Orientation change

I am developing an iPad app. I allow both landscape and portrait mode. My UI is fine in portrait mode but when I change it to landscape mode, my UI gets messed up. I saw some SO posts related to this and I added following code in initWith... in my UIView.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(abc)
name:UIDeviceOrientationDidChangeNotification
object:nil];
My UI is working fine in portrait mode after doing this. When I change it to landscape mode, my UI is fine. But after I change it back into portrait mode, my app crashes. I read some posts on SO related to app crashing an got to know about instruments. I enabled zombies and found that a message is being sent to already released object and this message is coming from NSNotificationCenter.
Is there something else that I need to handle apart from registering my device ? Also, is there any way where in I can change the implementation from UIView to UIViewController and implement the methods that UIViewController has regarding device orientation ? Please let me know the steps I need to follow in order to get this done. Thanks!
Where are you registering for the notifications? You need to remove the observer when you are about to change orientations (either in prepForSegue or willAnimateRotationToInterfaceOrientation depending on however you've got your setup) in order prevent messaging a no longer valid object. You also don't want to pile up several notifications if your registering in viewDidAppear/viewWillAppear.
Remove the observer using:
[[NSNotificationCenter defaultCenter] removeObserver:self];//removes all notifications for that object (the way I've used it before)
or if you want to be specific, do something like:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice];//remove just that notification
The UIViewController class has several methods that deal with changes in orientation. See the docs for a discussion of those methods.
One method you should look into is viewWillLayoutSubviews. This is a common place to perform manual view layout. This is called anytime the view controller's orientation changes.
Using these methods is much more common than registering for device orientation change notifications. Based on your statements about the crash, a possible issue is that you never remove the observer that you add. For every call to addObserver there must be a corresponding call to removeObserver. Otherwise the observer is called even if it has long gone away. And this results in the crash you describe.

Resources