Preface
For iOS 8 + local shared file list
text
1. Preparation
1.1 The file sharing of the app is closed by default and needs to be enabled in plist:
Application supports iTunes file sharingSet to YES
After enabling it, connect the device to iTunes, and you can see your app in the file sharing in the iTunes app (if you can't see it, you need to disconnect and unplug the data cable). You can copy some videos to facilitate testing.
1.2 Import library
Used to play videos
2. Get a video list
private let VIDEO_EXTENSIONS = [ ".MOV", ".MP4" ] private var fileManager = () func loadVideos() { var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) if > 0 { let documentsDirectory = paths[0] as String let documentUrl = NSURL(fileURLWithPath: documentsDirectory, isDirectory: true) do { let files = try (documentsDirectory) for file in files { fetchVideos((file).path ?? "") } } catch { } () } } func fetchVideos(path: String) { var isDir: ObjCBool = false if ! && (path, isDirectory: &isDir) { if isDir { do { let files = try (path) for file in files { fetchVideos(file) } } catch { } } else { var file = File(path: path) if () && isVideoFileExtension() { do { if let attr: NSDictionary = try (path) { = () } } catch { } (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) { = path = NSURL(fileURLWithPath: path) = ?? "" = "." + ( ?? "") } func isValid() -> Bool { return !( || ) } }
Code description:
a) You need to pay attention to some usage of swift, such as the usage of fileExistsAtPath
b) String's pathExtension and lastPathComponent are gone, and they are all changed to the NSURL. Many online information still gets these attributes from NSString or String.
c) AVURLAsset The duration of the video can be retrieved CMTimeGetSeconds(AVURLAsset(URL: , options: nil).duration)
3. Play videos
func play(file: File) { let player = AVPlayer(URL: ) let playerViewController = AVPlayerViewController() = player (playerViewController, animated: true) { ?.play() } }
4. Open with...
func openIn(file: File, indexPath: NSIndexPath) { let document = UIDocumentInteractionController(URL: ) let rect = (indexPath) (rect, inView: , animated: true) }
5. Delete videos
func delete(file: File, indexPath: NSIndexPath) { do { try () () ([indexPath], withRowAnimation: ) } catch { } }
6. Save to album
func saveToCameraRoll(file: File, indexPath: NSIndexPath) { if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum() { UISaveVideoAtPathToSavedPhotosAlbum(, 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 } }
Code description:
Note the usage of UISaveVideoAtPathToSavedPhotosAlbum. If the Selector is written incorrectly, an error will be reported.
The above is the example code of IOS 8 shared files. Friends who need it can refer to it.