如何解析并仅获取此字符串值

时间:2017-01-13 07:13:47

标签: objective-c

我想只获得数组字符串值应用。例如(SLGoogleAuth,HalfTunes,TheBackgrounder,Calculiator)。但不知道该怎么办? 这是一个代码。

    @implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    
    Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
    SEL selector=NSSelectorFromString(@"defaultWorkspace");
    NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector]; 
    SEL selectorALL = NSSelectorFromString(@"allApplications");
    NSLog(@"apps: %@", [workspace performSelector:selectorALL]);
}

它是输出:

enter image description here

提前致谢

1 个答案:

答案 0 :(得分:0)

你不想解析它。 NSLog打印出对象的描述。您想直接访问该值。

[LSApplicationWorkspace allApplications];

返回LSApplicationProxy的NSArray。 LSApplicationProxy类有一个ivar _bundleURL,其中包含您需要的信息。您需要运行时功能才能访问它。下面的工作示例:

    // #import <objc/runtime.h>
    Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
    SEL selector=NSSelectorFromString(@"defaultWorkspace");
    NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector];
    SEL selectorALL = NSSelectorFromString(@"allApplications");
    NSArray* appProxies = [workspace performSelector:selectorALL];

    Ivar bundleUrlIvar = class_getInstanceVariable([appProxies.firstObject class], "_bundleURL");

    NSMutableString* result = [NSMutableString string];
    for (id appProxy in appProxies)
    {
        NSURL* url = object_getIvar(appProxy, bundleUrlIvar);
        // at this point you have the information and you can do whatever you want with it
        // I will make it a list as you asked
        if (url)
        {
            [result appendFormat:@",%@", [url lastPathComponent]];
        }
    }

    if (result.length > 0)
    {
        // remove comma from beginning of the list
        [result deleteCharactersInRange:NSMakeRange(0, 1)];
    }


    NSLog(@"apps: %@", result);

请注意,当您使用私有api时,AppStore会拒绝此操作。因此请自行决定使用。