I use SDWebImage in my app to load and cache images from the web. It works like a charm, but the only issue is that whenever I exit my app, even momentarily, the image cache gets cleared and all the images have to be downloaded again. This is especially a problem when the Internet connection is slow. I would like the images to be retained when I return to the app. Does anyone know how to achieve this?
Go to SDImageCache.m and seek method - (id)initWithNamespace:(NSString *)ns
There you will find such code:
// Subscribe to app events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(clearMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(cleanDisk)
name:UIApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(backgroundCleanDisk)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
Feel free to comment out or change anything if you want. I'd make an option to turn off these lines of code.
make sure you don't set the maxCacheSize of [SDImageCache sharedImageCache], if it is set when the app go to background, it will clear cache files to fit the maxCacheSize.
in cleanDisk method, you can see there is a check before removing files
if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize)
{
// ...
}
so if maxCacheSize is not set, it will do nothing.
Related
I have created a method to check the status of a server in my viewcontroller, I need to check this, everytime I will open the app.
I call [self checkStatus]; from viewWillAppear and viewDidLoad, but when I open the app, by clicking home button, and I try to open the app again (clicking the app icon in applications) this method is not called. I have a NSLog to view when it is launched or not.
I'm frustrated. Thanks for your help.
You can react to app changes using NotificationCenter:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doSomething:) name:UIApplicationDidBecomeActiveNotification object:nil];
BTW: don't forget to removeObserver when you don't need it!
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
You can also use UIApplicationWillEnterForegroundNotification etc, depending what do you need.
You should read about app lifecycle on Apple Developer pages :). Read:
AppleDeveloperLink Especially section: "Execution States for Apps" to know more about app lifecycle.
StackOverflowLink to know more about view lifecycle.
iOS is not calling those methods again, but the delegate methods in the AppDelegate. You have to propagate the message to your controllers then.
I hope this will help you: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/
I want to receive a notification when the user adds a new keyboard language in the general settings and my application is in background.
Can anyone advise what code is needed to achieve this, or what preference key i should look for.
I have tried :-
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(defaultsChanged:)
name:NSUserDefaultsDidChangeNotification
object:nil];
I have a UIView ,I want it to display in a new day.For example,today I launch my app,and that UIView comes to my view,but after this,I can't see that UIView until tomorrow.
So I want to know how to judge whether it is a new day to display the UIView?
You need to register for the UIApplicationSignificantTimeChangeNotification notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(timeChange) name:UIApplicationSignificantTimeChangeNotification object:nil];
Then in the timeChange method (or whatever you call it) update your view.
Don't forget to unregister:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationSignificantTimeChangeNotification object:nil];
This notification is sent at midnight, if the user changes the time on their device, or the timezone changes. If your app is in the background when the change happens, it will get notified when the app returns to the foreground.
I have read over many stack overflow questions where people ask to terminate their app oppose to let it run in the background.
The main answer I found was to set the application does not run in background BOOL to YES in my info.plist
I have done this and cleaned my project but still my application runs in the background when the user presses the home button. This solution simply does not work.
What can I do to make my application quit when a user presses the home button.
My app is currently running on iOS 6.
Any help is appreciated :)
This answer is for your first comment, not the original question. Have your iPod view controller register for the UIApplicationDidEnterBackgroundNotification notification. The implementation should stop the music. This is a much better user experience than choosing to have your app terminate on suspend.
// Put this in a good place like viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];
// Handle the notification
- (void)backgrounding {
// app is leaving the foreground, stop the music
}
// In your dealloc method add:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
Please see the picture. After pressing the fullscreen button, the webview maximizes behind the UIPopover.
I tried to look out for MPMoviePlayerDidEnterFullscreenNotification, no luck.
I really don't want to ship my own UIPopoverController but this is my only "solution" at the moment. Using anything other than UIWebView is also not an option, as I am displaying YouTube-Movies.
Edit: I use a UIWebView, so there is no way to access the views/classes that are used internally.
According to this answer you can listen for UIMoviePlayerControllerDidEnterFullscreenNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(youTubeStarted:) name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(youTubeFinished:) name:#"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];