<span style="font-size:18px;">#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btnOpen = [[UIButton alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width-40, 40)];
btnOpen.backgroundColor = [UIColor purpleColor];
[btnOpen setTitle:@"开启桌面图标" forState:UIControlStateNormal];
[btnOpen setTag:0];
[btnOpen addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnOpen];
UIButton *btnHidden = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, self.view.frame.size.width-40, 40)];
btnHidden.backgroundColor = [UIColor purpleColor];
[btnHidden setTitle:@"隐藏桌面图标" forState:UIControlStateNormal];
[btnHidden setTag:1];
[btnHidden addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnHidden];
}
- (void)btnClick:(id)sender {
NSInteger tag = [sender tag];
if (tag ==0) {
NSLog(@"开启桌面图标");
}else {
[self hiddenAppIcon];
NSLog(@"隐藏桌面图标");
}
}
- (void)hiddenAppIcon
{
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
NSFileManager* manager = [NSFileManager defaultManager];
if (plistPath)
{
NSLog(@"%@", plistPath);
NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *array = [NSArray arrayWithObject:@"hidden"];
[infoDict setObject:array forKey:@"SBAppTags"];
[infoDict writeToFile:plistPath atomically:YES];
[manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
NSMutableDictionary* infoDictModified = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(@"%@", infoDictModified);
if ([manager isWritableFileAtPath:plistPath])
{
NSLog(@"written");
} else {
NSLog(@"Something went wrong");
}
}
}
@end</span>
第一次运行图标会存在,退出模拟器,然后运行其他的APP,再回到桌面,APP的图标就会隐藏了。