gpt4 book ai didi

objective-c - 如何从 __NSCFType 解析 NSString 值?

转载 作者:行者123 更新时间:2023-12-03 17:49:24 26 4
gpt4 key购买 nike

我正在尝试获取 OSX10 的所有启动(登录)应用程序。为此,我编写了以下代码(如下所示):

-(NSMutableArray*)getStartUpApplicaitonPaths{
// Get the LoginItems list.
LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItemsRef == nil) return nil;
// Iterate over the LoginItems.
NSArray *loginItems = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);

NSMutableArray* data = [NSMutableArray arrayWithArray:loginItems];

return data;
}

从上面的代码中,我得到了 __NSCFType 对象的 NSMutableArray。当我尝试通过转换数组的对象来获取路径时

NSString* file = [NSString stringWithFormat:@"%@", [startupFiles objectAtIndex:0]];

我得到的结果如下:

BundleBinding [0x103] URL: file:///Applications/iTunes.app/Contents/MacOS/iTunesHelper.app/ bundle identifier: com.apple.iTunesHelper iTunesHelper

我需要从上面给出的字符串中解析 URL 和标识符。请帮忙。

最佳答案

对象的类型为:LSSharedFileListItem,仅记录在头文件中。

这里有一些可能有帮助的代码,它将 NSLog() 所有文件名:

NSURL *itemURL = [[NSBundle mainBundle] bundleURL];
CFURLRef URLToToggle = (__bridge CFURLRef)itemURL;

LSSharedFileListRef loginItems = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, /*options*/ NULL);
if (loginItems) {
UInt32 seed = 0U;
Boolean found;

CFArrayRef currentLoginItems = LSSharedFileListCopySnapshot(loginItems,
&seed);
const CFIndex count = CFArrayGetCount(currentLoginItems);
for (CFIndex idx = 0; idx < count; ++idx) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(currentLoginItems, idx);
CFURLRef outURL = NULL;

const UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
#if (__MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10)
outURL = LSSharedFileListItemCopyResolvedURL(item, resolutionFlags, /*outError*/ NULL);
if (outURL == NULL) {
if (outURL)
CFRelease(outURL);
continue;
}
#else
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &outURL, /*outRef*/ NULL);
if (err != noErr || outURL == NULL) {
if (outURL)
CFRelease(outURL);
continue;
}
#endif
found = CFEqual(outURL, URLToToggle);
NSLog(@"%@", outURL);
CFRelease(outURL);
}

CFRelease(currentLoginItems);
CFRelease(loginItems);
}

我的实例中的输出:

file:///Volumes/User/dgrassi/Library/PreferencePanes/MouseLocator.prefPane/Contents/Resources/MouseLocatorAgent.app/
file:///Applications/iTunes.app/Contents/MacOS/iTunesHelper.app/
file:///Applications/Dropbox.app/
file:///Library/PreferencePanes/Screens%20Connect.prefPane/Contents/MacOS/Screens%20Connect.app/
file:///Library/Application%20Support/EyeTV/EyeTV%20Helper.app/ file:///Applications/Carbon%20Copy%20Cloner.app/Contents/Library/LoginItems/CCC%20User%20Agent.app/

这来自seafile-client

关于objective-c - 如何从 __NSCFType 解析 NSString 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31948696/

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