Is there a way to get notified when the user drags a file into iTunes File Sharing and Sync is complete? - iphone

When I plug in the iPhone to iTunes and drag a file into the File Sharing section of my app, the app on the screen goes away for a moment and then comes back. It seems that none of the app delegate methods are triggered at this time, not even something like "went to background, went to foreground".
As soon as my app comes back after a sync where the user added or removed files, I want to update the screen.
Maybe there is a notification beeing sent?

Also, <MediaPlayer/MediaPlayer.h> framework's [MPMediaLibary defaultMediaLibrary] can post notification MPMediaLibraryDidChangeNotification, which is fired especially when your media library is updated while your device is syncing with iTunes.
You can let your object to observe this notification by adding:
#import <MediaPlayer/MediaPlayer.h>
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(<#selector#>:) name:MPMediaLibraryDidChangeNotification object:[MPMediaLibrary defaultMediaLibrary]];
Also make sure to activate this notification by using - (void)beginGeneratingLibraryChangeNotifications

- (void)applicationWillResignActive:(UIApplication *)application is called when the sync starts and - (void)applicationDidBecomeActive:(UIApplication *)application after the sync is complete

applicationWillResignActive does not work starting with iOS 5.0.
You can use the DirectoryWatcher class in the DocInteraction sample app.

Related

Can your iPhone app be notified of new notifications?

If your iPhone app is running is there a way to let the app know when a new notification comes in?
Normally the notification window pops up on the top then goes away, but that is handled by the system. Is there a call made to the app that is currently running?
While my app is running I would like to keep track of new notifications.
Thanks, Nick
There is a call made to the AppDelegate
-didReceiveRemoteNotification
-(void)application: (UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"%#",userInfo);
}

Prevent iPhone from locking when connected to charger and app is running

I'm writing an iPhone app. When the app is running and the iPhone is charging, there is no need to lock the iPhone. Is it possible to prevent the locking of the iPhone when the device is charging and my app is running?
You can subscribe to UIDeviceBatteryStateDidChangeNotification notification to get the moment when your iphone begins/stops to charge. Then in case iphone is charging you can set idleTimerDisabled property in UIApplication object to YES to prevent device to go to sleep:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateBatteryState:)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
- (void) updateBatteryState:(NSNotification*)notification{
[UIApplication sharedApplication].idleTimerDisabled =
([UIDevice currentDevice].batteryState == UIDeviceBatteryStateCharging);
}
P.S. If user decides to put device to sleep with sleep/wake button there's no way to prevent him of doing so
This is not possible with current SDK.
EDIT: hmm, haven't got the question correctly from the first read - look on other replies for correct answer; my guess was, you were asking about if it possible to prevent appearance of the sync/charge screen when connecting device via usb or to the wall outlet

Is there a way in iOS to observe the home button being pushed?

I am basically trying to make the device play a sound whenever the app exits (even if the phone is being turned off, the sound would play while the button is being held down). Is this possible?
You could try the - (void)applicationWillResignActive:(UIApplication *)application UIApplicationDelegate method.
Alternately, you could listen for the UIApplicationWillResignActiveNotification notification.

UILocalNotification fires after reinstalling the app

My app has an alarm function using UILocalNotification, and it works great. However, if the user uninstalls the app, then later REINSTALLS it, he would receive all the "in between" notifications at once.
I have tried to call:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if it's the first time the app is launched, but it doesn't help, because the notification is received even before application:didFinishLaunchingWithOptions: is called.
This was worse in 4.0 when the alarm was repeated even if the user has deleted the app, but at least that bug was fixed by Apple in later release. However now I'm stuck with this. Anyone has an idea?
According to Apple, this is not a bug (I filed a bug report). The system retains the UILocalNotifications for uninstalled apps for 24 hours just in case the user deleted the app by accident, and restores the said UILocalNotifications if the app is re-installed within that time frame.
The solution would be to remove all UILocalNotifications on first startup, like so:
- (BOOL) application: (UIApplication*) application
didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
if (self.isFirstRun)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.firstRun = NO;
}
/* Other code here */
...
}
of course, implement your own firstRun setter and getter to fetch/save into persistent storage, like NSUserDefaults.
This is actually a bug in iPhone. If you removed the application and install it later also, it will have same app id, so when the application is reinstalled all the past local notifications were fired even if you didn't open the app.

Awake from sleep event on the iPhone?

Is there any way to detect if the iPhone wakes up from sleep while you're app is running? Eg: your app is running, the user locks the screen (or the screen auto locks) and some time later the user unlocks the screen and up pops your app. Is there some way to get an event at that point or detect it somehow?
I've tried searching the Google and this forum, but I can't seem to find anything about it.
See applicationDidBecomeActive: on UIApplicationDelegate.
Stick these in you AppDelegate.m file:
-(void) applicationWillResignActive:(UIApplication *)application {
NSLog(#"Asleep");
}
-(void) applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"Awake");
}
#Kevin - Nothing wrong with your answer - thanks by the way. Just thought I'd save the next person a Google search.