首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIDocumentInteractionController,无文件扩展名,但使用UTI

UIDocumentInteractionController,无文件扩展名,但使用UTI
EN

Stack Overflow用户
提问于 2011-12-13 03:44:09
回答 1查看 5.5K关注 0票数 7

如何将文件发送到不同的应用程序,以了解该应用程序支持哪个UTI?假设该文件没有文件扩展名,但我碰巧知道该文件的UTI。

我尝试了以下几种方法:

代码语言:javascript
复制
// target is a NSURL with the location of the extension less file on the system
// knownUTI is a NSString containing the UTI of the file 
    UIDocumentInteractionController* dic = [UIDocumentInteractionController interactionControllerWithURL:target];  
    [dic retain];

    dic.delegate = self;
    dic.UTI = knownUTI; 
    [dic presentOpenInMenuFromRect:CGRectZero inView:superController.view animated:YES]

它显示支持的应用程序,但是,如果我选择它,它不会切换应用程序。委托调用

代码语言:javascript
复制
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application

代码语言:javascript
复制
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application

永远不会被调用,并且应用程序永远不会切换。

目标应用程序以以下方式导出其UTI:

代码语言:javascript
复制
    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeIconFiles</key>
            <array/>
            <key>CFBundleTypeName</key>
            <string>Migration DocType</string>
            <key>CFBundleTypeRol</key>
            <string>Shell</string>
            <key>LSHandlerRank</key>
            <string>Owner</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>com.mycomp.customstring</string>
            </array>
        </dict>
    </array>

...

<key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.data</string>
            </array>
            <key>UTTypeDescription</key>
            <string>My custom UTI</string>
            <key>UTTypeIdentifier</key>
            <string>com.mycomp.customstring</string>
        </dict>
    </array>

由于这不起作用,我也尝试添加一个自定义扩展。尽管如此,它仍然不会以这种方式工作。在向文件添加自定义扩展名时,我将其移交给DocumentInteractionController,它可以正常工作。但是,应用程序列表显示了支持相同文件扩展名的所有其他应用程序,而不管我提供的UTI类型是什么。

假设我在两个不同的应用程序中声明了两个UTI:

代码语言:javascript
复制
App1 with UTI1: com.mycomp.a  with extension .abc
App2 with UTI2: com.mycomp.b  with extension .abc

当将文件交给DocumentInteractionController,并将UTI设置为com.mycomp.a时,它还会将App2显示为能够处理该文件的可能应用程序。

我用下面的方式定义了一个带扩展的UTI:

代码语言:javascript
复制
<key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.data</string>
            </array>
            <key>UTTypeDescription</key>
            <string>My UTI Type</string>
            <key>UTTypeIdentifier</key>
            <string>com.mycomp.a</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <string>abc</string>
                <key>public.mime-type</key>
                <string>application/abc</string>
            </dict>
        </dict>
    </array>

我真的很感谢你的帮助,我有点卡住了。因此,问题又来了:我如何将一个文件发送到一个已知UTI的应用程序,或者没有扩展名,或者与其他文件具有相同的扩展名,而我不想在DocumentInteractionController中将应用程序显示为选择?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-14 01:16:08

我找到了这个问题的解决方案。然而,我认为这不是一个很好的选择。

在测试过程中,我发现如果不使用文件扩展名,UIDocumentInteractionController将根据我指定的UTI显示应用程序。当将文件发送到目标应用程序时,不会发生任何事情。我得出结论,我需要一个文件扩展名来完成最终的发送。

我的方法是在将文件发送到目标应用程序之前修改URL属性,并为其提供相同的文件,但目标应用程序接受一个文件扩展名。尽管如此,我的应用程序还是崩溃了。我使用Instruments对其进行了分析,发现问题是由于UIDocumentInteractionController过度释放了一些代理对象。我还看到,最终的过度释放发生在一个名为_invalidate of UIDocumentInteractionController(Private)的方法中,这个方法调用了category来释放对象。

因为类别不能被其他类别覆盖,所以我决定用我自己的实现混合使用category方法,检查URL是否包含文件扩展名,然后将调用重定向到原始的_invalidate方法,或者什么也不做。

下面的代码显示了我所做的事情:

代码语言:javascript
复制
#include <objc/runtime.h>

@interface UIDocumentInteractionController(InvalidationRedirect)

-(void)_invalidateMY;
+(void)load;
void Swizzle(Class c, SEL orig, SEL newSEL);
@end

@implementation UIDocumentInteractionController(InvalidationRedirect)

void Swizzle(Class c, SEL orig, SEL newSEL)
{
    Method origMethod = class_getInstanceMethod(c, orig);
    Method newMethod = class_getInstanceMethod(c, newSEL);
    if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
        class_replaceMethod(c, newSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    else
        method_exchangeImplementations(origMethod, newMethod);
}

-(void)_invalidateMY{
    @synchronized(self) {
        if(![[[[self.URL lastPathComponent] componentsSeparatedByString:@"."] lastObject] isEqualToString:@"extension"]) {
            [self _invalidateMY];
        }
    }
}

+(void)load
{
    Swizzle([UIDocumentInteractionController class], @selector(_invalidate), @selector(_invalidateMY));
}

@end

这段代码与_invalidateMY交换原始的_invalidate方法,导致每次对_invalidate的调用都调用_invalidateMY,反之亦然。

下面的代码显示了我如何处理UIDocumentInteractionController

代码语言:javascript
复制
  // create a file without extension
  NSString *fileName = @"myFile";

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

  NSString *documentsDirectory = [paths objectAtIndex:0];

  NSURL* target = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]];

  if([[@"THIS IS JUST A TEST STRING" dataUsingEncoding:NSUTF8StringEncoding] writeToURL:target atomically:NO]) {
      NSLog(@"file written successfully");
  }else {
      NSLog(@"Error.. file writing failed");
  }

  UIDocumentInteractionController* dic = [UIDocumentInteractionController interactionControllerWithURL:target];
  [dic retain];
  dic.delegate = self;


  // set the UTI to the known UTI we want to list applications for
  dic.UTI = @"com.mycomp.a";

  [dic presentOpenInMenuFromRect:CGRectZero inView:superController.view animated:YES];

下面的代码显示了UIDocumentInteractionController的委托方法,它交换URL:

代码语言:javascript
复制
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application
{
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSError *error;
    NSURL* newTarget = [NSURL URLWithString:[NSString stringWithFormat:@"%@.extension", controller.URL]];
    // rename file to file with extension
    if (![fileMgr moveItemAtURL:controller.URL toURL:newTarget error:&error] && error) {
        NSLog(@"Error moving file: %@", [error localizedDescription]);
    }
    @synchronized(controller) {
        //exchange URL with URL+extension
        controller.URL = newTarget; //<- this results in calling _invalidate
    }
    NSLog(@"%@", [NSString stringWithContentsOfURL:controller.URL encoding:NSUTF8StringEncoding error:nil]);
}

这个解决方案是有效的,但在我看来,这是一个肮脏的黑客行为,必须有更好的解决方案。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8479917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档