ios App上传Excel文件

今天App有个需求,需要从本地上传Excel文件到后台,大家知道苹果是沙盒机制,App之间不许分享数据。不过话说回来,上传文件大部分人应该是从微信或者QQ等三方应用过来,那么问题就是可不可以App之间分享数据,这个功能肯定是有的。

  • 首先配置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>

ios App上传Excel文件
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:str
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          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

Demo地址

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