Swift 共享文件操作小结(iOS 8 +)

时间:2021-10-14 14:39:35

前言

  适用于 iOS 8 + 本地共享文件列表

正文

  一、准备

    1.1  默认 App 的文件共享是关闭的,需要在 plist 中设置启用:

    Application supports iTunes file sharing  设置为  YES

启用后把设备连接到 iTunes 上,在 iTunes 应用里的文件共享就能看到你的 App 了(如果看不见需要断开重新拔插一下数据线),可以拷贝一些视频进去,便于测试。

    1.2  导入库

      Photos.framework

      AVKit.framework  用于播放视频    

  二、获取视频列表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
private let VIDEO_EXTENSIONS = [
   ".MOV", ".MP4"
 ]
 
 private var fileManager = NSFileManager.defaultManager()
 
 func loadVideos() {
   var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
   if paths.count > 0 {
     let documentsDirectory = paths[0] as String
     let documentUrl = NSURL(fileURLWithPath: documentsDirectory, isDirectory: true)
     do {
       documentUrl.path
       let files = try fileManager.contentsOfDirectoryAtPath(documentsDirectory)
       for file in files {
         fetchVideos(documentUrl.URLByAppendingPathComponent(file).path ?? "")
       }
     catch {
       
     }
     
     self.tableView.reloadData()
   }
 }
 
 func fetchVideos(path: String) {
   var isDir: ObjCBool = false
   if !path.isEmpty && fileManager.fileExistsAtPath(path, isDirectory: &isDir) {
     if isDir {
       do {
         let files = try fileManager.contentsOfDirectoryAtPath(path)
         for file in files {
           fetchVideos(file)
         }
       } catch {
       }
     } else {
       var file = File(path: path)
       if file.isValid() && isVideoFileExtension(file.fileExtension.uppercaseString) {
         do {
           if let attr: NSDictionary = try fileManager.attributesOfItemAtPath(path) {
             file.fileSize = attr.fileSize()
           }
         } catch {
         }
         videos.append(file)
       }
     }
   }
 }
 
 func isVideoFileExtension(ext: String) -> Bool {
   for videoExtension in VIDEO_EXTENSIONS {
     if ext == videoExtension {
       return true
     }
   }
   return false
 }
 
 struct File {
   var fileExtension = ""
   var fileName = ""
   var path = ""
   var assert: AVURLAsset?
   var url: NSURL!
   var fileSize: UInt64 = 0
   
   init(path: String) {
     self.path = path
     self.url = NSURL(fileURLWithPath: path)
     self.fileName = url.lastPathComponent ?? ""
     self.fileExtension = "." + (url.pathExtension ?? "")
   }
   
   func isValid() -> Bool {
     return !(fileName.isEmpty || fileExtension.isEmpty)
   }
 }

代码说明:

      a)需要注意一些 swift 的用法,例如 fileExistsAtPath 的用法

      b)还有 String 的 pathExtension 和 lastPathComponent 都没了,都改到了 NSURL 下面去了,网上很多资料都还是从 NSString 或者 String 取这些属性

      c)AVURLAsset 可以取到视频的时长 CMTimeGetSeconds(AVURLAsset(URL: file.url, options: nil).duration)

  三、播放视频

?
1
2
3
4
5
6
7
8
func play(file: File) {
   let player = AVPlayer(URL: file.url)
   let playerViewController = AVPlayerViewController()
   playerViewController.player = player
   self.presentViewController(playerViewController, animated: true) {
     playerViewController.player?.play()
   }
 }

        四、用 ... 打开

?
1
2
3
4
5
func openIn(file: File, indexPath: NSIndexPath) {
   let document = UIDocumentInteractionController(URL: file.url)
   let rect = self.tableView.rectForRowAtIndexPath(indexPath)
   document.presentOpenInMenuFromRect(rect, inView: self.tableView, animated: true)
 }

        五、删除视频

?
1
2
3
4
5
6
7
8
9
func delete(file: File, indexPath: NSIndexPath) {
   do {
     try fileManager.removeItemAtPath(file.path)
     videos.removeAtIndex(indexPath.row)
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
   } catch {
     
   }
 }

        六、保存到相册

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func saveToCameraRoll(file: File, indexPath: NSIndexPath) {
   if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(file.path) {
     UISaveVideoAtPathToSavedPhotosAlbum(file.path, self, "image:didFinishSavingWithError:contextInfo:", nil)
   } else {
     // save faild
   }
 }
 
 func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
   if error == nil {
     // save success
   } else {
     // save faild
   }
 }

 代码说明:

      注意 UISaveVideoAtPathToSavedPhotosAlbum 的用法,后面 Selector 写得不对就会报错。

以上就是IOS 8 共享文件的实例代码,有需要的朋友可以参考下。