美文网首页iOS Developer
给 APP添加外部文件导入功能

给 APP添加外部文件导入功能

作者: Arclin | 来源:发表于2017-06-21 15:09 被阅读394次

意思就是在其他app点击分享之类的按钮时会出现 '用xxxx(你的app名字)导入' 然后你就可以吧这个文件导入你的app的沙盒当中.

  1. 修改 info.plist(这里public.data是允许所有文件类型,如果要特定某种类型的文件,如只允许导入PPT, 或者Word文档, 那么就得添加多个CFBundleTypeName CFBundleTypeRole LSHandlerRank LSItemContentTypes 具体见苹果API文档)
 <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeIconFiles</key>
            <array/>
            <key>CFBundleTypeName</key>
            <string>data</string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSHandlerRank</key>
            <string>Default</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>public.data</string>
            </array>
        </dict>
    </array>
    
  1. APPDelegate.h

获取到根控制器,执行复制到Document文件夹方法
源路径:url.path

@property (strong, nonatomic) NSURL *sharedURL;

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    if(url.fileURL){
        self.sharedURL = url;
        UIViewController *vc = self.window.rootViewController;
        if ([vc isKindOfClass:[UINavigationController class]]) {
            UINavigationController * nav = (UINavigationController *)self.window.rootViewController;
            UIViewController *topVC = nav.childViewControllers.firstObject;
            if ([topVC respondsToSelector:@selector(handleSharedFile)]) {
                [topVC performSelectorOnMainThread:@selector(handleSharedFile) withObject:nil waitUntilDone:NO];
            }
        } else {
            if ([vc respondsToSelector:@selector(handleSharedFile)]) {
                [vc performSelectorOnMainThread:@selector(handleSharedFile) withObject:nil waitUntilDone:NO];
            }
        }
    }
}
  1. 根控制器方法(头文件需要声明方法)
- (void)handleSharedFile {
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (app.sharedURL != nil) {
        self.sharedURL = [app.sharedURL copy];
        app.sharedURL = nil;
        [self saveSharedFile:self.sharedURL];
    }
}
- (void)saveSharedFile:(NSURL *)url {
    MBProgressHUD *hud = [MBProgressHUD showButtonHUDAddedTo:self.view animated:YES];
    DKFile *file = [[DKFile alloc] init];
    file.fullPath = url.path;
    file.fileName = url.path.lastPathComponent;
    [[DKFileManager sharedInstance] copyItemsOfSelectFiles:@[file] fromStorage:DKFileStorageTypeInternal toStorage:DKFileStorageTypeInternal toPath:[DKFileManager defaultPath:kShareDirectory storage:DKFileStorageTypeInternal] progressHUD:hud complete:^{
        [SVProgressHUD showSuccessWithStatus:@"已保存到 iPhone -> SharedFiles"];
    } failure:^(NSError *errors) {
        
    }];
}

相关文章

  • 给 APP添加外部文件导入功能

    意思就是在其他app点击分享之类的按钮时会出现 '用xxxx(你的app名字)导入' 然后你就可以吧这个文件导入你...

  • 面试题(一)

    1、html添加样式的几种方式 1)链接外部样式文件:样式文件需额外引入,应在 元素中增加 子元素2)导入外部样式...

  • 给App的某个功能添加桌面快捷方式

    给App的某个功能添加桌面快捷方式 给App的某个功能添加桌面快捷方式

  • cocos2d-x系列

    NDK mk 语法2.0 一.import-module的功能 导入外部模块的.mk文件 ,和 include基本...

  • okhttp模拟get、post请求

    现在 build.gradle(Module:app) 文件的 dependencies 节点下添加下面代码(导入...

  • 如何查看调试手机里的文件?

    因为从外部导入问价到自己的APP内部,还要对文件进行一些操作,那首先就是要获取文件,这是导入的地址://url =...

  • Vue仿CNODE社区(单页面)

    在 APP.vue 中引入外部文件时,注意路径的大小写问题(会警告或者报错) 给元素添加 v-for = 'ite...

  • springboot导入excel文件并解析为entity

    好久没写了,写一下最近用到的导入excel文件的功能吧 1、maven的pom文件添加依赖 2、excel导入工具...

  • Robot Framework - Variable file

    RF导入变量文件 在Setting中导入 Setting中导入变量文件时,和导入外部资源文件类似。变量文件的路径可...

  • Data Binding

    【1】导入依赖 【2】 在app模块下的 build.gradle 文件添加内容 【3】 布局 [本章完...]

网友评论

    本文标题:给 APP添加外部文件导入功能

    本文链接:https://www.haomeiwen.com/subject/hrqtcxtx.html