vinnycoyne.com
Magical adventures in nerdery.

16 July 2013

iOS - Simulate on-device memory warnings

The iOS Simulator has a handy feature that lets you simulate low-memory conditions, which is great for making sure your 'didReceiveMemoryWarning' code works as expected.

Unfortunately, this feature isn't available on-device (as far as I can tell). It is possible to trigger the low-memory notification by calling a private method, and so I've put together a small code snippet that does just that when you press either volume button on your iOS device.

Note: this code uses private, undocumented methods and will be rejected by the App Store review team. Do not ship this code in your app!

  1. In your Xcode project, link against MediaPlayer.framework
  1. include the following code in your app delegate:

#if DEBUG #import #endif - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { #if DEBUG // Debug code that lets us simulate memory warnings by pressing the device's volume buttons. // Don't ever ship this code, as it will be rejected by Apple. MPVolumeView *volume = [[MPVolumeView alloc] initWithFrame:CGRectZero]; [self.window addSubview:volume]; // Register to receive the button-press notifications __block id volumeObserver; volumeObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil queue:nil usingBlock:^(NSNotification *note) { NSLog(@"Manually simulating a memory warning."); [[UIApplication sharedApplication] performSelector:@selector(_performMemoryWarning)]; }]; #endif } [https://gist.github.com/appsandwich/6007702](https://gist.github.com/appsandwich/6007702)


← Back | 🔗 Permalink