2015 年更新
标LSSharedFileList
头说这已移至CoreServices框架。事实上,如果你 Cmd-Shift-O(在 Xcode 中)并键入 LSSharedFileList,然后导航到唯一的结果,你会在跳转栏中看到标题现在确实包含在CoreServices.framework
. 无论如何,关键仍然是kLSSharedFileListFavoriteItems
.
例子:
+ (BOOL)appendFavoriteItemWithURL:(NSURL *)url {
// Pessimism ...
BOOL result = NO;
// Do we have a file URL?
if (url.isFileURL) {
// Ask CoreServices for the favorite items list
// (kLSSharedFileListFavoriteItems)
LSSharedFileListRef list = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
if (list) {
// We've got the list, so try to append our item
// (use kLSSharedFileListItemBeforeFirst vs.
// kLSSharedFileListItemLast if desired)
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(list,
kLSSharedFileListItemLast,
NULL,
NULL,
(__bridge CFURLRef)url,
NULL,
NULL);
// Did it work?
if (item) {
// Release the item and flag success
CFRelease(item);
result = YES;
}
// Release the list
CFRelease(list);
}
}
return result;
}
用法:
// Create the path to the favorite item to add
NSString * itemPath = [@"~/Music" stringByExpandingTildeInPath];
NSURL * itemURL = [NSURL fileURLWithPath:itemPath];
// Insert the item
[WhateverClassTheAboveFunctionIsIn appendFavoriteItemWithURL:itemURL];