iOS 调起第三方程序打开文件

场景:
1.使用airdrop分享文件
2.使用其它app打开本app里面的文件,例如:图片,视频,pdf文件等等,
3.保存到本地文件夹

如果你有上面的任何一种需求那么看这篇文章就阔以了~。
本文按照以下三点讲解:
1.调起分享界面
2.如何可以把我们的app显示到分享界面
3.copy到我们app后如何处理

一.调起分享界面
这个比较简单,使用系统的UIDocumentInteractionController类,弹出后系统回自动识别能copy的app,回自己展示到弹出的面板上,系统自动处理了,很方便,不需要我们做多余的操作,比较简单,直接上代码:



 NSString *imageZipPath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"zip"];
        [self setupDocumentControllerWithURL:[NSURL fileURLWithPath:imageZipPath]];

        BOOL ret = NO;
        ret = [self.docInterCtrl
               presentOpenInMenuFromRect:self.view.bounds
               inView:self.view
               animated:YES];
        if (!ret)
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:Promt message:@"no app can open this file"preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancle"  style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }];
            
            [alertController addAction:cancelAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }







- (void)setupDocumentControllerWithURL:(NSURL *)url
{
    if (self.docInterCtrl == nil) {
        self.docInterCtrl =
        [UIDocumentInteractionController interactionControllerWithURL:url];
        self.docInterCtrl.delegate = self;
    } else {
        self.docInterCtrl.URL = url;
    }
}

注意事项:把UIDocumentInteractionController声明为一个强指针strong类型的实例,声明为局部的话回过早释放,会发生崩溃。
还可预览文件,这个需要事项相应的代理,比较简单,就不多说了。

二.如何可以把我们的app显示到分享界面

如果想我们的app可以打开别的app的文件,并展示到分享面板上,要配置一下info.plist文件,告诉系统哪些类型的文件需要使用UIDocumentInteractionController来打开。
总结一下:

<key>CFBundleDocumentTypes</key>

<array>

<dict>

<key>CFBundleTypeName</key>

<string>com.myapp.common-data</string>

<key>LSHandlerRank</key>

<string>Default</string>

<key>LSItemContentTypes</key>

<array>

<string>com.microsoft.powerpoint.ppt</string>

<string>public.item</string>

<string>com.microsoft.word.doc</string>

<string>com.adobe.pdf</string>

<string>com.microsoft.excel.xls</string>

<string>public.image</string>

<string>public.content</string>

<string>public.composite-content</string>

<string>public.archive</string>

<string>public.audio</string>

<string>public.movie</string>

<string>public.text</string>

<string>public.data</string>

</array>

</dict>

</array>

大概也就那么多,需要的直接粘贴就可以。

三.保存到本地文件夹
别的app的文件用我们的app打开,系统回将该文件copy到Documents/Inbox文件夹下,只需要对这个文件夹做处理及可以了。
我的做法是对Inbox文件夹做一个监听,文件夹有变动做某些操作即可。
监听文件夹使用的是苹果官方给出的类DirectoryWatcher

DirectoryWatcher.h

#import <Foundation/Foundation.h>

@class DirectoryWatcher;

@protocol DirectoryWatcherDelegate <NSObject>
@required
- (void)directoryDidChange:(DirectoryWatcher *)folderWatcher;
@end

@interface DirectoryWatcher : NSObject
{
    id <DirectoryWatcherDelegate> __weak delegate;
    
    int dirFD;
    int kq;
    
    CFFileDescriptorRef dirKQRef;
}

@property (nonatomic, weak) id <DirectoryWatcherDelegate> delegate;

+ (DirectoryWatcher *)watchFolderWithPath:(NSString *)watchPath delegate:(id<DirectoryWatcherDelegate>)watchDelegate;
- (void)invalidate;

@end

DirectoryWatcher.m


#import "DirectoryWatcher.h"

#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>

#import <CoreFoundation/CoreFoundation.h>

@interface DirectoryWatcher (DirectoryWatcherPrivate)

- (BOOL)startMonitoringDirectory:(NSString *)dirPath;
- (void)kqueueFired;

@end

@implementation DirectoryWatcher

@synthesize delegate;

- (instancetype)init
{
    self = [super init];
    delegate = NULL;
    
    dirFD = -1;
    kq = -1;
    dirKQRef = NULL;
    
    return self;
}

- (void)dealloc
{
    [self invalidate];
}

+ (DirectoryWatcher *)watchFolderWithPath:(NSString *)watchPath delegate:(id)watchDelegate
{
    DirectoryWatcher *retVal = NULL;
    if ((watchDelegate != NULL) && (watchPath != NULL))
    {
        DirectoryWatcher *tempManager = [[DirectoryWatcher alloc] init];
        tempManager.delegate = watchDelegate;
        if ([tempManager startMonitoringDirectory: watchPath])
        {
            // Everything appears to be in order, so return the DirectoryWatcher.
            // Otherwise we'll fall through and return NULL.
            retVal = tempManager;
        }
    }
    return retVal;
}

