ios 如何在 iPhone 中以编程方式设置锁屏、壁纸和铃声?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4476777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:20:23  来源:igfitidea点击:

How to set lock screen , wallpaper and Ringtone programmatically in iPhone?

iosobjective-ciphoneios4iphone-privateapi

提问by ios

In iPhone can we set the lock screen, wallpaper and ringtone programmatically?

在 iPhone 中我们可以通过编程设置锁屏、壁纸和铃声吗?

If Yes, then please let me know how to set them?

如果,那么请让我知道如何设置它们?

回答by WrightsCS

This can all be done easily, but will be rejected by Apple.

这一切都可以轻松完成,但会被 Apple 拒绝。

The ringtone can be changed by altering com.apple.SpringBoard.plist, specifically the ringtonekey.

铃声可以通过改变来改变com.apple.SpringBoard.plist,特别是ringtone键。

The following code can be used to read the actual ringtone title of custom ringtones (synced by iTunes).

以下代码可用于读取自定义铃声的实际铃声名称(由 iTunes 同步)。

NSMutableDictionary *custDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/private/var/mobile/Media/iTunes_Control/iTunes/Ringtones.plist"];
NSMutableDictionary *dictionary = [custDict objectForKey:@"Ringtones"];

NSArray *keys = [dictionary allKeys];
id key = [keys objectAtIndex:indexPath.row];
NSMutableDictionary *customRingtone = [dictionary objectForKey:key];
NSString *name = [customRingtone objectForKey:@"Name"];
cell.textLabel.text = name;

The Wallpapers can be overwritten at:

壁纸可以在以下位置覆盖:

NSString *homePath1 = @"/private/var/mobile/Library/SpringBoard/HomeBackground.jpg";
NSString *homePath2 = @"/private/var/mobile/Library/SpringBoard/HomeBackgroundPortrait.jpg";
NSString *lockPath1 = @"/private/var/mobile/Library/SpringBoard/LockBackground.jpg";
NSString *lockPath2 = @"/private/var/mobile/Library/SpringBoard/LockBackgroundPortrait.jpg";

These examples were used in one of my Cydia apps. Theres not really much more to them, but these should get you going in the right direction.

这些示例用于我的 Cydia 应用程序之一。对他们来说并没有更多,但这些应该会让你朝着正确的方向前进。

回答by Nate

The answer by WrightsCSstopped working at some point due to a change in iOS. Unfortunately, this is something you have to live with if you wish to use undocumentedfeatures.

由于 iOS 的变化,WrightsCS答案在某个时候停止工作。不幸的是,如果您想使用未记录的功能,这是您必须忍受的。

If you still need to do this, for non-App Store apps only, this code works in iOS 9.3. It could stop working in any future iOS release, though. (see comment below: no longer working in iOS 10)

如果您仍然需要这样做,仅对于非 App Store 应用程序,此代码适用于 iOS 9.3。不过,它可能会在任何未来的 iOS 版本中停止工作。(请参阅下面的评论:不再在 iOS 10 中工作)

#import "SBSUIWallpaperPreviewViewController.h"
#import <dlfcn.h>

// open the private framework dynamically
void *handle = dlopen("/System/Library/PrivateFrameworks/SpringBoardUIServices.framework/SpringBoardUIServices", RTLD_NOW);

UIImage *wallpaper = [UIImage imageNamed: @"background.jpg"];

Class sbClass = NSClassFromString(@"SBSUIWallpaperPreviewViewController");
// we create a view controller, but don't display it. 
//  just use it to load image and set wallpaper
SBSUIWallpaperPreviewViewController *controller = (SBSUIWallpaperPreviewViewController*)[[sbClass alloc] initWithImage: wallpaper];
[controller setWallpaperForLocations: 3];  // 3 -> set both for lock screen and home screen

dlclose(handle);

You'll need to add the private API header to your project. You can usually find these online with a little searching, for example, here.

您需要将私有 API 标头添加到您的项目中。您通常可以通过一些搜索在网上找到这些,例如,这里

In the example above, [SBSUIWallpaperPreviewViewController setWallpaperForLocations:]is called with an argument of 3: 3 indicates the image should be used for bothlock and home screens. 1 indicates Lock screen only. 2 indicates Home screen only.

另外,在上述的例子中,[SBSUIWallpaperPreviewViewController setWallpaperForLocations:]被调用的3参数:3表示图像应被用于两个锁和主画面。1 表示仅锁定屏幕。2 仅表示主屏幕。



For an explanation of why I open this framework up dynamically, see my related answer here.

有关为什么我动态打开此框架的解释,请参阅此处的相关答案

I don't have an answer regarding ringtones. This really should be a separate question: completely different APIs at work.

我没有关于铃声的答案。这真的应该是一个单独的问题:工作中的 API 完全不同。

回答by Bobo Shone

use private api if you can check PLStaticWallpaperImageViewController

如果可以检查,请使用私有 api PLStaticWallpaperImageViewController