Why is my iOS app getting launched automatically? - ios

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.

Related

UILocalNotification not firing properly on iPhone when app not foregrounded

UPDATE - a warning to anyone with similar symptoms... check Do Not Disturb is not active!
I finally paid to get an app I was working on running on my iPhone instead of on the simulator.
Having done so I've spotted a strange behaviour with regards to the UILocalNotifications that I create, which are set to use the default sound UILocalNotificationDefaultSoundName, and the default timezone ([NSTimeZone defaultTimeZone]).
If the app is foregrounded then the local notification fires on time and I receive it within
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
and I create an alert view to display it.
If however I lock my phone just before a notification is expected then nothing appears on the screen, but when I press the home button to show the lock screen I can see my app's notification in the notification centre on the lock screen. No sound was played, but then again the phone didn't wake up to show the notification properly.
If however I just press the home button so the app is put in the background leaving me looking at the springboard apps then I don't see any notifications whatsoever, and again no sound is heard.
On the simulator the same code correctly fires the notification banner when looking at the app icons in the spring board, although I've never been able to hear any sounds for the notification. But as the simulator doesn't let you configure the notification centre for apps I always presumed it was perhaps set to not allow sounds. On my device sounds for this apps notifications are enabled, and i've tried it with banner and alert modes, with no joy. Very frustrating.
Has anyone experienced this, or have an idea what could be going on?
Oh the irony of this answer.. think it's time to go to bed. My iPhone thinks that too for nearly 3 hours now... My question happily describes Do Not Disturbs behaviour, which was active.. Sorry to waste everyones time!!

Is applicationDidBecomeActive called in every app?

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:.

Xcode - How to deal with notifications posted into NSNotificationCenter while application was in the background as soon as app comes to foreground

I am currently dealing with Smartcard reader connected to a IOS device as an external accessory. When application goes to the background and remains there for a few (let's say 10-15 seconds) the reader is disconnected by iOS in order not to drain battery. This sends notification to NSNotificationCenter that the reader (EAAccessory) has been disconnected. When app comes to the foreground, it usually takes some time till the reader is connected back.
I am able to handle with these notifications using following methods:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(EAaccessoryConnect) name:EAAccessoryDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(EAaccessoryDisConnect) name:EAAccessoryDidDisconnectNotification object:nil];
The problem is, sometimes when application is in the background for a longer time (longer than 20 minutes), more than one EAAccessoryDidDisconnectNotification is obviously posted to the notification centre as well as EAAccessoryDidConnectNotification.
When I receive EAAccessoryDidDisconnectNotification I have to adequately handle it and allow some time till the EAAccessoryDidConnectNotification is received. However when another EAAccessoryDidDisconnectNotification comes, it messes up with my settings and the user is logged out from the application.
My question therefore is - is it possible to check which notifications have been sent to the NSNotificationCenter while app was in the background just after the app comes to the foreground, and remove multiple notification of the same kind - leaving only one notification of a kind. Or is there any other solution you suggest me to implement to deal with this?

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
}

UILocalNotification how to call certain method if application is in background

I am creating player application, which is playing audio streams through internet. I want to add alarm functionality in my app - in particular time my player begins to play audio stream, I am trying to use UILocalNotification mechanism. But I've difficulties with it when my application in background mode, I can't call 'play' method, when notification is receiced (can't without user interaction). May be it is impossible?
But I bought this application:
http://itunes.apple.com/us/app/radio-alarm-clock-mp3-radio/id380271167?mt=8
And it seems like radio can start playing when local notification is received. Alarm can start playing radio when my app is in background mode.
Earlier I was trying to use NSTimer for this, but when my app goes to background, timer stops. If I use beginBackgroundTaskWithExpirationHandler: it works only 10 minutes. My app has special flag in plist, what is is audio application, and can playing music in background. In this case timers are working. But if I stop playing and go to background, timer is not working.
When I use \Radio Alarm Clock' application, I hear 'white noise' from dinamic, when music in not playing. May be it is the secret of this application?
Can you help me with my problem? Thanks.
maybe it's too late.
I had a look to the app you've mentioned at http://itunes.apple.com/us/app/radio-alarm-clock-mp3-radio/id380271167?mt=8 and yes, I think you are absolutely right, the only way to achieve that the application remains active while in background is to play a fake sound while it is in the background, which should be prohibited by Apple.
I've also seen that they don't use the remote iPod control, and this was strange at a first look.
At the end my opinion is that they do the following:
Avoid the call to beginReceivingRemoteControlEvents which allows to activate the iPod controls while in background (in fact they don't have it)
In this way, the status bar doesn't show the play icon while
the app plays audio
When the app goes in background, it probably plays a no sound periodically (once every 10 secs for example), in this way the app remains active
I saw that they also avoided to manage interruptions, for example in case another app is in foreground and plays music. Again Apple should have rejected the app for that reason, cos it is against the rules to follow while in background, but maybe they didn't see it during the acceptance tests.
So my interpretation is that they have intentionally missed to activate the iPod controls, just to avoid to show the play icon in the status bar while in background. In this way, the users are unaware that the app is active and is doing something strange after they close it.
In addition you can see that the app doesn't interrupt when another app plays in foreground a sound or audio, because otherwise they risk that the app doesn't restart on time when the alarm shpould fire.
That's just my idea of how they do that, and I think this is the only way for an audio app on iOS to remain active while it is in background and is supposed to be halted (well, in case Apple doesn't see the trick).
Have you tried adding this to appdelegate.m
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Call your method in here.
}
if you have can you add code for us to see what your doing.

Resources