- (void)invalidate
{
    if (dirKQRef != NULL)
    {
        CFFileDescriptorInvalidate(dirKQRef);
        CFRelease(dirKQRef);
        dirKQRef = NULL;
        // We don't need to close the kq, CFFileDescriptorInvalidate closed it instead.
        // Change the value so no one thinks it's still live.
        kq = -1;
    }
    
    if(dirFD != -1)
    {
        close(dirFD);
        dirFD = -1;
    }
}

@end


#pragma mark -

@implementation DirectoryWatcher (DirectoryWatcherPrivate)

- (void)kqueueFired
{
    assert(kq >= 0);
    
    struct kevent   event;
    struct timespec timeout = {0, 0};
    int             eventCount;
    
    eventCount = kevent(kq, NULL, 0, &event, 1, &timeout);
    assert((eventCount >= 0) && (eventCount < 2));
    
    // call our delegate of the directory change
    [delegate directoryDidChange:self];
    
    CFFileDescriptorEnableCallBacks(dirKQRef, kCFFileDescriptorReadCallBack);
}

static void KQCallback(CFFileDescriptorRef kqRef, CFOptionFlags callBackTypes, void *info)
{
    DirectoryWatcher *obj;
    
    obj = (__bridge DirectoryWatcher *)info;
    assert([obj isKindOfClass:[DirectoryWatcher class]]);
    assert(kqRef == obj->dirKQRef);
    assert(callBackTypes == kCFFileDescriptorReadCallBack);
    
    [obj kqueueFired];
}

- (BOOL)startMonitoringDirectory:(NSString *)dirPath
{
    // Double initializing is not going to work...
    if ((dirKQRef == NULL) && (dirFD == -1) && (kq == -1))
    {
        // Open the directory we're going to watch
        dirFD = open([dirPath fileSystemRepresentation], O_EVTONLY);
        if (dirFD >= 0)
        {
            // Create a kqueue for our event messages...
            kq = kqueue();
            if (kq >= 0)
            {
                struct kevent eventToAdd;
                eventToAdd.ident  = dirFD;
                eventToAdd.filter = EVFILT_VNODE;
                eventToAdd.flags  = EV_ADD | EV_CLEAR;
                eventToAdd.fflags = NOTE_WRITE;
                eventToAdd.data   = 0;
                eventToAdd.udata  = NULL;
                
                int errNum = kevent(kq, &eventToAdd, 1, NULL, 0, NULL);
                if (errNum == 0)
                {
                    CFFileDescriptorContext context = { 0, (__bridge void *)(self), NULL, NULL, NULL };
                    CFRunLoopSourceRef      rls;
                    
                    // Passing true in the third argument so CFFileDescriptorInvalidate will close kq.
                    dirKQRef = CFFileDescriptorCreate(NULL, kq, true, KQCallback, &context);
                    if (dirKQRef != NULL)
                    {
                        rls = CFFileDescriptorCreateRunLoopSource(NULL, dirKQRef, 0);
                        if (rls != NULL)
                        {
                            CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
                            CFRelease(rls);
                            CFFileDescriptorEnableCallBacks(dirKQRef, kCFFileDescriptorReadCallBack);
                            
                            // If everything worked, return early and bypass shutting things down
                            return YES;
                        }
                        // Couldn't create a runloop source, invalidate and release the CFFileDescriptorRef
                        CFFileDescriptorInvalidate(dirKQRef);
                        CFRelease(dirKQRef);
                        dirKQRef = NULL;
                    }
                }
                // kq is active, but something failed, close the handle...
                close(kq);
                kq = -1;
            }
            // file handle is open, but something failed, close the handle...
            close(dirFD);
            dirFD = -1;
        }
    }
    return NO;
}

@end

将这两个文件夹导入使用既可。

eg:

#import "DirVC.h"
#import "DirectoryWatcher.h"
@interface DirVC ()<DirectoryWatcherDelegate>
@property (nonatomic, strong) DirectoryWatcher                *indoxWatcher;
@end

@implementation DirVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.indoxWatcher = [DirectoryWatcher watchFolderWithPath:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Inbox"]] delegate:self];
}

#pragma mark - 文件夹变动监听代理
- (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
{
    
}
@end

这样就能监听相应文件夹的变化了,不过Inbox文件夹操作文件的时候有点问题,建议将文件copy到自己建的文件夹做相应的操作。
如此就可以了,如果有更好的方法,欢迎私信交流~。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,475评论 4 372
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,744评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,101评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,732评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,141评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,049评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,188评论 2 320
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,965评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,716评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,867评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,341评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,663评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,376评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,200评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,990评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,179评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,979评论 2 279

推荐阅读更多精彩内容