简体   繁体   中英

LSCopyApplicationURLsForURL always returns null

When I try to log all available editors on my system for my temporary file (which is "toString" in this code) it always returns null, although I have many applications installed on my system.

NSArray *appUrls = (NSArray*)LSCopyApplicationURLsForURL((CFURLRef)[NSURL URLWithString:toString], kLSRolesViewer | kLSRolesEditor);

toString is containing the following file path: /var/folders/pl/tcc5k3fd6tj2__9dprg9dm1m0000gp/T/tempFile

What should be the problem here?

[NSURL URLWithString:toString]

expects a complete URL string including scheme, such as "file://var/folders/...".

Use

[NSURL fileURLWithPath:toString]

instead to get a file URL with the specified path.

Another problem could be that your file name does not have any file extension (eg ".txt"), because Launch Services uses the extension (or file type/creator) to find a suitable application.

I was struggling with the this and I wanted to get all Bundles that could open a determined path/file extension.

If you have a file extension, you can get all bundles that can edit it by the following:

//All Bundle Ids
NSString *pathExtension = @"docx";
CFArrayRef utisRef = UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension,(__bridge  CFStringRef) pathExtension,nil);
NSLog( @"UTI: utisRef %@", utisRef);
NSArray *utis = CFBridgingRelease(utisRef);
NSMutableSet *mutableSet = [[NSMutableSet alloc] init];
for (NSString *uti in utis) {
    CFArrayRef bundleIDsRef = LSCopyAllRoleHandlersForContentType((__bridge  CFStringRef) uti,kLSRolesEditor);
    [mutableSet addObjectsFromArray:CFBridgingRelease(bundleIDsRef)];
}
NSLog( @"bundleIDs: %@", mutableSet);

If you have a path of file and you want to get all apps location that can edit it, you can use the following:

//Location of apps
NSString *str = @"/Users/ricardoanjos/Library/Developer/Xcode/DerivedData/EgnyteDrive-hforbniifiojczefbnwanzxakvlr/Build/Products/Debug/1.pdf";
NSURL* url = [[NSURL alloc] initFileURLWithPath:str];
CFURLRef urlRef = (__bridge CFURLRef)url;
CFArrayRef appUrlsRef = LSCopyApplicationURLsForURL(urlRef, kLSRolesEditor);
NSArray *appUrls = CFBridgingRelease(appUrlsRef);
NSLog(@"appUrls: %@", appUrls);

I hope it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM