Skip to content

Instantly share code, notes, and snippets.

@camdenfullmer
Last active February 18, 2024 03:05
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save camdenfullmer/b287f634acd089f4b92f to your computer and use it in GitHub Desktop.
Save camdenfullmer/b287f634acd089f4b92f 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
@rursache
Copy link

any update for iOS 11?

@DaniWinter
Copy link

@iphonewsro still works on iOS 11

@DaniWinter
Copy link

Have you found a way to turn of perspective? Like in settings you can choose still or perspective wallpaper.

@jdong03
Copy link

jdong03 commented Jul 15, 2018

@DaniWinter How can a beginner implement this ~ are you using swift 4... Can you please let me know how to use this in a simple view app. Thank you so much. What type of app are you in? I am in single view

@gnanakeethan
Copy link

Awesome. Any idea on putting this into a FaceID system so that the wall paper could change depending on the user.

@davidgyoung
Copy link

davidgyoung commented Dec 17, 2018

Unfortunately, I don't see this working in iOS 10 and 11.4 despite what @DaniWinter says. I get a crash with the same Assertion failed: (0), function SBSUIWallpaperClearVideo, file /BuildRoot/Library/Caches/com.apple.xbs/Sources/SpringBoardUIServices/SpringBoard-3563.21.47.2/SBSUIWallpaperUtilities.m, line 143. described in the comments about a similar solution here: https://stackoverflow.com/a/38993774/1461050

@AblyAbhinav
Copy link

Hi
Is this solution working on ios 13 or up ?

@MichaelMKenny
Copy link

Hi
Is this solution working on ios 13 or up ?

No.
Somewhere down the line, setting a wallpaper now requires a private entitlement (can't build to device, not App Store safe). But I did figure out a solution when iOS 13 came out. Just note to actually set the wallpaper you'll need to be jailbroken or be using the simulator.

See my github project for setting separate light and dark wallpapers here:
https://github.com/MichaelMKenny/ios-13-light-dark-wallpaper-app

@AblyAbhinav
Copy link

Hi MichaelMKenny

Thanks for your prompt response.

I'm okay if my app is not able to publish on Appstore. but I would run on all devices without jailbreak.

If you can provide some more insights then it will be really helpful for me.

@MichaelMKenny
Copy link

Hi MichaelMKenny

Thanks for your prompt response.

I'm okay if my app is not able to publish on Appstore. but I would run on all devices without jailbreak.

If you can provide some more insights then it will be really helpful for me.

Not really much more to say. All my code is useless without the private entitlement. The only way to run it on the device is with a jailbreak. You can test the code by running it in the simulator, but Apple won't let you sign an app with a private entitlements onto a real device.

You may have heard of the psychic paper exploit that would skip the private entitlements check on iOS devices on 13.4.1 and below. But with my testing it seems Apple does further checking to prevent this. I spent a good few days messing around trying to get the private entitlement onto my device and use it to set a wallpaper (without a jailbreak), but the setting part wouldn't work (I was successful in using the exploit to achieve getting the private entitlement onto the device, just not the setting part).

TLDR, if you want it to be on your device that's not going to happen until Apple loosens their grip on that private API, or actually adds a wallpaper API to the SDK. I can report however that it fully works on my jailbroken device.

Sorry I don't have better news.

@AblyAbhinav
Copy link

Hi MichaelMKenny

Thanks for your detailed reply.

I checked your code and that really works perfectly on ios 13 simulators.

I'm planning to create an app which where I can select/add pictures of a particular category and with in-app in the configuration screen, user can set either set both lock and home screen or any single option and user can set a timer in which app will change wallpaper on regular interval.

I was planning to add a payment option (IAP) to unlock some categories.

As per my knowledge with Jailbroken devices, I can't implement IAP. If I would like to move with this app with jailbreak devices only then can you please guide

  1. What payment options I need to add to unlock some categories?
  2. I can add logic to randomly chose images and call your methods in the background to set wallpaper
  3. Where do I need to publish the app for jailbroken devices?

Please advise and guide to get some insights to get this app done.

--
Regards

@MichaelMKenny
Copy link

Hi MichaelMKenny

Thanks for your detailed reply.

I checked your code and that really works perfectly on ios 13 simulators.

I'm planning to create an app which where I can select/add pictures of a particular category and with in-app in the configuration screen, user can set either set both lock and home screen or any single option and user can set a timer in which app will change wallpaper on regular interval.

I was planning to add a payment option (IAP) to unlock some categories.

As per my knowledge with Jailbroken devices, I can't implement IAP. If I would like to move with this app with jailbreak devices only then can you please guide

  1. What payment options I need to add to unlock some categories?
  2. I can add logic to randomly chose images and call your methods in the background to set wallpaper
  3. Where do I need to publish the app for jailbroken devices?

Please advise and guide to get some insights to get this app done.

--
Regards

I'm sorry I don't think I can be of anymore assistance. My code is just a proof of concept. Hope you find a way to do what you want to do :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment