Skip to content

Instantly share code, notes, and snippets.

@liyamahendra
Forked from camdenfullmer/CMFWallpaper.m
Created January 9, 2018 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liyamahendra/5fc7f87acca9ddbeeedd761241438223 to your computer and use it in GitHub Desktop.
Save liyamahendra/5fc7f87acca9ddbeeedd761241438223 to your computer and use it in GitHub Desktop.
Set the wallpaper (lock and/or home screen) on iOS using Apple's private API. Please be aware that including this code in an App Store submission will result in a rejection. This has only been tested on iOS 9.2.
@implementation CMFWallpaper
+ (void)setImage:(UIImage *)image {
NSAssert([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized, @"access to photos is needed to set the wallpaper");
NSString *path;
#if TARGET_OS_SIMULATOR
path = @"/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibrary.framework";
#else
path = @"/System/Library/PrivateFrameworks/PhotoLibrary.framework";
#endif
NSBundle *bundle = [NSBundle bundleWithPath:path];
[bundle load];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
// Instantiate the controller.
id class = NSClassFromString(@"PLStaticWallpaperImageViewController");
id controller = [[class alloc] performSelector:@selector(initWithUIImage:) withObject:image];
// Select what wallpaper mode.
// 0 - Both lock screen and home screen.
// 1 - Home screen only.
// 2 - Lock screen only.
int wallpaperMode = 0;
[controller setValue:@(wallpaperMode) forKey:@"wallpaperMode"];
// Tell the controller to save the data.
[controller setValue:@YES forKey:@"saveWallpaperData"];
// Save the photo.
[controller performSelector:@selector(_savePhoto) withObject:nil];
#pragma clang diagnostic pop
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment