今天App有个需求,需要从本地上传Excel文件到后台,大家知道苹果是沙盒机制,App之间不许分享数据。
第一种方式就是App之间共享数据,上传文件大部分人应该是从微信或者QQ等三方应用过来,那么问题就是可不可以App之间分享数据,这个功能肯定是有的。
第二种方式就是iCloud Drive,直接从iCloud中选择,操作起来像从相册选择图片一样。iOS8支持,大部分都是iOS8系统了应该没什么版本问题。

  • 首先配置info.plist文件,这里主要就是注册自己的App支持打开的文件类型,比如Excel表格,当我们在微信里面打开时,分享面板就有了我们自己的App 了,最好直接打开文件编辑,不要在页面中修改,我修改的时候xcode总是闪退,不好用。
  • 还有一个问题就是LSItemContentTypes文件类型的问题,我只需要上传Excel如果写成com.microsoft.excel.xls那只能打开xls后缀的文件,但是微软也有xlsx后缀的,大家可以参考这篇文章, 另外苹果的官方支持的类型列表
<key>CFBundleDocumentTypes</key><array><dict><key>CFBundleTypeIconFiles</key><array><string>App-logo-橙色-58.png</string></array><key>CFBundleTypeName</key><string>Excel文件</string><key>LSHandlerRank</key><string>Owner</string><key>LSItemContentTypes</key><array><string>com.microsoft.excel.xls</string><string>org.openxmlformats.spreadsheetml.sheet</string><string>org.openxmlformats.wordprocessingml.document</string><string>com.microsoft.word.doc</string></array></dict></array>


info以后基本上就可以了,完成以后需要在Appdelegate里面处理App分享过来的文件,返回的url是自己App的沙盒路径

//
//  AppDelegate.m
//  testword
//
//  Created by TTSiMac on 2018/12/11.
//  Copyright © 2018 tts.com. All rights reserved.
//#import "AppDelegate.h"
#import "DisPlayXLSViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.if (launchOptions) {NSString *str = [NSString stringWithFormat:@"\n发送请求的应用程序的 Bundle ID:%@\n\n文件的NSURL:%@\n\n文件相关的属性列表对象:%@",launchOptions[UIApplicationLaunchOptionsSourceApplicationKey],launchOptions[UIApplicationLaunchOptionsURLKey],launchOptions[UIApplicationLaunchOptionsSourceApplicationKey]];[[[UIAlertView alloc] initWithTitle:@""message:strdelegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil, nil] show];}return YES;
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
{
//    UINavigationController *navigation = (UINavigationController *)application.keyWindow.rootViewController;
//    ViewController *displayController = (ViewController *)navigation.topViewController;
//
//    [displayController.imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]]];
//    [displayController.label setText:sourceApplication];return YES;
}#else
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options
{NSData *data = [NSData dataWithContentsOfURL:url];UIViewController *navigation = application.keyWindow.rootViewController;DisPlayXLSViewController *displayController = [[DisPlayXLSViewController alloc]init];displayController.fileURL = url;[navigation presentViewController:displayController animated:YES completion:nil];return YES;
}
#endif- (void)applicationWillResignActive:(UIApplication *)application {// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}- (void)applicationDidEnterBackground:(UIApplication *)application {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}- (void)applicationWillEnterForeground:(UIApplication *)application {// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}- (void)applicationDidBecomeActive:(UIApplication *)application {// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}- (void)applicationWillTerminate:(UIApplication *)application {// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}@end

第二种从iCloud Drive上传。参考代码,

//
//  ViewController.m
//  testword
//
//  Created by TTSiMac on 2018/12/11.
//  Copyright © 2018 tts.com. All rights reserved.
//#import "ViewController.h"
#import <QuickLook/QuickLook.h>@interface ZZDocument : UIDocument
@property (nonatomic, strong) NSData *data;
@end
@implementation ZZDocument- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError {self.data = contents;return YES;
}@endtypedef void(^downloadBlock)(id obj);@interface iCloudManager : NSObject+ (BOOL)iCloudEnable;+ (void)downloadWithDocumentURL:(NSURL*)url callBack:(downloadBlock)block;@end
@implementation iCloudManager+ (BOOL)iCloudEnable {NSFileManager *manager = [NSFileManager defaultManager];NSURL *url = [manager URLForUbiquityContainerIdentifier:nil];if (url != nil) {return YES;}NSLog(@"iCloud 不可用");return NO;
}
+ (void)downloadWithDocumentURL:(NSURL*)url callBack:(downloadBlock)block {ZZDocument *iCloudDoc = [[ZZDocument alloc]initWithFileURL:url];[iCloudDoc openWithCompletionHandler:^(BOOL success) {if (success) {[iCloudDoc closeWithCompletionHandler:^(BOOL success) {NSLog(@"关闭成功");}];if (block) {block(iCloudDoc.data);}}}];
}@end@interface ViewController ()<UIDocumentPickerDelegate,QLPreviewControllerDataSource>@property (nonatomic,strong) UIButton *opentICloudButton;
@property (nonatomic,strong)  QLPreviewController *previewController;
@property (nonatomic,copy) NSString *filePath;
@property (nonatomic,strong) NSURL *fileURL;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.opentICloudButton = [UIButton buttonWithType:UIButtonTypeCustom];self.opentICloudButton.frame = CGRectMake(0, 0, 100, 200);[self.opentICloudButton setTitle:@"打开iCloud" forState:UIControlStateNormal];[self.opentICloudButton addTarget:self action:@selector(openclick:) forControlEvents:UIControlEventTouchUpInside];self.opentICloudButton.backgroundColor = [UIColor orangeColor];[self.view addSubview:self.opentICloudButton];self.previewController = [[QLPreviewController alloc] init];self.previewController.view.frame = CGRectMake(0, 200,self.view.frame.size.width, self.view.frame.size.height-200);self.previewController.dataSource = self;[self addChildViewController:self.previewController];[self.view addSubview:self.previewController.view];}
- (void)openclick:(UIButton *)sender
{[self presentDocumentPicker];
}
- (void)presentDocumentPicker {NSArray *documentTypes = @[@"public.content", @"public.text", @"public.source-code ", @"public.image", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls", @"com.microsoft.powerpoint.ppt"];UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypesinMode:UIDocumentPickerModeOpen];documentPickerViewController.delegate = self;[self presentViewController:documentPickerViewController animated:YES completion:nil];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {NSArray *array = [[url absoluteString] componentsSeparatedByString:@"/"];NSString *fileName = [array lastObject];fileName = [fileName stringByRemovingPercentEncoding];self.fileURL = url;if ([iCloudManager iCloudEnable]) {[iCloudManager downloadWithDocumentURL:url callBack:^(id obj) {NSData *data = obj;//写入沙盒Documentsself.filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fileName]];[data writeToFile:self.filePath atomically:YES];[self.previewController reloadData];}];}
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{//需要在线预览的文件的路径if (self.fileURL) {return self.fileURL;}return nil;
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

Demo地址

UIDocumentInteractionController之程序间文档共享
使用iCloud API的正确方式
iOS实现App之间内容分享

ios App上传Excel文件相关推荐

  1. 实现web前端上传excel文件到flask服务器

    这里有两种方法:1. html方式:2. javascript方式(jQuery+ajax) 第1种方法:html方式 这种方法直接通过设计form表单相关属性实现将excel表格上传到服务器,并在服 ...

  2. Django框架(上传Excel文件并读取)

    博主今天整理下Django框架中上传Excel文件并读取 博主是要在管理平台中新增用例的维护功能,想着通过上传Excel文件来展示用例,下面是项目的路径图: 首先先建数据库模型 model.py 可以 ...

  3. php上传查询excel到mysql_PHP上传Excel文件导入数据到MySQL数据库示例

    PHP上传Excel文件导入数据到MySQL数据库示例2020-06-20 00:34:11 最近在做Excel文件导入数据到数据库.网站如果想支持批量插入数据,可以制作一个上传Excel文件,导入里 ...

  4. php 上传excel到mysql_PHP上传Excel文件导入数据到MySQL数据库示例

    最近在做Excel文件导入数据到数据库.网站如果想支持批量插入数据,可以制作一个上传Excel文件,导入里面的数据内容到MySQL数据库的小程序. 要用到的工具: ThinkPHP:轻量级国产PHP开 ...

  5. 服务器上传excel文件并读取数据,asp.net上传Excel文件并读取数据的实现方法

    前言 本文主要给大家介绍了关于asp.net上传Excel文件并读取数据的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 实现如下: 前台代码:使用服务端控件实现上传 服务端 ...

  6. vue之 上传 excel文件

    目录 vue之 上传 excel文件 父组件 UploadExcel.vue 效果 vue之 上传 excel文件 父组件 <upload-excel-component :on-success ...

  7. 帆软报表决策系统 上传excel文件

    这个属于二次开发的,比如我要在决策系统中开发一个功能,上传excel文件或者其他文件, 首先前端使用fineUI: BI.File1 = BI.inherit(BI.File,{_defaultCon ...

  8. Web项目,网页上传excel文件并解析实战示例

    最近写了一个基于poi解析excel文件的工具类,所以想在web项目中测试一下,就做了这个简单的项目.本项目主要使用了 SpringMVC+RESTful+Maven的风格.适合有一定基础的人员. 源 ...

  9. 上传excel文件到服务器

    最近遇到了需要上传excel文件,并将excel表中的数据都出来,存到数据库中的需求,今天将步骤整理一下,如下: 一.新建一个html(或jsp页面),如:uploadExcel.html,代码如下: ...

最新文章

  1. Cooike的一些用法
  2. 2.Java异常学习
  3. flex属性值----弹性盒子布局
  4. 微软发布Azure Cosmos DB产品以及新的物联网解决方案
  5. sublimeformaya
  6. 安装mysql查看随机密码命令_linux安装mysql-8.0.21-1.el7.x86_64.rpm-bundle - 君主-bye
  7. [Android]SQLite的使用
  8. 福利 | 2018 OpenInfra Days China限量版免费票任性放出
  9. “头疼”的俄罗斯开发者:不要再买我的软件了,收入不能提现
  10. MySql字符串拼接
  11. 世界记忆大师的记忆力训练方法
  12. linux WIFI命令iwlist、iwconfig、iwpriv
  13. weblogic安装失败常见问题
  14. [AcWing] 1017. 怪盗基德的滑翔翼(C++实现)最长上升子序列模型
  15. 分享:从编程中悟出的八字箴言
  16. 在Ubuntu安装和使用Anbox完整说明(一种在Linux使用Android应用的方法)
  17. 【目标检测评价指标】
  18. android菜单栏设置位置,android系统设置setting菜单在哪
  19. Java吃货联盟订餐系统
  20. 服务器系统电源管理,企业IT节能 巧用Windows系统电源管理

热门文章

  1. QT Creator 快速入门教程 读书笔记(一)
  2. 什么?跑跑卡丁车明天要出手游了?电脑玩跑跑卡丁车手游攻略提前看
  3. WordPress 5.9.2 官方版发布
  4. 什么是批处理,批处理与流处理的对比
  5. 责任链模式——P2P理财
  6. html 标签中 title 的换行
  7. 【unity shader案例】如何实现一个玻璃效果
  8. 计算机二级考试报名要上传照片吗,报考计算机等级考试(NCRE)对上传照片有何要求...
  9. 『天涯杂谈』语不惊人死不休——2004年最一针见血的500句话 (401...)
  10. 青蛙豪华版内存地址修改无限流