Swift 共享文件操作小结(iOS 8 +)
前言
适用于iOS8+本地共享文件列表
正文
一、准备
1.1默认App的文件共享是关闭的,需要在plist中设置启用:
ApplicationsupportsiTunesfilesharing设置为YES
启用后把设备连接到iTunes上,在iTunes应用里的文件共享就能看到你的App了(如果看不见需要断开重新拔插一下数据线),可以拷贝一些视频进去,便于测试。
1.2导入库
Photos.framework
AVKit.framework用于播放视频
二、获取视频列表
privateletVIDEO_EXTENSIONS=[
".MOV",".MP4"
]
privatevarfileManager=NSFileManager.defaultManager()
funcloadVideos(){
varpaths=NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)
ifpaths.count>0{
letdocumentsDirectory=paths[0]asString
letdocumentUrl=NSURL(fileURLWithPath:documentsDirectory,isDirectory:true)
do{
documentUrl.path
letfiles=tryfileManager.contentsOfDirectoryAtPath(documentsDirectory)
forfileinfiles{
fetchVideos(documentUrl.URLByAppendingPathComponent(file).path??"")
}
}catch{
}
self.tableView.reloadData()
}
}
funcfetchVideos(path:String){
varisDir:ObjCBool=false
if!path.isEmpty&&fileManager.fileExistsAtPath(path,isDirectory:&isDir){
ifisDir{
do{
letfiles=tryfileManager.contentsOfDirectoryAtPath(path)
forfileinfiles{
fetchVideos(file)
}
}catch{
}
}else{
varfile=File(path:path)
iffile.isValid()&&isVideoFileExtension(file.fileExtension.uppercaseString){
do{
ifletattr:NSDictionary=tryfileManager.attributesOfItemAtPath(path){
file.fileSize=attr.fileSize()
}
}catch{
}
videos.append(file)
}
}
}
}
funcisVideoFileExtension(ext:String)->Bool{
forvideoExtensioninVIDEO_EXTENSIONS{
ifext==videoExtension{
returntrue
}
}
returnfalse
}
structFile{
varfileExtension=""
varfileName=""
varpath=""
varassert:AVURLAsset?
varurl:NSURL!
varfileSize:UInt64=0
init(path:String){
self.path=path
self.url=NSURL(fileURLWithPath:path)
self.fileName=url.lastPathComponent??""
self.fileExtension="."+(url.pathExtension??"")
}
funcisValid()->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)
三、播放视频
funcplay(file:File){
letplayer=AVPlayer(URL:file.url)
letplayerViewController=AVPlayerViewController()
playerViewController.player=player
self.presentViewController(playerViewController,animated:true){
playerViewController.player?.play()
}
}
四、用...打开
funcopenIn(file:File,indexPath:NSIndexPath){
letdocument=UIDocumentInteractionController(URL:file.url)
letrect=self.tableView.rectForRowAtIndexPath(indexPath)
document.presentOpenInMenuFromRect(rect,inView:self.tableView,animated:true)
}
五、删除视频
funcdelete(file:File,indexPath:NSIndexPath){
do{
tryfileManager.removeItemAtPath(file.path)
videos.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:UITableViewRowAnimation.Automatic)
}catch{
}
}
六、保存到相册
funcsaveToCameraRoll(file:File,indexPath:NSIndexPath){
ifUIVideoAtPathIsCompatibleWithSavedPhotosAlbum(file.path){
UISaveVideoAtPathToSavedPhotosAlbum(file.path,self,"image:didFinishSavingWithError:contextInfo:",nil)
}else{
//savefaild
}
}
funcimage(image:UIImage,didFinishSavingWithErrorerror:NSErrorPointer,contextInfo:UnsafePointer<Void>){
iferror==nil{
//savesuccess
}else{
//savefaild
}
}
代码说明:
注意UISaveVideoAtPathToSavedPhotosAlbum的用法,后面Selector写得不对就会报错。
以上就是IOS8共享文件的实例代码,有需要的朋友可以参考下。