preface

The project should have an attachment function, requiring publishers to add word, PDF, etc., which viewers can download and preview.

Android terminal said OK no problem. On iOS, it indicates w. Due to the sandbox mechanism, different App files are not interchangeable on an iPhone without jailbreaking.

At least this feature is not as far-fetched as changing the App theme color based on the color of the phone case. You can still figure out how to do that.

Easy to download; Preview Many libraries have interfaces; The upload function is also not difficult. The difficulty is how to get the uploaded file.

You can’t write an underlying implementation like WPS Office. And even if it did, how many people would edit Office files on their phones, despite the larger software? Figure out how to forward Office files from somewhere else.

Rational analysis, work, editing these things are usually put on the computer to do, and then through the computer QQ, wechat, email sent to the mobile phone. Essentially, you upload it to the server and download it from the server on your phone. In order to achieve the App to obtain the file.

But our apps don’t have the function of sending from PC to phone. Can’t develop a computer side, dedicated to the user to upload files to the server.

We have also been used to office files at work through QQ, wechat, email transferred to mobile phones.

As a result, if you first send it to your mobile phone from your computer, you can also share it to the App indirectly.


The code for this article is here

In order to facilitate the test in the simulator, check the sandbox file status, made a demo preview file function, can also share the software of other apps.

Thank you to the original author. Easy to test App


First register the supported file types in the App.

This step is to enable the App to show up in the share list when you click on the share.

  • Add apple’s known format

Open the info.plist file and add the following.

Document Types: An array of supported types that hold each group item. Document Type Name: indicates the group Name. Setting it up doesn't matter. The simple point is whether to separate folder management. I split it into Office and PDF. Together, the function is the same. Handler rank: indicates the priority of the sequence. Owner, Default, Alternate. None Is not displayed. Document Content Type UTIs: UTI of the file Type. The identifier of the format. Apple has specified some identifiers for the format, linked below, you can view. You can also set the icon of the share list without setting the icon of the APP by default. I'm not going to go into that.Copy the code

Unify identifiers of the same type

For this article, the only supported Office file formats are DOC, XLS, and PPT.

However, there is no provision for the new extension name such as XLSX, how to support XLSX?


  • Add a custom format identifier

The first step is to add the following content to info.plist.

Identifier: Unique Identifier. Fill in the App's Bundle Identifier with a name. Equivalent Types: Add the extension here.Copy the code

The second step is similar to the above

As for other DOCTS, the same steps will not be posted.

Registering the supported file types is done here.


Get the file in the App after sharing

This method is called when you jump from another App to the destination App.

IOS 9.0 or later

- (BOOL)application:(UIApplication *)app 
            openURL:(NSURL *)url 
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options;
Copy the code

Versions prior to iOS 9.0

- (BOOL)application:(UIApplication *)application 
            openURL:(NSURL *)url 
  sourceApplication:(nullable NSString *)sourceApplication 
         annotation:(id)annotation;
Copy the code

The important thing is to determine what is happening based on the URL.

if ([url.host isEqualToString:@"safepay"] {// Jump to alipay wallet for payment, Handle payment results [[AlipaySDK defaultService] processOrderWithPaymentResult: url standbyCallback: ^ (NSDictionary * resultDic) { NSLog(@"result = %@",resultDic);
        }];
    }else if ([url.host isEqualToString:@"pay"] {// wechat payreturn [WXApi handleOpenURL:url delegate:self];
    }else if([url.host isEqualToString:@"oauth"] {// log inreturn [WXApi handleOpenURL:url delegate:self];
    }else if([url.host isEqualToString:@"qzapp"[QQApiInterface handleOpenURL:url delegate:self];return [TencentOAuth HandleOpenURL:url];
    } else{/ / callback handling file UIAlertController * alertCtl = [UIAlertController alertControllerWithTitle: @"File saved successfully"
                                                                           message:nil
                                                                    preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"Good" style:UIAlertActionStyleDefault handler:nil];
        [alertCtl addAction:action];
        [self.window.rootViewController presentViewController:alertCtl animated:YES completion:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"configFilesArray" object:nil];
        return YES;
    }
Copy the code

Unfortunately, file sharing callback url.host = nil. I’m going to have to put it in the last case. I hope some god tells us how to tell the difference.

When you jump into the destination App method, the files are already copied to the /Documents/Inbox directory. If the file name has the same name, “-1” or “-2” will be added automatically at the end of the file name.

To get the file name, type [url.path lastPathComponent].

This completes the file retrieval.

I made a file management tool.

I haven’t used NSFileManager very much before and it really took two days.

Which include the

Get the list of files (sorted by creation time) : easy to display in tableView. This step is really disgusting.

Walking through the folder brings up only the file name. Retrieves the file name property to return the dictionary with no file name.

Delete files: Files are deleted to clear up storage space, because shared files are not automatically deleted.


preview

  • Method one is a UIWebView. Just pass in the file path
    _webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    _webView.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    [_webView loadRequest:request];
    [_webView setScalesPageToFit:YES];
    [self.view addSubview:_webView];
Copy the code

  • Method 2 (recommended) use

    . This method is slightly more complex to use than the previous one, but has the benefit of being able to click and share. If users feel hard, you can switch to WPS browsing.