gpt4 book ai didi

Objective-C:如何让应用程序保持在停靠状态?

转载 作者:行者123 更新时间:2023-12-03 17:10:21 25 4
gpt4 key购买 nike

我试图将我的应用程序保留在停靠位置,但我无法做到这一点。 enter image description here

我想怎么做?

@interface UserDefaultsHelper : NSUserDefaults

- (BOOL)addApplicationToDock:(NSString *)path;

- (BOOL)removeApplicationFromDock:(NSString *)name;

@end


@implementation UserDefaultsHelper

- (BOOL)addApplicationToDock:(NSString *)path {
NSDictionary *domain = [self persistentDomainForName:@"com.apple.dock"];
NSArray *apps = [domain objectForKey:@"persistent-apps"];
NSArray *matchingApps = [apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K CONTAINS %@", @"tile-data.file-data._CFURLString", path]];
if ([matchingApps count] == 0) {
NSMutableDictionary *newDomain = [domain mutableCopy];
NSMutableArray *newApps = [apps mutableCopy];
NSDictionary *app = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObjectsAndKeys:path, @"_CFURLString", [NSNumber numberWithInt:0], @"_CFURLStringType", nil] forKey:@"file-data"] forKey:@"tile-data"];
[newApps addObject:app];
[newDomain setObject:newApps forKey:@"persistent-apps"];
[self setPersistentDomain:newDomain forName:@"com.apple.dock"];
return [self synchronize];
}
return NO;
}

- (BOOL)removeApplicationFromDock:(NSString *)name {
NSDictionary *domain = [self persistentDomainForName:@"com.apple.dock"];
NSArray *apps = [domain objectForKey:@"persistent-apps"];
NSArray *newApps = [apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"not %K CONTAINS %@", @"tile-data.file-data._CFURLString", name]];
if (![apps isEqualToArray:newApps]) {
NSMutableDictionary *newDomain = [domain mutableCopy];
[newDomain setObject:newApps forKey:@"persistent-apps"];
[self setPersistentDomain:newDomain forName:@"com.apple.dock"];
return [self synchronize];
}
return NO;
}

@end

我从这里找到了上面的代码。 Code Source

上面的代码正在编译并运行,但是它正在将应用程序添加到应用程序的扩展坞中。

我是这里的新手。请对我宽容一点,因为我没有操作系统水平的知识。

提前非常感谢您的帮助和关注。

最佳答案

所提供的代码是正确的,唯一的问题是扩展坞没有刷新。所以我们只需要刷新DOCK

为了刷新扩展坞,我从 obj-c 调用终端命令

终端命令 killall dock

    ....code...
[self runCommand:@"killall Dock"];
}

-(NSString*)runCommand:(NSString*)commandToRun;
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];

NSArray *arguments = [NSArray arrayWithObjects:
@"-c" ,
[NSString stringWithFormat:@"%@", commandToRun],
nil];
NSLog(@"run command: %@",commandToRun);
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *output;
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
return output;
}

关于Objective-C:如何让应用程序保持在停靠状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com