# AppShareFile
**Repository Path**: xiaoyigegespec/AppShareFile
## Basic Information
- **Project Name**: AppShareFile
- **Description**: 文件在其他应用和自己应用之间的传输,把自己应用添加在系统的活动列表中
- **Primary Language**: Swift
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-07-17
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
应用间的分享
# 1、设置 Info.plist
```swift
CFBundleDocumentTypes
CFBundleTypeName
com.myapp.common-data
LSItemContentTypes
com.microsoft.powerpoint.ppt
com.microsoft.word.doc
com.adobe.pdf
com.microsoft.excel.xls
public.image
public.content
public.composite-content
public.archive
public.audio
public.movie
public.text
public.data
public.item
```
# 代码实现
## 1.
```swift
var documentVC:UIDocumentInteractionController!
@IBAction func share(_ sender: Any) {
let url = URL.init(fileURLWithPath: filePath.path)
let topVC = Utils.topviewController()
documentVC = UIDocumentInteractionController(url: url)
// documentVC.url = url
documentVC.delegate = self
documentVC.presentOpenInMenu(from: CGRect.zero, in: (topVC?.view)!, animated: true)
}
//Mark: - UIDocumentInteractionControllerDelegate
func documentInteractionController(_ controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) {
}
func documentInteractionController(_ controller: UIDocumentInteractionController, didEndSendingToApplication application: String?) {
}
func documentInteractionControllerDidDismissOpenInMenu(_ controller: UIDocumentInteractionController) {
}
```
## 2.
> 用moveItem 拷贝文件,不要用copyItem, 因为会自动生成一个 Inbox 文件
经过测试上面的方法不太可行,应在新建一层根目录:
```
let baseFolderName = "AppBaseFolder"
/// documen的路径
var documentBaseUrl: NSURL {
let url = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
let baseFolderPath = self.createFolder(name: baseFolderName, routeUrl: url)
let baseFolderUrl = URL.init(fileURLWithPath: baseFolderPath)
return baseFolderUrl as NSURL
}
```
在 AppDelegate 的didFinishLaunchingWithOptions中
```swift
/// 源文件名字
let sourceName = url.lastPathComponent
/// 源文件路径
let soureUrlPath = url.path
let targartUrl = FilePathUtils.documentsDirectoryPath()
let ulr = URL.init(string: targartUrl)?.appendingPathComponent(sourceName)
//if FileManager.default.fileExists(atPath: ulr!.path) {
//print("-------->>>>>>>>>> 该文件已经存在")
//}
//else {
do {
try FileManager.default.moveItem(atPath: soureUrlPath, toPath: ulr!.path)
print("Success to copy file.")
} catch {
print("Failed to copy file.")
}
// }
```