问题
I am using NSWorkSpace SetIcon:forFile:options:
for changing application icon. On Yosemite it is working fine. However on El Capitan application icon is not updating.
回答1:
You are not alone. Try to set nil icon first and then your icon:
[[NSWorkspace sharedWorkspace] setIcon:nil forFile:path options:0]
[[NSWorkspace sharedWorkspace] setIcon:image forFile:path options:0];
回答2:
I was facing the same problem on OSx 10.14.6. I use the workaround where we set the icon after a delay after creating the file. Immediately setting the icon was working sometimes and not refreshing sometimes despite returning success.
auto nspath = [NSString stringWithUTF8String:path.c_str()];
auto nsdata = [contents dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] removeItemAtPath:nspath error:nil];
auto ret = [[NSFileManager defaultManager] createFileAtPath:nspath contents:nsdata attributes:nil];
// hide the extension of the file after creation.
if (ret) {
[[NSFileManager defaultManager] setAttributes:@{NSFileExtensionHidden: @true} ofItemAtPath:nspath error:nil];
// set the icon, after a delay. Bug in OSx, the icon is not reflected sometimes despite YES return
// if it is set immediately after creating the file.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
auto img = [NSImage imageNamed:@"Icon"];
[[NSWorkspace sharedWorkspace] setIcon:img forFile:nspath options:0];
});
}
来源:https://stackoverflow.com/questions/33608339/nsworkspace-seticon-not-updating-application-icon-on-el-capitan