Explorar el Código

11111---未完成

huangxiaodong hace 11 meses
padre
commit
ba38ce92dc
Se han modificado 15 ficheros con 2147 adiciones y 31 borrados
  1. 74 0
      创维盒子/双子星云手机.xcodeproj/project.pbxproj
  2. 3 3
      创维盒子/双子星云手机/AppDelegate/SceneDelegate.m
  3. 15 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/NSURLSession+TYCorrectedResumeData.h
  4. 161 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/NSURLSession+TYCorrectedResumeData.m
  5. 72 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownLoadDataManager.h
  6. 611 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownLoadDataManager.m
  7. 93 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownLoadModel.h
  8. 126 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownLoadModel.m
  9. 21 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadDelegate.h
  10. 85 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadSessionManager.h
  11. 729 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadSessionManager.m
  12. 22 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadUtility.h
  13. 36 0
      创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadUtility.m
  14. 3 2
      创维盒子/双子星云手机/Class/Set/uploadFile/uploadFileManager/boxDownloadFileManager.h
  15. 96 26
      创维盒子/双子星云手机/Class/Set/uploadFile/uploadFileManager/boxDownloadFileManager.m

+ 74 - 0
创维盒子/双子星云手机.xcodeproj/project.pbxproj

@@ -393,6 +393,28 @@
 		6B34DC6D2BF2070D002DD1EF /* titleLabelReusableView.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B34DC6A2BF2070D002DD1EF /* titleLabelReusableView.h */; };
 		6B34DC6E2BF2070D002DD1EF /* titleLabelReusableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B34DC6B2BF2070D002DD1EF /* titleLabelReusableView.m */; };
 		6B34DC6F2BF2070D002DD1EF /* titleLabelReusableView.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B34DC6A2BF2070D002DD1EF /* titleLabelReusableView.h */; };
+		6B39583E2C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958322C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.h */; };
+		6B39583F2C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958322C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.h */; };
+		6B3958402C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3958332C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.m */; };
+		6B3958412C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3958332C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.m */; };
+		6B3958422C32942F003FC6C3 /* TYDownLoadDataManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958342C32942F003FC6C3 /* TYDownLoadDataManager.h */; };
+		6B3958432C32942F003FC6C3 /* TYDownLoadDataManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958342C32942F003FC6C3 /* TYDownLoadDataManager.h */; };
+		6B3958442C32942F003FC6C3 /* TYDownLoadDataManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3958352C32942F003FC6C3 /* TYDownLoadDataManager.m */; };
+		6B3958452C32942F003FC6C3 /* TYDownLoadDataManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3958352C32942F003FC6C3 /* TYDownLoadDataManager.m */; };
+		6B3958462C32942F003FC6C3 /* TYDownloadDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958362C32942F003FC6C3 /* TYDownloadDelegate.h */; };
+		6B3958472C32942F003FC6C3 /* TYDownloadDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958362C32942F003FC6C3 /* TYDownloadDelegate.h */; };
+		6B3958482C32942F003FC6C3 /* TYDownLoadModel.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958372C32942F003FC6C3 /* TYDownLoadModel.h */; };
+		6B3958492C32942F003FC6C3 /* TYDownLoadModel.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958372C32942F003FC6C3 /* TYDownLoadModel.h */; };
+		6B39584A2C32942F003FC6C3 /* TYDownLoadModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3958382C32942F003FC6C3 /* TYDownLoadModel.m */; };
+		6B39584B2C32942F003FC6C3 /* TYDownLoadModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3958382C32942F003FC6C3 /* TYDownLoadModel.m */; };
+		6B39584C2C32942F003FC6C3 /* TYDownloadSessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958392C32942F003FC6C3 /* TYDownloadSessionManager.h */; };
+		6B39584D2C32942F003FC6C3 /* TYDownloadSessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3958392C32942F003FC6C3 /* TYDownloadSessionManager.h */; };
+		6B39584E2C32942F003FC6C3 /* TYDownloadSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B39583A2C32942F003FC6C3 /* TYDownloadSessionManager.m */; };
+		6B39584F2C32942F003FC6C3 /* TYDownloadSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B39583A2C32942F003FC6C3 /* TYDownloadSessionManager.m */; };
+		6B3958502C32942F003FC6C3 /* TYDownloadUtility.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B39583B2C32942F003FC6C3 /* TYDownloadUtility.h */; };
+		6B3958512C32942F003FC6C3 /* TYDownloadUtility.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B39583B2C32942F003FC6C3 /* TYDownloadUtility.h */; };
+		6B3958522C32942F003FC6C3 /* TYDownloadUtility.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B39583C2C32942F003FC6C3 /* TYDownloadUtility.m */; };
+		6B3958532C32942F003FC6C3 /* TYDownloadUtility.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B39583C2C32942F003FC6C3 /* TYDownloadUtility.m */; };
 		6B3A4B152B6F69A500F74679 /* cloudPhoneCommonModel.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3A4B132B6F69A500F74679 /* cloudPhoneCommonModel.h */; };
 		6B3A4B162B6F69A500F74679 /* cloudPhoneCommonModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3A4B142B6F69A500F74679 /* cloudPhoneCommonModel.m */; };
 		6B3AD6842BF48DBA0096D6B7 /* shareRecordDetailsHWViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3AD6822BF48DBA0096D6B7 /* shareRecordDetailsHWViewController.h */; };
@@ -1523,6 +1545,17 @@
 		6B34DC652BF1FFB1002DD1EF /* imageCollectionViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = imageCollectionViewCell.m; sourceTree = "<group>"; };
 		6B34DC6A2BF2070D002DD1EF /* titleLabelReusableView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = titleLabelReusableView.h; sourceTree = "<group>"; };
 		6B34DC6B2BF2070D002DD1EF /* titleLabelReusableView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = titleLabelReusableView.m; sourceTree = "<group>"; };
+		6B3958322C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURLSession+TYCorrectedResumeData.h"; sourceTree = "<group>"; };
+		6B3958332C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURLSession+TYCorrectedResumeData.m"; sourceTree = "<group>"; };
+		6B3958342C32942F003FC6C3 /* TYDownLoadDataManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYDownLoadDataManager.h; sourceTree = "<group>"; };
+		6B3958352C32942F003FC6C3 /* TYDownLoadDataManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYDownLoadDataManager.m; sourceTree = "<group>"; };
+		6B3958362C32942F003FC6C3 /* TYDownloadDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYDownloadDelegate.h; sourceTree = "<group>"; };
+		6B3958372C32942F003FC6C3 /* TYDownLoadModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYDownLoadModel.h; sourceTree = "<group>"; };
+		6B3958382C32942F003FC6C3 /* TYDownLoadModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYDownLoadModel.m; sourceTree = "<group>"; };
+		6B3958392C32942F003FC6C3 /* TYDownloadSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYDownloadSessionManager.h; sourceTree = "<group>"; };
+		6B39583A2C32942F003FC6C3 /* TYDownloadSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYDownloadSessionManager.m; sourceTree = "<group>"; };
+		6B39583B2C32942F003FC6C3 /* TYDownloadUtility.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYDownloadUtility.h; sourceTree = "<group>"; };
+		6B39583C2C32942F003FC6C3 /* TYDownloadUtility.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYDownloadUtility.m; sourceTree = "<group>"; };
 		6B3A4B132B6F69A500F74679 /* cloudPhoneCommonModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cloudPhoneCommonModel.h; sourceTree = "<group>"; };
 		6B3A4B142B6F69A500F74679 /* cloudPhoneCommonModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = cloudPhoneCommonModel.m; sourceTree = "<group>"; };
 		6B3AD6822BF48DBA0096D6B7 /* shareRecordDetailsHWViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = shareRecordDetailsHWViewController.h; sourceTree = "<group>"; };
@@ -2399,6 +2432,7 @@
 		6B05815D2AFCD0A900D37290 /* uploadFile */ = {
 			isa = PBXGroup;
 			children = (
+				6B39583D2C32942F003FC6C3 /* TYDownloadManager */,
 				6B9B7A312C2D1776001879C3 /* mixDownloadManager */,
 				6B6ABED72BFF3A6600480BAC /* receiveView */,
 				6B5D401C2B4AA70C000965CF /* downLoadFile */,
@@ -2664,6 +2698,24 @@
 			path = view;
 			sourceTree = "<group>";
 		};
+		6B39583D2C32942F003FC6C3 /* TYDownloadManager */ = {
+			isa = PBXGroup;
+			children = (
+				6B3958322C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.h */,
+				6B3958332C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.m */,
+				6B3958342C32942F003FC6C3 /* TYDownLoadDataManager.h */,
+				6B3958352C32942F003FC6C3 /* TYDownLoadDataManager.m */,
+				6B3958362C32942F003FC6C3 /* TYDownloadDelegate.h */,
+				6B3958372C32942F003FC6C3 /* TYDownLoadModel.h */,
+				6B3958382C32942F003FC6C3 /* TYDownLoadModel.m */,
+				6B3958392C32942F003FC6C3 /* TYDownloadSessionManager.h */,
+				6B39583A2C32942F003FC6C3 /* TYDownloadSessionManager.m */,
+				6B39583B2C32942F003FC6C3 /* TYDownloadUtility.h */,
+				6B39583C2C32942F003FC6C3 /* TYDownloadUtility.m */,
+			);
+			path = TYDownloadManager;
+			sourceTree = "<group>";
+		};
 		6B3F96312AD2A0FA008E349E /* netWork */ = {
 			isa = PBXGroup;
 			children = (
@@ -3346,6 +3398,7 @@
 				6B93548F2BF30A8C00AA8D31 /* editTypeBottomView.h in Headers */,
 				6B2C1E862C070ADE00FDCF82 /* ZFPlayerConst.h in Headers */,
 				6BFF26992AFA16DA00FCB4F7 /* forgetSecretKeyViewController.h in Headers */,
+				6B3958422C32942F003FC6C3 /* TYDownLoadDataManager.h in Headers */,
 				6B45C4FD2B57BC87007E6911 /* cloudPhoneExtraFileListModel.h in Headers */,
 				6B2C1E642C070ADE00FDCF82 /* ZFLandscapeRotationManager_iOS15.h in Headers */,
 				6BFF26912AF9D88200FCB4F7 /* AuthCodeTextField.h in Headers */,
@@ -3423,8 +3476,10 @@
 				18FCE53E2AD136670020F623 /* PlayerViewController+AdjustBtnFrame.h in Headers */,
 				6B5BE5812BFC81E300A1B9B3 /* NASFileAndFolderModel.h in Headers */,
 				6B7EA7BC2BF759AC002D5CC2 /* shareSecretTableViewCell.h in Headers */,
+				6B39584C2C32942F003FC6C3 /* TYDownloadSessionManager.h in Headers */,
 				6B2C1E362C070ADE00FDCF82 /* ZFPlayerControlView.h in Headers */,
 				6BA08F472C01D05B00A2242D /* CWFileManager.h in Headers */,
+				6B3958502C32942F003FC6C3 /* TYDownloadUtility.h in Headers */,
 				6B05815B2AFC851E00D37290 /* mySetHeadView.h in Headers */,
 				6BF52CAD2AD82EA500A617DB /* PlayerControlButTipView.h in Headers */,
 				6B9B7A3A2C2D37C7001879C3 /* mixDownloadCacheManager.h in Headers */,
@@ -3433,6 +3488,7 @@
 				6B4D23B12B33DDBC005B718A /* imageVersionRenewTipView.h in Headers */,
 				6B0582892B045E0B00D37290 /* uploadFileDataModel.h in Headers */,
 				6BA08F082C0187DD00A2242D /* boxDownloadFileRecordCell.h in Headers */,
+				6B3958482C32942F003FC6C3 /* TYDownLoadModel.h in Headers */,
 				6BA08F572C01D05B00A2242D /* CWUploadTask+CheckInfo.h in Headers */,
 				6BF52C972AD5546500A617DB /* SimplePing.h in Headers */,
 				6B3F96342AD2A15A008E349E /* netWorkManager.h in Headers */,
@@ -3475,6 +3531,7 @@
 				6B2C1E702C070ADE00FDCF82 /* ZFLandscapeViewController_iOS15.h in Headers */,
 				6BED888E2B4E901900F76DDC /* downloadFileRecordCell.h in Headers */,
 				184C8ACC2A94557200F26650 /* HidenMaskSetViewController.h in Headers */,
+				6B39583E2C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.h in Headers */,
 				6B6ABEF22BFF6B4C00480BAC /* boxSaveFileManager.h in Headers */,
 				6BA08F532C01D05B00A2242D /* CWUploadTask.h in Headers */,
 				6B2C1E682C070ADE00FDCF82 /* ZFLandscapeRotationManager_iOS16.h in Headers */,
@@ -3519,6 +3576,7 @@
 				6B2C1E282C070ADE00FDCF82 /* ZFLandScapeControlView.h in Headers */,
 				6B3A4B152B6F69A500F74679 /* cloudPhoneCommonModel.h in Headers */,
 				6B05819F2AFCF4E800D37290 /* AJPhotoListView.h in Headers */,
+				6B3958462C32942F003FC6C3 /* TYDownloadDelegate.h in Headers */,
 				6B9354A72BF3848D00AA8D31 /* shareRecordViewController.h in Headers */,
 				18963B462AB84398006E7C50 /* GuideView.h in Headers */,
 				6B1FBD322B455DBD00926382 /* PlayerViewController+upLoadFile.h in Headers */,
@@ -3573,6 +3631,7 @@
 				6B9354902BF30A8C00AA8D31 /* editTypeBottomView.h in Headers */,
 				6B2C1E872C070ADE00FDCF82 /* ZFPlayerConst.h in Headers */,
 				6BD5069F2B9576A4006E7CB0 /* forgetSecretKeyViewController.h in Headers */,
+				6B3958432C32942F003FC6C3 /* TYDownLoadDataManager.h in Headers */,
 				6BD506A02B9576A4006E7CB0 /* cloudPhoneExtraFileListModel.h in Headers */,
 				6B2C1E652C070ADE00FDCF82 /* ZFLandscapeRotationManager_iOS15.h in Headers */,
 				6BD506A12B9576A4006E7CB0 /* AuthCodeTextField.h in Headers */,
@@ -3650,8 +3709,10 @@
 				6BD506CB2B9576A4006E7CB0 /* PlayerViewController+AdjustBtnFrame.h in Headers */,
 				6B5BE5832BFC81E300A1B9B3 /* NASFileAndFolderModel.h in Headers */,
 				6B7EA7BE2BF759AC002D5CC2 /* shareSecretTableViewCell.h in Headers */,
+				6B39584D2C32942F003FC6C3 /* TYDownloadSessionManager.h in Headers */,
 				6B2C1E372C070ADE00FDCF82 /* ZFPlayerControlView.h in Headers */,
 				6BA08F482C01D05B00A2242D /* CWFileManager.h in Headers */,
+				6B3958512C32942F003FC6C3 /* TYDownloadUtility.h in Headers */,
 				6BD506CC2B9576A4006E7CB0 /* mySetHeadView.h in Headers */,
 				6BD506CD2B9576A4006E7CB0 /* PlayerControlButTipView.h in Headers */,
 				6B9B7A3D2C2D37C7001879C3 /* mixDownloadCacheManager.h in Headers */,
@@ -3660,6 +3721,7 @@
 				6BD506D02B9576A4006E7CB0 /* imageVersionRenewTipView.h in Headers */,
 				6BD506D12B9576A4006E7CB0 /* uploadFileDataModel.h in Headers */,
 				6BA08F0A2C0187DD00A2242D /* boxDownloadFileRecordCell.h in Headers */,
+				6B3958492C32942F003FC6C3 /* TYDownLoadModel.h in Headers */,
 				6BA08F582C01D05B00A2242D /* CWUploadTask+CheckInfo.h in Headers */,
 				6BD506D22B9576A4006E7CB0 /* SimplePing.h in Headers */,
 				6BD506D32B9576A4006E7CB0 /* netWorkManager.h in Headers */,
@@ -3702,6 +3764,7 @@
 				6B2C1E712C070ADE00FDCF82 /* ZFLandscapeViewController_iOS15.h in Headers */,
 				6BD506E92B9576A4006E7CB0 /* downloadFileRecordCell.h in Headers */,
 				6BD506EA2B9576A4006E7CB0 /* HidenMaskSetViewController.h in Headers */,
+				6B39583F2C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.h in Headers */,
 				6B6ABEF42BFF6B4C00480BAC /* boxSaveFileManager.h in Headers */,
 				6BA08F542C01D05B00A2242D /* CWUploadTask.h in Headers */,
 				6B2C1E692C070ADE00FDCF82 /* ZFLandscapeRotationManager_iOS16.h in Headers */,
@@ -3746,6 +3809,7 @@
 				6B2C1E292C070ADE00FDCF82 /* ZFLandScapeControlView.h in Headers */,
 				6BD506F92B9576A4006E7CB0 /* cloudPhoneCommonModel.h in Headers */,
 				6BD506FA2B9576A4006E7CB0 /* AJPhotoListView.h in Headers */,
+				6B3958472C32942F003FC6C3 /* TYDownloadDelegate.h in Headers */,
 				6B9354A82BF3848D00AA8D31 /* shareRecordViewController.h in Headers */,
 				6BD506FB2B9576A4006E7CB0 /* GuideView.h in Headers */,
 				6BD506FC2B9576A4006E7CB0 /* PlayerViewController+upLoadFile.h in Headers */,
@@ -4190,6 +4254,7 @@
 				6BD5071E2B9576A4006E7CB0 /* iTools.m in Sources */,
 				6BD5071F2B9576A4006E7CB0 /* SetPWDSecondViewController.m in Sources */,
 				6BD507202B9576A4006E7CB0 /* NSArray+JSONModel.m in Sources */,
+				6B39584F2C32942F003FC6C3 /* TYDownloadSessionManager.m in Sources */,
 				6BD507212B9576A4006E7CB0 /* HWAddBookmarkViewController.m in Sources */,
 				6BD507222B9576A4006E7CB0 /* AuthCodeTextField.m in Sources */,
 				6BA08F5A2C01D05B00A2242D /* CWUploadTask+CheckInfo.m in Sources */,
@@ -4216,6 +4281,7 @@
 				6BD5072E2B9576A4006E7CB0 /* HWToolListCell.m in Sources */,
 				6BD5072F2B9576A4006E7CB0 /* cloudPhoneExtraFileListModel.m in Sources */,
 				6B2C1E7F2C070ADE00FDCF82 /* ZFOrientationObserver.m in Sources */,
+				6B3958412C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.m in Sources */,
 				6BD507302B9576A4006E7CB0 /* UIActivityIndicatorView+AFNetworking.m in Sources */,
 				6BD507312B9576A4006E7CB0 /* BaseViewController.m in Sources */,
 				6B63179F2BFD8CA000FF4FB4 /* videoPlayByAVPlayerViewController.m in Sources */,
@@ -4242,6 +4308,7 @@
 				6BD507402B9576A4006E7CB0 /* DDYLanguageTool.m in Sources */,
 				6B7EA7A12BF5E0ED002D5CC2 /* NASMsgModel.m in Sources */,
 				6BD507412B9576A4006E7CB0 /* AudioSessionObject.m in Sources */,
+				6B3958532C32942F003FC6C3 /* TYDownloadUtility.m in Sources */,
 				6B6ABEDD2BFF3AA400480BAC /* receiveView.m in Sources */,
 				6BD507422B9576A4006E7CB0 /* ChangeLogoViewController.m in Sources */,
 				6BD507432B9576A4006E7CB0 /* uploadFileRecordheadView.m in Sources */,
@@ -4270,6 +4337,7 @@
 				6BD507522B9576A4006E7CB0 /* diskListTableCell.m in Sources */,
 				6BD507532B9576A4006E7CB0 /* HWBookmarkViewController.m in Sources */,
 				6B2C1E832C070ADE00FDCF82 /* ZFPersentInteractiveTransition.m in Sources */,
+				6B3958452C32942F003FC6C3 /* TYDownLoadDataManager.m in Sources */,
 				6BD507542B9576A4006E7CB0 /* AFURLRequestSerialization.m in Sources */,
 				6BD507552B9576A4006E7CB0 /* AJPhotoGroupCell.m in Sources */,
 				6BD507562B9576A4006E7CB0 /* HaveNewVersionView.m in Sources */,
@@ -4279,6 +4347,7 @@
 				6BD507592B9576A4006E7CB0 /* Const.m in Sources */,
 				6BD5075A2B9576A4006E7CB0 /* PlayerViewController+backupsFile.m in Sources */,
 				6B2C1E332C070ADE00FDCF82 /* ZFNetworkSpeedMonitor.m in Sources */,
+				6B39584B2C32942F003FC6C3 /* TYDownLoadModel.m in Sources */,
 				6BD5075B2B9576A4006E7CB0 /* couldphoneSysInfoModel.m in Sources */,
 				6B2C1E512C070ADE00FDCF82 /* ZFUtilities.m in Sources */,
 				6BD5075C2B9576A4006E7CB0 /* BGTool.m in Sources */,
@@ -4499,6 +4568,7 @@
 				A003F6D127D8425C00715CBF /* iTools.m in Sources */,
 				18EC79DD2ABC109F0084836A /* SetPWDSecondViewController.m in Sources */,
 				183AE6012A89CE3C00B11CB0 /* NSArray+JSONModel.m in Sources */,
+				6B39584E2C32942F003FC6C3 /* TYDownloadSessionManager.m in Sources */,
 				A00221DB27EAFF4A00E45F78 /* HWAddBookmarkViewController.m in Sources */,
 				6BFF26922AF9D88200FCB4F7 /* AuthCodeTextField.m in Sources */,
 				6BA08F592C01D05B00A2242D /* CWUploadTask+CheckInfo.m in Sources */,
@@ -4525,6 +4595,7 @@
 				A084D68E27E859D300054880 /* HWToolListCell.m in Sources */,
 				6B45C4FE2B57BC87007E6911 /* cloudPhoneExtraFileListModel.m in Sources */,
 				6B2C1E7E2C070ADE00FDCF82 /* ZFOrientationObserver.m in Sources */,
+				6B3958402C32942F003FC6C3 /* NSURLSession+TYCorrectedResumeData.m in Sources */,
 				183AE6512A8A2CF000B11CB0 /* UIActivityIndicatorView+AFNetworking.m in Sources */,
 				A003F6A627D841C800715CBF /* BaseViewController.m in Sources */,
 				6B63179C2BFD8CA000FF4FB4 /* videoPlayByAVPlayerViewController.m in Sources */,
@@ -4551,6 +4622,7 @@
 				6BBB35E32AE935C000E4A59B /* DDYLanguageTool.m in Sources */,
 				6B7EA79F2BF5E0ED002D5CC2 /* NASMsgModel.m in Sources */,
 				6BBB35DE2AE76AD800E4A59B /* AudioSessionObject.m in Sources */,
+				6B3958522C32942F003FC6C3 /* TYDownloadUtility.m in Sources */,
 				6B6ABEDB2BFF3AA400480BAC /* receiveView.m in Sources */,
 				181E84FC2A8F57A500292B96 /* ChangeLogoViewController.m in Sources */,
 				6B8FF6BA2B04E19600800981 /* uploadFileRecordheadView.m in Sources */,
@@ -4579,6 +4651,7 @@
 				6B45C5092B5FA2DD007E6911 /* diskListTableCell.m in Sources */,
 				A031DD6827EC195400909527 /* HWBookmarkViewController.m in Sources */,
 				6B2C1E822C070ADE00FDCF82 /* ZFPersentInteractiveTransition.m in Sources */,
+				6B3958442C32942F003FC6C3 /* TYDownLoadDataManager.m in Sources */,
 				183AE6572A8A2CF000B11CB0 /* AFURLRequestSerialization.m in Sources */,
 				6B0581AC2AFCF57300D37290 /* AJPhotoGroupCell.m in Sources */,
 				184C8AD92A949DAC00F26650 /* HaveNewVersionView.m in Sources */,
@@ -4588,6 +4661,7 @@
 				A003F6E227D8428200715CBF /* Const.m in Sources */,
 				6B1FBD372B455DD600926382 /* PlayerViewController+backupsFile.m in Sources */,
 				6B2C1E322C070ADE00FDCF82 /* ZFNetworkSpeedMonitor.m in Sources */,
+				6B39584A2C32942F003FC6C3 /* TYDownLoadModel.m in Sources */,
 				6B21708D2B14BDAA00656670 /* couldphoneSysInfoModel.m in Sources */,
 				6B2C1E502C070ADE00FDCF82 /* ZFUtilities.m in Sources */,
 				A08A94FE27E9A4E400C544BB /* BGTool.m in Sources */,

+ 3 - 3
创维盒子/双子星云手机/AppDelegate/SceneDelegate.m

@@ -36,7 +36,7 @@
 #import "queryOneShareModel.h"
 #import "boxSaveFileManager.h"
 #import "boxDownloadFileManager.h"
-#import "mixDownloadManager.h"
+//#import "mixDownloadManager.h"
 
 @interface SceneDelegate ()<SetUsePWDViewControllerDelegate,GuideViewControllerDelegate>
 @property(nonatomic, strong)   CalculatorViewController *calculatorVC;
@@ -791,7 +791,7 @@
     [[backupsFileManager shareInstance] suspendBackupsFileFun];
     
     //frp的下载
-    [[mixDownloadManager shareManager] suspendAllDownloadTask];
+    //[[mixDownloadManager shareManager] suspendAllDownloadTask];
     
     NSString *uid =[connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
     if(uid){
@@ -801,7 +801,7 @@
         uid = @"";
     }
     
-    [mixDownloadManager shareManager].uid = uid;
+    //[mixDownloadManager shareManager].uid = uid;
 }
 
 #pragma mark 上报版本号信息

+ 15 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/NSURLSession+TYCorrectedResumeData.h

@@ -0,0 +1,15 @@
+//
+//  NSURLSession+TYCorrectedResumeData.h
+//  TYDownloadManagerDemo
+//
+//  Created by tanyang on 2016/10/7.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSURLSession (TYCorrectedResumeData)
+
+- (NSURLSessionDownloadTask *)downloadTaskWithCorrectResumeData:(NSData *)resumeData;
+
+@end

+ 161 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/NSURLSession+TYCorrectedResumeData.m

@@ -0,0 +1,161 @@
+//
+//  NSURLSession+TYCorrectedResumeData.m
+//  TYDownloadManagerDemo
+//
+//  Created by tanyang on 2016/10/7.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import "NSURLSession+TYCorrectedResumeData.h"
+#import <UIKit/UIKit.h>
+
+#define IS_IOS10ORLATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10)
+#define IS_IOS11ORLATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11)
+#define IS_IOS12ORLATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 12)
+
+@implementation NSURLSession (TYCorrectedResumeData)
+
+- (NSURLSessionDownloadTask *)downloadTaskWithCorrectResumeData:(NSData *)resumeData {
+    NSString *kResumeCurrentRequest = @"NSURLSessionResumeCurrentRequest";
+    NSString *kResumeOriginalRequest = @"NSURLSessionResumeOriginalRequest";
+    
+    if (IS_IOS11ORLATER)
+    {
+        NSString * dataStr = [[NSString alloc]initWithData:resumeData encoding:NSUTF8StringEncoding];
+        NSString * newStr = [self cleanResumeDataWithString:dataStr];
+        resumeData = [newStr dataUsingEncoding:NSUTF8StringEncoding];
+    }
+    
+    NSData *cData = correctResumeData(resumeData);
+    cData = cData?cData:resumeData;
+    NSURLSessionDownloadTask *task = [self downloadTaskWithResumeData:cData];
+    NSMutableDictionary *resumeDic = getResumeDictionary(cData);
+    if (resumeDic) {
+        if (task.originalRequest == nil) {
+            NSData *originalReqData = resumeDic[kResumeOriginalRequest];
+            NSURLRequest *originalRequest = [NSKeyedUnarchiver unarchiveObjectWithData:originalReqData ];
+            if (originalRequest) {
+                [task setValue:originalRequest forKey:@"originalRequest"];
+            }
+        }
+        if (task.currentRequest == nil) {
+            NSData *currentReqData = resumeDic[kResumeCurrentRequest];
+            NSURLRequest *currentRequest = [NSKeyedUnarchiver unarchiveObjectWithData:currentReqData];
+            if (currentRequest) {
+                [task setValue:currentRequest forKey:@"currentRequest"];
+            }
+        }
+        
+    }
+    return task;
+}
+
+#pragma mark- private
+
+- (NSString *)cleanResumeDataWithString:(NSString *)dataStr
+{
+    if([dataStr containsString:@"<key>NSURLSessionResumeByteRange</key>"])
+    {
+        NSRange rangeKey  = [dataStr rangeOfString:@"<key>NSURLSessionResumeByteRange</key>"];
+        NSString * headStr = [dataStr substringToIndex:rangeKey.location];
+        NSString * backStr = [dataStr substringFromIndex:rangeKey.location];
+        NSRange rangeValue = [backStr rangeOfString:@"</string>\n\t"];
+        NSString * tailStr = [backStr substringFromIndex:rangeValue.location + rangeValue.length];
+        dataStr = [headStr stringByAppendingString:tailStr];
+    }
+    return dataStr;
+}
+
+
+NSData * correctRequestData(NSData *data) {
+    if (!data) {
+        return nil;
+    }
+    // return the same data if it's correct
+    if ([NSKeyedUnarchiver unarchiveObjectWithData:data] != nil) {
+        return data;
+    }
+    NSMutableDictionary *archive = [[NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainersAndLeaves format:nil error:nil] mutableCopy];
+    
+    if (!archive) {
+        return nil;
+    }
+    NSInteger k = 0;
+    id objectss = archive[@"$objects"];
+    while ([objectss[1] objectForKey:[NSString stringWithFormat:@"$%ld",k]] != nil) {
+        k += 1;
+    }
+    NSInteger i = 0;
+    while ([archive[@"$objects"][1] objectForKey:[NSString stringWithFormat:@"__nsurlrequest_proto_prop_obj_%ld",i]] != nil) {
+        NSMutableArray *arr = archive[@"$objects"];
+        NSMutableDictionary *dic = arr[1];
+        id obj = [dic objectForKey:[NSString stringWithFormat:@"__nsurlrequest_proto_prop_obj_%ld",i]];
+        if (obj) {
+            [dic setValue:obj forKey:[NSString stringWithFormat:@"$%ld",i+k]];
+            [dic removeObjectForKey:[NSString stringWithFormat:@"__nsurlrequest_proto_prop_obj_%ld",i]];
+            [arr replaceObjectAtIndex:1 withObject:dic];
+            archive[@"$objects"] = arr;
+        }
+        i++;
+    }
+    if ([archive[@"$objects"][1] objectForKey:@"__nsurlrequest_proto_props"] != nil) {
+        NSMutableArray *arr = archive[@"$objects"];
+        NSMutableDictionary *dic = arr[1];
+        id obj = [dic objectForKey:@"__nsurlrequest_proto_props"];
+        if (obj) {
+            [dic setValue:obj forKey:[NSString stringWithFormat:@"$%ld",i+k]];
+            [dic removeObjectForKey:@"__nsurlrequest_proto_props"];
+            [arr replaceObjectAtIndex:1 withObject:dic];
+            archive[@"$objects"] = arr;
+        }
+    }
+    // Rectify weird "NSKeyedArchiveRootObjectKey" top key to NSKeyedArchiveRootObjectKey = "root"
+    if ([archive[@"$top"] objectForKey:@"NSKeyedArchiveRootObjectKey"] != nil) {
+        [archive[@"$top"] setObject:archive[@"$top"][@"NSKeyedArchiveRootObjectKey"] forKey: NSKeyedArchiveRootObjectKey];
+        [archive[@"$top"] removeObjectForKey:@"NSKeyedArchiveRootObjectKey"];
+    }
+    // Reencode archived object
+    NSData *result = [NSPropertyListSerialization dataWithPropertyList:archive format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
+    return result;
+}
+
+NSMutableDictionary *getResumeDictionary(NSData *data) {
+    NSMutableDictionary *iresumeDictionary = nil;
+    if (IS_IOS10ORLATER) {
+        id root = nil;
+        id  keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
+        @try {
+            root = [keyedUnarchiver decodeTopLevelObjectForKey:@"NSKeyedArchiveRootObjectKey" error:nil];
+            if (root == nil) {
+                root = [keyedUnarchiver decodeTopLevelObjectForKey:NSKeyedArchiveRootObjectKey error:nil];
+            }
+        } @catch(NSException *exception) {
+            
+        }
+        [keyedUnarchiver finishDecoding];
+        iresumeDictionary = [root mutableCopy];
+    }
+    
+    if (iresumeDictionary == nil) {
+        iresumeDictionary = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainersAndLeaves format:nil error:nil];
+    }
+    return iresumeDictionary;
+}
+
+NSData *correctResumeData(NSData *data) {
+    NSString *kResumeCurrentRequest = @"NSURLSessionResumeCurrentRequest";
+    NSString *kResumeOriginalRequest = @"NSURLSessionResumeOriginalRequest";
+    if (data == nil) {
+        return  nil;
+    }
+    NSMutableDictionary *resumeDictionary = getResumeDictionary(data);
+    if (resumeDictionary == nil) {
+        return nil;
+    }
+    resumeDictionary[kResumeCurrentRequest] = correctRequestData(resumeDictionary[kResumeCurrentRequest]);
+    resumeDictionary[kResumeOriginalRequest] = correctRequestData(resumeDictionary[kResumeOriginalRequest]);
+    NSData *result = [NSPropertyListSerialization dataWithPropertyList:resumeDictionary format:NSPropertyListXMLFormat_v1_0 options:0 error:nil];
+    return result;
+}
+
+@end

+ 72 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownLoadDataManager.h

@@ -0,0 +1,72 @@
+//
+//  TYDownLoadDataManager.h
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/12.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "TYDownLoadModel.h"
+#import "TYDownloadDelegate.h"
+
+/**
+ *  下载管理类 封装NSURLSessionDataTask
+ */
+@interface TYDownLoadDataManager : NSObject <NSURLSessionDataDelegate>
+
+// 下载代理
+@property (nonatomic,weak) id<TYDownloadDelegate> delegate;
+
+// 等待中的模型 只读
+@property (nonatomic, strong,readonly) NSMutableArray *waitingDownloadModels;
+
+// 下载中的模型 只读
+@property (nonatomic, strong,readonly) NSMutableArray *downloadingModels;
+
+// 最大下载数
+@property (nonatomic, assign) NSInteger maxDownloadCount;
+
+// 等待下载队列 先进先出 默认YES, 当NO时,先进后出
+@property (nonatomic, assign) BOOL resumeDownloadFIFO;
+
+// 全部并发 默认NO, 当YES时,忽略maxDownloadCount
+@property (nonatomic, assign) BOOL isBatchDownload;
+
+// 单例
++ (TYDownLoadDataManager *)manager;
+
+// 开始下载
+- (TYDownloadModel *)startDownloadURLString:(NSString *)URLString toDestinationPath:(NSString *)destinationPath progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state;
+
+// 开始下载
+- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state;
+
+// 开始下载
+- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 恢复下载(除非确定对这个model进行了suspend,否则使用start)
+- (void)resumeWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 暂停下载
+- (void)suspendWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 取消下载
+- (void)cancleWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 删除下载
+- (void)deleteFileWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 删除下载
+- (void)deleteAllFileWithDownloadDirectory:(NSString *)downloadDirectory;
+
+// 获取正在下载模型
+- (TYDownloadModel *)downLoadingModelForURLString:(NSString *)URLString;
+
+// 获取本地下载模型的进度
+- (TYDownloadProgress *)progessWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 是否已经下载
+- (BOOL)isDownloadCompletedWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+@end

+ 611 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownLoadDataManager.m

@@ -0,0 +1,611 @@
+//
+//  TYDownLoadDataManager.m
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/12.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import "TYDownLoadDataManager.h"
+
+/**
+ *  下载模型
+ */
+@interface TYDownloadModel ()
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  task info
+// 下载状态
+@property (nonatomic, assign) TYDownloadState state;
+// 下载任务
+@property (nonatomic, strong) NSURLSessionDataTask *task;
+// 文件流
+@property (nonatomic, strong) NSOutputStream *stream;
+// 下载文件路径
+@property (nonatomic, strong) NSString *filePath;
+// 下载时间
+@property (nonatomic, strong) NSDate *downloadDate;
+// 手动取消当做暂停
+@property (nonatomic, assign) BOOL manualCancle;
+
+@end
+
+/**
+ *  下载进度
+ */
+@interface TYDownloadProgress ()
+// 续传大小
+@property (nonatomic, assign) int64_t resumeBytesWritten;
+// 这次写入的数量
+@property (nonatomic, assign) int64_t bytesWritten;
+// 已下载的数量
+@property (nonatomic, assign) int64_t totalBytesWritten;
+// 文件的总大小
+@property (nonatomic, assign) int64_t totalBytesExpectedToWrite;
+// 下载进度
+@property (nonatomic, assign) float progress;
+// 下载速度
+@property (nonatomic, assign) float speed;
+// 下载剩余时间
+@property (nonatomic, assign) int remainingTime;
+
+@end
+
+
+@interface TYDownLoadDataManager ()
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  file info
+// 文件管理
+@property (nonatomic, strong) NSFileManager *fileManager;
+// 缓存文件目录
+@property (nonatomic, strong) NSString *downloadDirectory;
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  session info
+// 下载seesion会话
+@property (nonatomic, strong) NSURLSession *session;
+// 下载模型字典 key = url
+@property (nonatomic, strong) NSMutableDictionary *downloadingModelDic;
+// 下载中的模型
+@property (nonatomic, strong) NSMutableArray *waitingDownloadModels;
+// 等待中的模型
+@property (nonatomic, strong) NSMutableArray *downloadingModels;
+// 回调代理的队列
+@property (strong, nonatomic) NSOperationQueue *queue;
+
+@end
+
+@implementation TYDownLoadDataManager
+
+#pragma mark - getter
+
++ (TYDownLoadDataManager *)manager
+{
+    static id sharedInstance = nil;
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        sharedInstance = [[self alloc] init];
+    });
+    return sharedInstance;
+}
+
+- (instancetype)init
+{
+    if (self = [super init]) {
+        _maxDownloadCount = 1;
+        _resumeDownloadFIFO = YES;
+        _isBatchDownload = NO;
+    }
+    return self;
+}
+
+- (NSFileManager *)fileManager
+{
+    if (!_fileManager) {
+        _fileManager = [[NSFileManager alloc]init];
+    }
+    return _fileManager;
+}
+
+- (NSURLSession *)session
+{
+    if (!_session) {
+        _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:self.queue];
+    }
+    return _session;
+}
+
+- (NSOperationQueue *)queue
+{
+    if (!_queue) {
+        _queue = [[NSOperationQueue alloc]init];
+        _queue.maxConcurrentOperationCount = 1;
+    }
+    return _queue;
+}
+
+- (NSString *)downloadDirectory
+{
+    if (!_downloadDirectory) {
+        _downloadDirectory = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"TYDownloadCache"];
+        [self createDirectory:_downloadDirectory];
+    }
+    return _downloadDirectory;
+}
+
+// 下载文件信息plist路径
+- (NSString *)fileSizePathWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    return [downloadModel.downloadDirectory stringByAppendingPathComponent:@"downloadsFileSize.plist"];
+}
+
+// 下载model字典
+- (NSMutableDictionary *)downloadingModelDic
+{
+    if (!_downloadingModelDic) {
+        _downloadingModelDic = [NSMutableDictionary dictionary];
+    }
+    return _downloadingModelDic;
+}
+
+// 等待下载model队列
+- (NSMutableArray *)waitingDownloadModels
+{
+    if (!_waitingDownloadModels) {
+        _waitingDownloadModels = [NSMutableArray array];
+    }
+    return _waitingDownloadModels;
+}
+
+// 正在下载model队列
+- (NSMutableArray *)downloadingModels
+{
+    if (!_downloadingModels) {
+        _downloadingModels = [NSMutableArray array];
+    }
+    return _downloadingModels;
+}
+
+
+#pragma mark - downlaod
+
+// 开始下载
+- (TYDownloadModel *)startDownloadURLString:(NSString *)URLString toDestinationPath:(NSString *)destinationPath progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state
+{
+    // 验证下载地址
+    if (!URLString) {
+        NSLog(@"dwonloadURL can't nil");
+        return nil;
+    }
+    
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:URLString];
+    
+    if (!downloadModel || ![downloadModel.filePath isEqualToString:destinationPath]) {
+        downloadModel = [[TYDownloadModel alloc]initWithURLString:URLString filePath:destinationPath];
+    }
+    
+    [self startWithDownloadModel:downloadModel progress:progress state:state];
+    
+    return downloadModel;
+}
+
+- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state
+{
+    downloadModel.progressBlock = progress;
+    downloadModel.stateBlock = state;
+    
+    [self startWithDownloadModel:downloadModel];
+}
+
+- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel) {
+        return;
+    }
+    
+    if (downloadModel.state == TYDownloadStateReadying) {
+        [self downloadModel:downloadModel didChangeState:TYDownloadStateReadying filePath:nil error:nil];
+        return;
+    }
+    
+    // 验证是否已经下载文件
+    if ([self isDownloadCompletedWithDownloadModel:downloadModel]) {
+        downloadModel.state = TYDownloadStateCompleted;
+        [self downloadModel:downloadModel didChangeState:TYDownloadStateCompleted filePath:downloadModel.filePath error:nil];
+        return;
+    }
+    
+    // 验证是否存在
+    if (downloadModel.task && downloadModel.task.state == NSURLSessionTaskStateRunning) {
+        downloadModel.state = TYDownloadStateRunning;
+        [self downloadModel:downloadModel didChangeState:TYDownloadStateRunning filePath:nil error:nil];
+        return;
+    }
+    
+    [self resumeWithDownloadModel:downloadModel];
+}
+
+// 自动下载下一个等待队列任务
+- (void)willResumeNextWithDowloadModel:(TYDownloadModel *)downloadModel
+{
+    if (_isBatchDownload) {
+        return;
+    }
+    
+    @synchronized (self) {
+        [self.downloadingModels removeObject:downloadModel];
+        // 还有未下载的
+        if (self.waitingDownloadModels.count > 0) {
+            [self resumeWithDownloadModel:_resumeDownloadFIFO ? self.waitingDownloadModels.firstObject:self.waitingDownloadModels.lastObject];
+        }
+    }
+}
+
+// 是否开启下载等待队列任务
+- (BOOL)canResumeDownlaodModel:(TYDownloadModel *)downloadModel
+{
+    if (_isBatchDownload) {
+        return YES;
+    }
+    
+    @synchronized (self) {
+        if (self.downloadingModels.count >= _maxDownloadCount ) {
+            if ([self.waitingDownloadModels indexOfObject:downloadModel] == NSNotFound) {
+                [self.waitingDownloadModels addObject:downloadModel];
+                self.downloadingModelDic[downloadModel.downloadURL] = downloadModel;
+            }
+            downloadModel.state = TYDownloadStateReadying;
+            [self downloadModel:downloadModel didChangeState:TYDownloadStateReadying filePath:nil error:nil];
+            return NO;
+        }
+        
+        if ([self.waitingDownloadModels indexOfObject:downloadModel] != NSNotFound) {
+            [self.waitingDownloadModels removeObject:downloadModel];
+        }
+        
+        if ([self.downloadingModels indexOfObject:downloadModel] == NSNotFound) {
+            [self.downloadingModels addObject:downloadModel];
+        }
+        return YES;
+    }
+}
+
+// 恢复下载
+- (void)resumeWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel) {
+        return;
+    }
+    
+    if (![self canResumeDownlaodModel:downloadModel]) {
+        return;
+    }
+    
+    // 如果task 不存在 或者 取消了
+    if (!downloadModel.task || downloadModel.task.state == NSURLSessionTaskStateCanceling) {
+        NSString *URLString = downloadModel.downloadURL;
+        
+        // 创建请求
+        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
+        
+        // 设置请求头
+        NSString *range = [NSString stringWithFormat:@"bytes=%zd-", [self fileSizeWithDownloadModel:downloadModel]];
+        [request setValue:range forHTTPHeaderField:@"Range"];
+        
+        // 创建流
+        downloadModel.stream = [NSOutputStream outputStreamToFileAtPath:downloadModel.filePath append:YES];
+        
+        downloadModel.downloadDate = [NSDate date];
+        self.downloadingModelDic[downloadModel.downloadURL] = downloadModel;
+        // 创建一个Data任务
+        downloadModel.task = [self.session dataTaskWithRequest:request];
+        downloadModel.task.taskDescription = URLString;
+    }
+    
+    [downloadModel.task resume];
+    
+    downloadModel.state = TYDownloadStateRunning;
+    [self downloadModel:downloadModel didChangeState:TYDownloadStateRunning filePath:nil error:nil];
+}
+
+// 暂停下载
+- (void)suspendWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel.manualCancle) {
+        downloadModel.manualCancle = YES;
+        [downloadModel.task cancel];
+    }
+}
+
+// 取消下载
+- (void)cancleWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel.task && downloadModel.state == TYDownloadStateReadying) {
+        [self removeDownLoadingModelForURLString:downloadModel.downloadURL];
+        @synchronized (self) {
+            [self.waitingDownloadModels removeObject:downloadModel];
+        }
+        downloadModel.state = TYDownloadStateNone;
+        [self downloadModel:downloadModel didChangeState:TYDownloadStateNone filePath:nil error:nil];
+        return;
+    }
+    
+    if (downloadModel.state != TYDownloadStateCompleted && downloadModel.state != TYDownloadStateFailed){
+        [downloadModel.task cancel];
+    }
+}
+
+#pragma mark - delete file
+
+- (void)deleteFileWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel || !downloadModel.filePath) {
+        return;
+    }
+    
+    // 文件是否存在
+    if ([self.fileManager fileExistsAtPath:downloadModel.filePath]) {
+        
+        // 删除任务
+        downloadModel.task.taskDescription = nil;
+        [downloadModel.task cancel];
+        downloadModel.task = nil;
+        
+        // 删除流
+        if (downloadModel.stream.streamStatus > NSStreamStatusNotOpen && downloadModel.stream.streamStatus < NSStreamStatusClosed) {
+            [downloadModel.stream close];
+        }
+        downloadModel.stream = nil;
+        // 删除沙盒中的资源
+        NSError *error = nil;
+        [self.fileManager removeItemAtPath:downloadModel.filePath error:&error];
+        if (error) {
+            NSLog(@"delete file error %@",error);
+        }
+        
+        [self removeDownLoadingModelForURLString:downloadModel.downloadURL];
+        // 删除资源总长度
+        if ([self.fileManager fileExistsAtPath:[self fileSizePathWithDownloadModel:downloadModel]]) {
+            @synchronized (self) {
+                NSMutableDictionary *dict = [self fileSizePlistWithDownloadModel:downloadModel];
+                [dict removeObjectForKey:downloadModel.downloadURL];
+                [dict writeToFile:[self fileSizePathWithDownloadModel:downloadModel] atomically:YES];
+            }
+        }
+    }
+}
+
+- (void)deleteAllFileWithDownloadDirectory:(NSString *)downloadDirectory
+{
+    if (!downloadDirectory) {
+        downloadDirectory = self.downloadDirectory;
+    }
+    if ([self.fileManager fileExistsAtPath:downloadDirectory]) {
+        
+        // 删除任务
+        for (TYDownloadModel *downloadModel in [self.downloadingModelDic allValues]) {
+            if ([downloadModel.downloadDirectory isEqualToString:downloadDirectory]) {
+                // 删除任务
+                downloadModel.task.taskDescription = nil;
+                [downloadModel.task cancel];
+                downloadModel.task = nil;
+                
+                // 删除流
+                if (downloadModel.stream.streamStatus > NSStreamStatusNotOpen && downloadModel.stream.streamStatus < NSStreamStatusClosed) {
+                    [downloadModel.stream close];
+                }
+                downloadModel.stream = nil;
+            }
+        }
+        // 删除沙盒中所有资源
+        [self.fileManager removeItemAtPath:downloadDirectory error:nil];
+    }
+}
+
+#pragma mark - public
+
+// 获取下载模型
+- (TYDownloadModel *)downLoadingModelForURLString:(NSString *)URLString
+{
+    return [self.downloadingModelDic objectForKey:URLString];
+}
+
+// 是否已经下载
+- (BOOL)isDownloadCompletedWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    long long fileSize = [self fileSizeInCachePlistWithDownloadModel:downloadModel];
+    if (fileSize > 0 && fileSize == [self fileSizeWithDownloadModel:downloadModel]) {
+        return YES;
+    }
+    return NO;
+}
+
+// 当前下载进度
+- (TYDownloadProgress *)progessWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    TYDownloadProgress *progress = [[TYDownloadProgress alloc]init];
+    progress.totalBytesExpectedToWrite = [self fileSizeInCachePlistWithDownloadModel:downloadModel];
+    progress.totalBytesWritten = MIN([self fileSizeWithDownloadModel:downloadModel], progress.totalBytesExpectedToWrite);
+    progress.progress = progress.totalBytesExpectedToWrite > 0 ? 1.0*progress.totalBytesWritten/progress.totalBytesExpectedToWrite : 0;
+    
+    return progress;
+}
+
+#pragma mark - private
+
+- (void)downloadModel:(TYDownloadModel *)downloadModel didChangeState:(TYDownloadState)state filePath:(NSString *)filePath error:(NSError *)error
+{
+    if (_delegate && [_delegate respondsToSelector:@selector(downloadModel:didChangeState:filePath:error:)]) {
+        [_delegate downloadModel:downloadModel didChangeState:state filePath:filePath error:error];
+    }
+    
+    if (downloadModel.stateBlock) {
+        downloadModel.stateBlock(state,filePath,error);
+    }
+}
+
+- (void)downloadModel:(TYDownloadModel *)downloadModel updateProgress:(TYDownloadProgress *)progress
+{
+    if (_delegate && [_delegate respondsToSelector:@selector(downloadModel:didUpdateProgress:)]) {
+        [_delegate downloadModel:downloadModel didUpdateProgress:progress];
+    }
+    
+    if (downloadModel.progressBlock) {
+        downloadModel.progressBlock(progress);
+    }
+}
+
+//  创建缓存目录文件
+- (void)createDirectory:(NSString *)directory
+{
+    if (![self.fileManager fileExistsAtPath:directory]) {
+        [self.fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL];
+    }
+}
+
+// 获取文件大小
+- (long long)fileSizeWithDownloadModel:(TYDownloadModel *)downloadModel{
+    NSString *filePath = downloadModel.filePath;
+    if (![self.fileManager fileExistsAtPath:filePath]) return 0;
+    return [[self.fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
+}
+
+// 获取plist保存文件大小
+- (long long)fileSizeInCachePlistWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    NSDictionary *downloadsFileSizePlist = [NSDictionary dictionaryWithContentsOfFile:[self fileSizePathWithDownloadModel:downloadModel]];
+    return [downloadsFileSizePlist[downloadModel.downloadURL] longLongValue];
+}
+
+// 获取plist文件内容
+- (NSMutableDictionary *)fileSizePlistWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    NSMutableDictionary *downloadsFileSizePlist = [NSMutableDictionary dictionaryWithContentsOfFile:[self fileSizePathWithDownloadModel:downloadModel]];
+    if (!downloadsFileSizePlist) {
+        downloadsFileSizePlist = [NSMutableDictionary dictionary];
+    }
+    return downloadsFileSizePlist;
+}
+
+- (void)removeDownLoadingModelForURLString:(NSString *)URLString
+{
+    [self.downloadingModelDic removeObjectForKey:URLString];
+}
+
+#pragma mark - NSURLSessionDelegate
+
+/**
+ * 接收到响应
+ */
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSHTTPURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
+{
+    
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:dataTask.taskDescription];
+    if (!downloadModel) {
+        return;
+    }
+    
+    // 创建目录
+    [self createDirectory:_downloadDirectory];
+    [self createDirectory:downloadModel.downloadDirectory];
+    
+    // 打开流
+    [downloadModel.stream open];
+    
+    // 获得服务器这次请求 返回数据的总长度
+    long long totalBytesWritten =  [self fileSizeWithDownloadModel:downloadModel];
+    long long totalBytesExpectedToWrite = totalBytesWritten + dataTask.countOfBytesExpectedToReceive;
+    
+    downloadModel.progress.resumeBytesWritten = totalBytesWritten;
+    downloadModel.progress.totalBytesWritten = totalBytesWritten;
+    downloadModel.progress.totalBytesExpectedToWrite = totalBytesExpectedToWrite;
+    
+    // 存储总长度
+    @synchronized (self) {
+        NSMutableDictionary *dic = [self fileSizePlistWithDownloadModel:downloadModel];
+        dic[downloadModel.downloadURL] = @(totalBytesExpectedToWrite);
+        [dic writeToFile:[self fileSizePathWithDownloadModel:downloadModel] atomically:YES];
+    }
+    
+    // 接收这个请求,允许接收服务器的数据
+    completionHandler(NSURLSessionResponseAllow);
+}
+
+/**
+ * 接收到服务器返回的数据
+ */
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
+{
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:dataTask.taskDescription];
+    if (!downloadModel || downloadModel.state == TYDownloadStateSuspended) {
+        return;
+    }
+    // 写入数据
+    [downloadModel.stream write:data.bytes maxLength:data.length];
+    
+    // 下载进度
+    downloadModel.progress.bytesWritten = data.length;
+    downloadModel.progress.totalBytesWritten += downloadModel.progress.bytesWritten;
+    downloadModel.progress.progress  = MIN(1.0, 1.0*downloadModel.progress.totalBytesWritten/downloadModel.progress.totalBytesExpectedToWrite);
+    
+    // 时间
+    NSTimeInterval downloadTime = -1 * [downloadModel.downloadDate timeIntervalSinceNow];
+    downloadModel.progress.speed = (downloadModel.progress.totalBytesWritten - downloadModel.progress.resumeBytesWritten) / downloadTime;
+    
+    int64_t remainingContentLength = downloadModel.progress.totalBytesExpectedToWrite - downloadModel.progress.totalBytesWritten;
+    downloadModel.progress.remainingTime = ceilf(remainingContentLength / downloadModel.progress.speed);
+    
+    dispatch_async(dispatch_get_main_queue(), ^(){
+        [self downloadModel:downloadModel updateProgress:downloadModel.progress];
+    });
+}
+
+/**
+ * 请求完毕(成功|失败)
+ */
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
+{
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:task.taskDescription];
+    
+    if (!downloadModel) {
+        return;
+    }
+    
+    // 关闭流
+    [downloadModel.stream close];
+    downloadModel.stream = nil;
+    downloadModel.task = nil;
+    
+    [self removeDownLoadingModelForURLString:downloadModel.downloadURL];
+
+    if (downloadModel.manualCancle) {
+        // 暂停下载
+        dispatch_async(dispatch_get_main_queue(), ^(){
+            downloadModel.manualCancle = NO;
+            downloadModel.state = TYDownloadStateSuspended;
+            [self downloadModel:downloadModel didChangeState:TYDownloadStateSuspended filePath:nil error:nil];
+            [self willResumeNextWithDowloadModel:downloadModel];
+        });
+    }else if (error){
+        // 下载失败
+        dispatch_async(dispatch_get_main_queue(), ^(){
+            downloadModel.state = TYDownloadStateFailed;
+            [self downloadModel:downloadModel didChangeState:TYDownloadStateFailed filePath:nil error:error];
+            [self willResumeNextWithDowloadModel:downloadModel];
+        });
+    }else if ([self isDownloadCompletedWithDownloadModel:downloadModel]) {
+        // 下载完成
+        dispatch_async(dispatch_get_main_queue(), ^(){
+            downloadModel.state = TYDownloadStateCompleted;
+            [self downloadModel:downloadModel didChangeState:TYDownloadStateCompleted filePath:downloadModel.filePath error:nil];
+            [self willResumeNextWithDowloadModel:downloadModel];
+        });
+    }else {
+        // 下载完成
+         dispatch_async(dispatch_get_main_queue(), ^(){
+             downloadModel.state = TYDownloadStateCompleted;
+             [self downloadModel:downloadModel didChangeState:TYDownloadStateCompleted filePath:downloadModel.filePath error:nil];
+             [self willResumeNextWithDowloadModel:downloadModel];
+         });
+    }
+}
+
+@end

+ 93 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownLoadModel.h

@@ -0,0 +1,93 @@
+//
+//  TYDownloadModel.h
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/1.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+// 下载状态
+typedef NS_ENUM(NSUInteger, TYDownloadState) {
+    TYDownloadStateNone,        // 未下载 或 下载删除了
+    TYDownloadStateReadying,    // 等待下载
+    TYDownloadStateRunning,     // 正在下载
+    TYDownloadStateSuspended,   // 下载暂停
+    TYDownloadStateCompleted,   // 下载完成
+    TYDownloadStateFailed       // 下载失败
+};
+
+@class TYDownloadProgress;
+@class TYDownloadModel;
+
+// 进度更新block
+typedef void (^TYDownloadProgressBlock)(TYDownloadProgress *progress);
+// 状态更新block
+typedef void (^TYDownloadStateBlock)(TYDownloadState state,NSString *filePath, NSError *error);
+
+/**
+ *  下载模型
+ */
+@interface TYDownloadModel : NSObject
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  download info
+// 下载地址
+@property (nonatomic, strong, readonly) NSString *downloadURL;
+// 文件名 默认nil 则为下载URL中的文件名
+@property (nonatomic, strong, readonly) NSString *fileName;
+// 缓存文件目录 默认nil 则为manger缓存目录
+@property (nonatomic, strong, readonly) NSString *downloadDirectory;
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  task info
+// 下载状态
+@property (nonatomic, assign, readonly) TYDownloadState state;
+// 下载任务
+@property (nonatomic, strong, readonly) NSURLSessionTask *task;
+// 文件流
+@property (nonatomic, strong, readonly) NSOutputStream *stream;
+// 下载进度
+@property (nonatomic, strong ,readonly) TYDownloadProgress *progress;
+// 下载路径 如果设置了downloadDirectory,文件下载完成后会移动到这个目录,否则,在manager默认cache目录里
+@property (nonatomic, strong, readonly) NSString *filePath;
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  download block
+// 下载进度更新block
+@property (nonatomic, copy) TYDownloadProgressBlock progressBlock;
+// 下载状态更新block
+@property (nonatomic, copy) TYDownloadStateBlock stateBlock;
+
+
+- (instancetype)initWithURLString:(NSString *)URLString;
+/**
+ *  初始化方法
+ *
+ *  @param URLString 下载地址
+ *  @param filePath  缓存地址 当为nil 默认缓存到cache
+ */
+- (instancetype)initWithURLString:(NSString *)URLString filePath:(NSString *)filePath;
+
+@end
+
+/**
+ *  下载进度
+ */
+@interface TYDownloadProgress : NSObject
+
+// 续传大小
+@property (nonatomic, assign, readonly) int64_t resumeBytesWritten;
+// 这次写入的数量
+@property (nonatomic, assign, readonly) int64_t bytesWritten;
+// 已下载的数量
+@property (nonatomic, assign, readonly) int64_t totalBytesWritten;
+// 文件的总大小
+@property (nonatomic, assign, readonly) int64_t totalBytesExpectedToWrite;
+// 下载进度
+@property (nonatomic, assign, readonly) float progress;
+// 下载速度
+@property (nonatomic, assign, readonly) float speed;
+// 下载剩余时间
+@property (nonatomic, assign, readonly) int remainingTime;
+
+
+@end

+ 126 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownLoadModel.m

@@ -0,0 +1,126 @@
+//
+//  TYDownloadModel.m
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/1.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import "TYDownloadModel.h"
+
+@interface TYDownloadProgress ()
+// 续传大小
+@property (nonatomic, assign) int64_t resumeBytesWritten;
+// 这次写入的数量
+@property (nonatomic, assign) int64_t bytesWritten;
+// 已下载的数量
+@property (nonatomic, assign) int64_t totalBytesWritten;
+// 文件的总大小
+@property (nonatomic, assign) int64_t totalBytesExpectedToWrite;
+// 下载进度
+@property (nonatomic, assign) float progress;
+// 下载速度
+@property (nonatomic, assign) float speed;
+// 下载剩余时间
+@property (nonatomic, assign) int remainingTime;
+
+@end
+
+@interface TYDownloadModel ()
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  download info
+// 下载地址
+@property (nonatomic, strong) NSString *downloadURL;
+// 文件名 默认nil 则为下载URL中的文件名
+@property (nonatomic, strong) NSString *fileName;
+// 缓存文件目录 默认nil 则为manger缓存目录
+@property (nonatomic, strong) NSString *downloadDirectory;
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  task info
+// 下载状态
+@property (nonatomic, assign) TYDownloadState state;
+// 下载任务
+@property (nonatomic, strong) NSURLSessionTask *task;
+// 文件流
+@property (nonatomic, strong) NSOutputStream *stream;
+// 下载文件路径,下载完成后有值,把它移动到你的目录
+@property (nonatomic, strong) NSString *filePath;
+// 下载时间
+@property (nonatomic, strong) NSDate *downloadDate;
+// 断点续传需要设置这个数据 
+@property (nonatomic, strong) NSData *resumeData;
+// 手动取消当做暂停
+@property (nonatomic, assign) BOOL manualCancle;
+
+@end
+
+@implementation TYDownloadModel
+
+- (instancetype)init
+{
+    if (self = [super init]) {
+        _progress = [[TYDownloadProgress alloc]init];
+    }
+    return self;
+}
+
+- (instancetype)initWithURLString:(NSString *)URLString
+{
+    return [self initWithURLString:URLString filePath:nil];
+}
+
+- (instancetype)initWithURLString:(NSString *)URLString filePath:(NSString *)filePath
+{
+    if (self = [self init]) {
+        _downloadURL = URLString;
+        NSString*urlFileName = filePath.lastPathComponent;
+        NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+        _fileName = decodeUrlFileName;
+        
+//          if ([_downloadURL.lastPathComponent containsString:@"?"]) {
+//             NSArray *array = [_downloadURL.lastPathComponent componentsSeparatedByString:@"?"];
+//             _fileName = array.firstObject;
+//         }else {
+//             _fileName = _downloadURL.lastPathComponent;
+//         }
+        _downloadDirectory = filePath.stringByDeletingLastPathComponent;
+        _filePath = filePath;
+    }
+    return self;
+}
+
+-(NSString *)fileName
+{
+    if (!_fileName) {
+        _fileName = _downloadURL.lastPathComponent;
+//          if ([_downloadURL.lastPathComponent containsString:@"?"]) {
+//             NSArray *array = [_downloadURL.lastPathComponent componentsSeparatedByString:@"?"];
+//             _fileName = array.firstObject;
+//         }else {
+//             _fileName = _downloadURL.lastPathComponent;
+//         }
+    }
+    return _fileName;
+}
+
+- (NSString *)downloadDirectory
+{
+    if (!_downloadDirectory) {
+        _downloadDirectory = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"TYDownloadCache"];
+    }
+    return _downloadDirectory;
+}
+
+- (NSString *)filePath
+{
+    if (!_filePath) {
+        _filePath = [self.downloadDirectory stringByAppendingPathComponent:self.fileName];
+    }
+    return _filePath;
+}
+
+@end
+
+@implementation TYDownloadProgress
+
+@end

+ 21 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadDelegate.h

@@ -0,0 +1,21 @@
+//
+//  TYDownloadDelegate.h
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/24.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "TYDownloadModel.h"
+
+// 下载代理
+@protocol TYDownloadDelegate <NSObject>
+
+// 更新下载进度
+- (void)downloadModel:(TYDownloadModel *)downloadModel didUpdateProgress:(TYDownloadProgress *)progress;
+
+// 更新下载状态
+- (void)downloadModel:(TYDownloadModel *)downloadModel didChangeState:(TYDownloadState)state filePath:(NSString *)filePath error:(NSError *)error;
+
+@end

+ 85 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadSessionManager.h

@@ -0,0 +1,85 @@
+//
+//  TYDownloadSessionManager.h
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/12.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "TYDownloadModel.h"
+#import "TYDownloadDelegate.h"
+
+/**
+ *  下载管理类 封装NSURLSessionDownloadTask
+ */
+@interface TYDownloadSessionManager : NSObject<NSURLSessionDownloadDelegate>
+
+// 下载代理
+@property (nonatomic,weak) id<TYDownloadDelegate> delegate;
+
+// 等待中的模型 只读
+@property (nonatomic, strong,readonly) NSMutableArray *waitingDownloadModels;
+
+// 下载中的模型 只读
+@property (nonatomic, strong,readonly) NSMutableArray *downloadingModels;
+
+// 最大下载数
+@property (nonatomic, assign) NSInteger maxDownloadCount;
+
+// 等待下载队列 先进先出 默认YES, 当NO时,先进后出
+@property (nonatomic, assign) BOOL resumeDownloadFIFO;
+
+// 全部并发 默认NO, 当YES时,忽略maxDownloadCount
+@property (nonatomic, assign) BOOL isBatchDownload;
+
+// 后台session configure
+@property (nonatomic, strong) NSString *backgroundConfigure;
+@property (nonatomic, copy) void (^backgroundSessionCompletionHandler)();
+
+// 后台下载完成后调用 返回文件保存路径filePath
+@property (nonatomic, copy) NSString *(^backgroundSessionDownloadCompleteBlock)(NSString *downloadURL);
+
+// 单例
++ (TYDownloadSessionManager *)manager;
+
+// 配置后台session
+- (void)configureBackroundSession;
+
+// 获取正在下载模型
+- (TYDownloadModel *)downLoadingModelForURLString:(NSString *)URLString;
+
+// 获取后台运行task
+- (NSURLSessionDownloadTask *)backgroundSessionTasksWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 是否已经下载
+- (BOOL)isDownloadCompletedWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 取消所有完成或失败后台task
+- (void)cancleAllBackgroundSessionTasks;
+
+// 开始下载
+- (TYDownloadModel *)startDownloadURLString:(NSString *)URLString toDestinationPath:(NSString *)destinationPath progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state;
+
+// 开始下载
+- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 开始下载
+- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state;
+
+// 恢复下载(除非确定对这个model进行了suspend,否则使用start)
+- (void)resumeWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 暂停下载
+- (void)suspendWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 取消下载
+- (void)cancleWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 删除下载
+- (void)deleteFileWithDownloadModel:(TYDownloadModel *)downloadModel;
+
+// 删除下载
+- (void)deleteAllFileWithDownloadDirectory:(NSString *)downloadDirectory;
+
+@end

+ 729 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadSessionManager.m

@@ -0,0 +1,729 @@
+//
+//  TYDownloadSessionManager.m
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/12.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import "TYDownloadSessionManager.h"
+#import <CommonCrypto/CommonDigest.h>
+#import <UIKit/UIKit.h>
+#import "NSURLSession+TYCorrectedResumeData.h"
+
+/**
+ *  下载模型
+ */
+@interface TYDownloadModel ()
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  task info
+// 下载状态
+@property (nonatomic, assign) TYDownloadState state;
+// 下载任务
+@property (nonatomic, strong) NSURLSessionDownloadTask *task;
+// 文件流
+@property (nonatomic, strong) NSOutputStream *stream;
+// 下载文件路径,下载完成后有值,把它移动到你的目录
+@property (nonatomic, strong) NSString *filePath;
+// 下载时间
+@property (nonatomic, strong) NSDate *downloadDate;
+// 断点续传需要设置这个数据
+@property (nonatomic, strong) NSData *resumeData;
+// 手动取消当做暂停
+@property (nonatomic, assign) BOOL manualCancle;
+
+@end
+
+/**
+ *  下载进度
+ */
+@interface TYDownloadProgress ()
+// 续传大小
+@property (nonatomic, assign) int64_t resumeBytesWritten;
+// 这次写入的数量
+@property (nonatomic, assign) int64_t bytesWritten;
+// 已下载的数量
+@property (nonatomic, assign) int64_t totalBytesWritten;
+// 文件的总大小
+@property (nonatomic, assign) int64_t totalBytesExpectedToWrite;
+// 下载进度
+@property (nonatomic, assign) float progress;
+// 下载速度
+@property (nonatomic, assign) float speed;
+// 下载剩余时间
+@property (nonatomic, assign) int remainingTime;
+
+@end
+
+@interface TYDownloadSessionManager ()
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  file info
+// 文件管理
+@property (nonatomic, strong) NSFileManager *fileManager;
+// 缓存文件目录
+@property (nonatomic, strong) NSString *downloadDirectory;
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>  session info
+// 下载seesion会话
+@property (nonatomic, strong) NSURLSession *session;
+// 下载模型字典 key = url, value = model
+@property (nonatomic, strong) NSMutableDictionary *downloadingModelDic;
+// 下载中的模型
+@property (nonatomic, strong) NSMutableArray *waitingDownloadModels;
+// 等待中的模型
+@property (nonatomic, strong) NSMutableArray *downloadingModels;
+// 回调代理的队列
+@property (strong, nonatomic) NSOperationQueue *queue;
+
+@end
+
+#define IS_IOS8ORLATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
+#define IS_IOS10ORLATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10)
+#define IS_IOS12ORLATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 12)
+
+@implementation TYDownloadSessionManager
+
++ (TYDownloadSessionManager *)manager
+{
+    static id sharedInstance = nil;
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        sharedInstance = [[self alloc] init];
+    });
+    return sharedInstance;
+}
+
+- (instancetype)init
+{
+    if (self = [super init]) {
+        _backgroundConfigure = @"TYDownloadSessionManager.backgroundConfigure";
+        _maxDownloadCount = 1;
+        _resumeDownloadFIFO = YES;
+        _isBatchDownload = NO;
+    }
+    return self;
+}
+
+- (void)configureBackroundSession
+{
+    if (!_backgroundConfigure) {
+        return;
+    }
+    [self session];
+}
+
+#pragma mark - getter
+
+- (NSFileManager *)fileManager
+{
+    if (!_fileManager) {
+        _fileManager = [[NSFileManager alloc]init];
+    }
+    return _fileManager;
+}
+
+- (NSURLSession *)session
+{
+    if (!_session) {
+        if (_backgroundConfigure) {
+            if (IS_IOS8ORLATER) {
+                NSURLSessionConfiguration *configure = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:_backgroundConfigure];
+                _session = [NSURLSession sessionWithConfiguration:configure delegate:self delegateQueue:self.queue];
+            }else{
+                _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration backgroundSessionConfiguration:_backgroundConfigure]delegate:self delegateQueue:self.queue];
+            }
+        }else {
+            _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:self.queue];
+        }
+    }
+    return _session;
+}
+
+- (NSOperationQueue *)queue
+{
+    if (!_queue) {
+        _queue = [[NSOperationQueue alloc]init];
+        _queue.maxConcurrentOperationCount = 1;
+    }
+    return _queue;
+}
+
+- (NSString *)downloadDirectory
+{
+    if (!_downloadDirectory) {
+        _downloadDirectory = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"TYDownloadCache"];
+        [self createDirectory:_downloadDirectory];
+    }
+    return _downloadDirectory;
+}
+
+- (NSMutableDictionary *)downloadingModelDic
+{
+    if (!_downloadingModelDic) {
+        _downloadingModelDic = [NSMutableDictionary dictionary];
+    }
+    return _downloadingModelDic;
+}
+
+- (NSMutableArray *)waitingDownloadModels
+{
+    if (!_waitingDownloadModels) {
+        _waitingDownloadModels = [NSMutableArray array];
+    }
+    return _waitingDownloadModels;
+}
+
+- (NSMutableArray *)downloadingModels
+{
+    if (!_downloadingModels) {
+        _downloadingModels = [NSMutableArray array];
+    }
+    return _downloadingModels;
+}
+
+#pragma mark - downlaod
+
+// 开始下载
+- (TYDownloadModel *)startDownloadURLString:(NSString *)URLString toDestinationPath:(NSString *)destinationPath progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state
+{
+    // 验证下载地址
+    if (!URLString) {
+        NSLog(@"dwonloadURL can't nil");
+        return nil;
+    }
+    
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:URLString];
+    
+    if (!downloadModel || ![downloadModel.filePath isEqualToString:destinationPath]) {
+        downloadModel = [[TYDownloadModel alloc]initWithURLString:URLString filePath:destinationPath];
+    }
+    
+    [self startWithDownloadModel:downloadModel progress:progress state:state];
+    
+    return downloadModel;
+}
+
+- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state
+{
+    downloadModel.progressBlock = progress;
+    downloadModel.stateBlock = state;
+    
+    [self startWithDownloadModel:downloadModel];
+}
+
+
+- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel) {
+        return;
+    }
+    
+    if (downloadModel.state == TYDownloadStateReadying) {
+        [self downloadModel:downloadModel didChangeState:TYDownloadStateReadying filePath:nil error:nil];
+        return;
+    }
+
+    // 验证是否存在
+    if (downloadModel.task && downloadModel.task.state == NSURLSessionTaskStateRunning) {
+        downloadModel.state = TYDownloadStateRunning;
+        [self downloadModel:downloadModel didChangeState:TYDownloadStateRunning filePath:nil error:nil];
+        return;
+    }
+    
+    // 后台下载设置
+    [self configirebackgroundSessionTasksWithDownloadModel:downloadModel];
+    
+    [self resumeWithDownloadModel:downloadModel];
+}
+
+// 恢复下载
+- (void)resumeWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel) {
+        return;
+    }
+    
+    if (![self canResumeDownlaodModel:downloadModel]) {
+        return;
+    }
+    
+    // 如果task 不存在 或者 取消了
+    if (!downloadModel.task || downloadModel.task.state == NSURLSessionTaskStateCanceling) {
+        
+        NSData *resumeData = [self resumeDataFromFileWithDownloadModel:downloadModel];
+        
+        if ([self isValideResumeData:resumeData]) {
+            if (IS_IOS10ORLATER && !IS_IOS12ORLATER) {
+                downloadModel.task = [self.session downloadTaskWithCorrectResumeData:resumeData];
+            }else {
+                 downloadModel.task = [self.session downloadTaskWithResumeData:resumeData];
+            }
+        }else {
+            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadModel.downloadURL]];
+            downloadModel.task = [self.session downloadTaskWithRequest:request];
+        }
+        downloadModel.task.taskDescription = downloadModel.downloadURL;
+        downloadModel.downloadDate = [NSDate date];
+    }
+
+    if (!downloadModel.downloadDate) {
+        downloadModel.downloadDate = [NSDate date];
+    }
+    
+    if (![self.downloadingModelDic objectForKey:downloadModel.downloadURL]) {
+        self.downloadingModelDic[downloadModel.downloadURL] = downloadModel;
+    }
+    
+    [downloadModel.task resume];
+    
+    downloadModel.state = TYDownloadStateRunning;
+    [self downloadModel:downloadModel didChangeState:TYDownloadStateRunning filePath:nil error:nil];
+}
+
+- (BOOL)isValideResumeData:(NSData *)resumeData
+{
+    if (!resumeData || resumeData.length == 0) {
+        return NO;
+    }
+    return YES;
+}
+
+// 暂停下载
+- (void)suspendWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel.manualCancle) {
+        downloadModel.manualCancle = YES;
+        [self cancleWithDownloadModel:downloadModel clearResumeData:NO];
+    }
+}
+
+// 取消下载
+- (void)cancleWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (downloadModel.state != TYDownloadStateCompleted && downloadModel.state != TYDownloadStateFailed){
+        [self cancleWithDownloadModel:downloadModel clearResumeData:NO];
+    }
+}
+
+// 删除下载
+- (void)deleteFileWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!downloadModel || !downloadModel.filePath) {
+        return;
+    }
+    
+    [self cancleWithDownloadModel:downloadModel clearResumeData:YES];
+    [self deleteFileIfExist:downloadModel.filePath];
+}
+
+- (void)deleteAllFileWithDownloadDirectory:(NSString *)downloadDirectory
+{
+    if (!downloadDirectory) {
+        downloadDirectory = self.downloadDirectory;
+    }
+    
+     for (TYDownloadModel *downloadModel in [self.downloadingModelDic allValues]) {
+          if ([downloadModel.downloadDirectory isEqualToString:downloadDirectory]) {
+              [self cancleWithDownloadModel:downloadModel clearResumeData:YES];
+          }
+     }
+    // 删除沙盒中所有资源
+    [self.fileManager removeItemAtPath:downloadDirectory error:nil];
+}
+
+// 取消下载 是否删除resumeData
+- (void)cancleWithDownloadModel:(TYDownloadModel *)downloadModel clearResumeData:(BOOL)clearResumeData
+{
+    if (!downloadModel.task && downloadModel.state == TYDownloadStateReadying) {
+        [self removeDownLoadingModelForURLString:downloadModel.downloadURL];
+        @synchronized (self) {
+            [self.waitingDownloadModels removeObject:downloadModel];
+        }
+        downloadModel.state = TYDownloadStateNone;
+        [self downloadModel:downloadModel didChangeState:TYDownloadStateNone filePath:nil error:nil];
+        return;
+    }
+    if (clearResumeData) {
+        downloadModel.state = TYDownloadStateNone;
+        downloadModel.resumeData = nil;
+        [self deleteFileIfExist:[self resumeDataPathWithDownloadURL:downloadModel.downloadURL]];
+        [downloadModel.task cancel];
+    }else {
+        [(NSURLSessionDownloadTask *)downloadModel.task cancelByProducingResumeData:^(NSData *resumeData){
+        }];
+    }
+}
+
+- (void)willResumeNextWithDowloadModel:(TYDownloadModel *)downloadModel
+{
+    if (_isBatchDownload) {
+        return;
+    }
+    
+    @synchronized (self) {
+        [self.downloadingModels removeObject:downloadModel];
+        // 还有未下载的
+        if (self.waitingDownloadModels.count > 0) {
+            [self resumeWithDownloadModel:_resumeDownloadFIFO ? self.waitingDownloadModels.firstObject:self.waitingDownloadModels.lastObject];
+        }
+    }
+}
+
+- (BOOL)canResumeDownlaodModel:(TYDownloadModel *)downloadModel
+{
+    if (_isBatchDownload) {
+        return YES;
+    }
+    
+    @synchronized (self) {
+        if (self.downloadingModels.count >= _maxDownloadCount ) {
+            if ([self.waitingDownloadModels indexOfObject:downloadModel] == NSNotFound) {
+                [self.waitingDownloadModels addObject:downloadModel];
+                self.downloadingModelDic[downloadModel.downloadURL] = downloadModel;
+            }
+            downloadModel.state = TYDownloadStateReadying;
+            [self downloadModel:downloadModel didChangeState:TYDownloadStateReadying filePath:nil error:nil];
+            return NO;
+        }
+        
+        if ([self.waitingDownloadModels indexOfObject:downloadModel] != NSNotFound) {
+            [self.waitingDownloadModels removeObject:downloadModel];
+        }
+        
+        if ([self.downloadingModels indexOfObject:downloadModel] == NSNotFound) {
+            [self.downloadingModels addObject:downloadModel];
+        }
+        return YES;
+    }
+}
+
+#pragma mark - configire background task
+
+// 配置后台后台下载session
+- (void)configirebackgroundSessionTasksWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (!_backgroundConfigure) {
+        return ;
+    }
+    
+    NSURLSessionDownloadTask *task = [self backgroundSessionTasksWithDownloadModel:downloadModel];
+    if (!task) {
+        return;
+    }
+    
+    downloadModel.task = task;
+    if (task.state == NSURLSessionTaskStateRunning) {
+        [task suspend];
+    }
+}
+
+- (NSURLSessionDownloadTask *)backgroundSessionTasksWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    NSArray *tasks = [self sessionDownloadTasks];
+    for (NSURLSessionDownloadTask *task in tasks) {
+        if (task.state == NSURLSessionTaskStateRunning || task.state == NSURLSessionTaskStateSuspended) {
+            if ([downloadModel.downloadURL isEqualToString:task.taskDescription]) {
+                return task;
+            }
+        }
+    }
+    return nil;
+}
+
+// 获取所以的后台下载session
+- (NSArray *)sessionDownloadTasks
+{
+    __block NSArray *tasks = nil;
+    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
+    [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
+        tasks = downloadTasks;
+        dispatch_semaphore_signal(semaphore);
+    }];
+    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
+    return tasks;
+}
+
+#pragma mark - public
+
+// 获取下载模型
+- (TYDownloadModel *)downLoadingModelForURLString:(NSString *)URLString
+{
+    return [self.downloadingModelDic objectForKey:URLString];
+}
+
+// 是否已经下载
+- (BOOL)isDownloadCompletedWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    return [self.fileManager fileExistsAtPath:downloadModel.filePath];
+}
+
+// 取消所有后台
+- (void)cancleAllBackgroundSessionTasks
+{
+    if (!_backgroundConfigure) {
+        return;
+    }
+    
+    for (NSURLSessionDownloadTask *task in [self sessionDownloadTasks]) {
+        [task cancelByProducingResumeData:^(NSData * resumeData) {
+            }];
+    }
+}
+
+#pragma mark - private
+
+- (void)downloadModel:(TYDownloadModel *)downloadModel didChangeState:(TYDownloadState)state filePath:(NSString *)filePath error:(NSError *)error
+{
+    if (_delegate && [_delegate respondsToSelector:@selector(downloadModel:didChangeState:filePath:error:)]) {
+        [_delegate downloadModel:downloadModel didChangeState:state filePath:filePath error:error];
+    }
+    
+    if (downloadModel.stateBlock) {
+        downloadModel.stateBlock(state,filePath,error);
+    }
+}
+
+- (void)downloadModel:(TYDownloadModel *)downloadModel updateProgress:(TYDownloadProgress *)progress
+{
+    if (_delegate && [_delegate respondsToSelector:@selector(downloadModel:didUpdateProgress:)]) {
+        [_delegate downloadModel:downloadModel didUpdateProgress:progress];
+    }
+    
+    if (downloadModel.progressBlock) {
+        downloadModel.progressBlock(progress);
+    }
+}
+
+- (void)removeDownLoadingModelForURLString:(NSString *)URLString
+{
+    [self.downloadingModelDic removeObjectForKey:URLString];
+}
+
+// 获取resumeData
+- (NSData *)resumeDataFromFileWithDownloadModel:(TYDownloadModel *)downloadModel
+{
+    if (downloadModel.resumeData) {
+        return downloadModel.resumeData;
+    }
+    NSString *resumeDataPath = [self resumeDataPathWithDownloadURL:downloadModel.downloadURL];
+    
+    if ([_fileManager fileExistsAtPath:resumeDataPath]) {
+        NSData *resumeData = [NSData dataWithContentsOfFile:resumeDataPath];
+        return resumeData;
+    }
+    return nil;
+}
+
+// resumeData 路径
+- (NSString *)resumeDataPathWithDownloadURL:(NSString *)downloadURL
+{
+    NSString *resumeFileName = [[self class] md5:downloadURL];
+    return [self.downloadDirectory stringByAppendingPathComponent:resumeFileName];
+}
+
++ (NSString *)md5:(NSString *)str
+{
+    const char *cStr = [str UTF8String];
+    if (cStr == NULL) {
+        cStr = "";
+    }
+    unsigned char result[CC_MD5_DIGEST_LENGTH];
+    CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
+    return [NSString stringWithFormat:
+            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
+            result[0], result[1], result[2], result[3],
+            result[4], result[5], result[6], result[7],
+            result[8], result[9], result[10], result[11],
+            result[12], result[13], result[14], result[15]
+            ];
+}
+
+//  创建缓存目录文件
+- (void)createDirectory:(NSString *)directory
+{
+    if (![self.fileManager fileExistsAtPath:directory]) {
+        [self.fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL];
+    }
+}
+
+- (void)moveFileAtURL:(NSURL *)srcURL toPath:(NSString *)dstPath
+{
+    if (!dstPath) {
+        NSLog(@"error filePath is nil!");
+        return;
+    }
+    NSError *error = nil;
+    if ([self.fileManager fileExistsAtPath:dstPath] ) {
+        [self.fileManager removeItemAtPath:dstPath error:&error];
+        if (error) {
+            NSLog(@"removeItem error %@",error);
+        }
+    }
+    
+    NSURL *dstURL = [NSURL fileURLWithPath:dstPath];
+    [self.fileManager moveItemAtURL:srcURL toURL:dstURL error:&error];
+    if (error){
+        NSLog(@"moveItem error:%@",error);
+    }
+}
+
+- (void)deleteFileIfExist:(NSString *)filePath
+{
+    if ([self.fileManager fileExistsAtPath:filePath] ) {
+        NSError *error  = nil;
+        [self.fileManager removeItemAtPath:filePath error:&error];
+        if (error) {
+            NSLog(@"emoveItem error %@",error);
+        }
+    }
+}
+
+
+#pragma mark - NSURLSessionDownloadDelegate
+
+// 恢复下载
+- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
+{
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:downloadTask.taskDescription];
+    
+    if (!downloadModel || downloadModel.state == TYDownloadStateSuspended) {
+        return;
+    }
+    
+    downloadModel.progress.resumeBytesWritten = fileOffset;
+}
+
+// 监听文件下载进度
+- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
+      didWriteData:(int64_t)bytesWritten
+ totalBytesWritten:(int64_t)totalBytesWritten
+totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
+{
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:downloadTask.taskDescription];
+    
+    if (!downloadModel || downloadModel.state == TYDownloadStateSuspended) {
+        return;
+    }
+    
+    float progress = (double)totalBytesWritten/totalBytesExpectedToWrite;
+    
+    int64_t resumeBytesWritten = downloadModel.progress.resumeBytesWritten;
+    
+    NSTimeInterval downloadTime = -1 * [downloadModel.downloadDate timeIntervalSinceNow];
+    float speed = (totalBytesWritten - resumeBytesWritten) / downloadTime;
+    
+    int64_t remainingContentLength = totalBytesExpectedToWrite - totalBytesWritten;
+    int remainingTime = ceilf(remainingContentLength / speed);
+    
+    downloadModel.progress.bytesWritten = bytesWritten;
+    downloadModel.progress.totalBytesWritten = totalBytesWritten;
+    downloadModel.progress.totalBytesExpectedToWrite = totalBytesExpectedToWrite;
+    downloadModel.progress.progress = progress;
+    downloadModel.progress.speed = speed;
+    downloadModel.progress.remainingTime = remainingTime;
+    
+    dispatch_async(dispatch_get_main_queue(), ^(){
+        [self downloadModel:downloadModel updateProgress:downloadModel.progress];
+    });
+}
+
+
+// 下载成功
+- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
+didFinishDownloadingToURL:(NSURL *)location
+{
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:downloadTask.taskDescription];
+    if (!downloadModel && _backgroundSessionDownloadCompleteBlock) {
+        NSString *filePath = _backgroundSessionDownloadCompleteBlock(downloadTask.taskDescription);
+        // 移动文件到下载目录
+        [self createDirectory:filePath.stringByDeletingLastPathComponent];
+        [self moveFileAtURL:location toPath:filePath];
+        return;
+    }
+    
+    if (location) {
+        // 移动文件到下载目录
+        [self createDirectory:downloadModel.downloadDirectory];
+        [self moveFileAtURL:location toPath:downloadModel.filePath];
+    }
+}
+
+// 下载完成
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
+{
+    TYDownloadModel *downloadModel = [self downLoadingModelForURLString:task.taskDescription];
+    
+    if (!downloadModel) {
+        NSData *resumeData = error ? [error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData]:nil;
+        if (resumeData) {
+            [self createDirectory:_downloadDirectory];
+            [resumeData writeToFile:[self resumeDataPathWithDownloadURL:task.taskDescription] atomically:YES];
+        }else {
+            [self deleteFileIfExist:[self resumeDataPathWithDownloadURL:task.taskDescription]];
+        }
+        return;
+    }
+
+    NSData *resumeData = nil;
+    if (error) {
+        resumeData = [error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData];
+    }
+    // 缓存resumeData
+    if (resumeData) {
+        downloadModel.resumeData = resumeData;
+        [self createDirectory:_downloadDirectory];
+        [downloadModel.resumeData writeToFile:[self resumeDataPathWithDownloadURL:downloadModel.downloadURL] atomically:YES];
+    }else {
+        downloadModel.resumeData = nil;
+        [self deleteFileIfExist:[self resumeDataPathWithDownloadURL:downloadModel.downloadURL]];
+    }
+    
+    downloadModel.progress.resumeBytesWritten = 0;
+    downloadModel.task = nil;
+    [self removeDownLoadingModelForURLString:downloadModel.downloadURL];
+    
+    if (downloadModel.manualCancle) {
+        // 手动取消,当做暂停
+        dispatch_async(dispatch_get_main_queue(), ^(){
+            downloadModel.manualCancle = NO;
+            downloadModel.state = TYDownloadStateSuspended;
+            [self downloadModel:downloadModel didChangeState:TYDownloadStateSuspended filePath:nil error:nil];
+            [self willResumeNextWithDowloadModel:downloadModel];
+        });
+    }else if (error){
+        
+        if (downloadModel.state == TYDownloadStateNone) {
+            // 删除下载
+            dispatch_async(dispatch_get_main_queue(), ^(){
+                downloadModel.state = TYDownloadStateNone;
+                [self downloadModel:downloadModel didChangeState:TYDownloadStateNone filePath:nil error:error];
+                [self willResumeNextWithDowloadModel:downloadModel];
+            });
+        }else {
+            // 下载失败
+            dispatch_async(dispatch_get_main_queue(), ^(){
+                downloadModel.state = TYDownloadStateFailed;
+                [self downloadModel:downloadModel didChangeState:TYDownloadStateFailed filePath:nil error:error];
+                [self willResumeNextWithDowloadModel:downloadModel];
+            });
+        }
+    }else {
+        // 下载完成
+        dispatch_async(dispatch_get_main_queue(), ^(){
+            downloadModel.state = TYDownloadStateCompleted;
+            [self downloadModel:downloadModel didChangeState:TYDownloadStateCompleted filePath:downloadModel.filePath error:nil];
+            [self willResumeNextWithDowloadModel:downloadModel];
+        });
+    }
+
+}
+
+// 后台session下载完成
+- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
+{
+    if (self.backgroundSessionCompletionHandler) {
+        self.backgroundSessionCompletionHandler();
+    }
+}
+
+@end

+ 22 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadUtility.h

@@ -0,0 +1,22 @@
+//
+//  TYDownloadUtility.h
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/12.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+/**
+ *  下载工具类
+ */
+@interface TYDownloadUtility : NSObject
+
+// 返回文件大小
++ (float)calculateFileSizeInUnit:(unsigned long long)contentLength;
+
+// 返回文件大小的单位
++ (NSString *)calculateUnit:(unsigned long long)contentLength;
+
+@end

+ 36 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/TYDownloadManager/TYDownloadUtility.m

@@ -0,0 +1,36 @@
+//
+//  TYDownloadUtility.m
+//  TYDownloadManagerDemo
+//
+//  Created by tany on 16/6/12.
+//  Copyright © 2016年 tany. All rights reserved.
+//
+
+#import "TYDownloadUtility.h"
+
+@implementation TYDownloadUtility
+
++ (float)calculateFileSizeInUnit:(unsigned long long)contentLength
+{
+    if(contentLength >= pow(1024, 3))
+        return (float) (contentLength / (float)pow(1024, 3));
+    else if(contentLength >= pow(1024, 2))
+        return (float) (contentLength / (float)pow(1024, 2));
+    else if(contentLength >= 1024)
+        return (float) (contentLength / (float)1024);
+    else
+        return (float) (contentLength);
+}
++ (NSString *)calculateUnit:(unsigned long long)contentLength
+{
+    if(contentLength >= pow(1024, 3))
+        return @"GB";
+    else if(contentLength >= pow(1024, 2))
+        return @"MB";
+    else if(contentLength >= 1024)
+        return @"KB";
+    else
+        return @"Bytes";
+}
+
+@end

+ 3 - 2
创维盒子/双子星云手机/Class/Set/uploadFile/uploadFileManager/boxDownloadFileManager.h

@@ -8,14 +8,15 @@
 #import <Foundation/Foundation.h>
 #import "queryOneShareModel.h"
 #import "uploadFileManager.h"
-#import "mixDownloadManager.h"
+//#import "mixDownloadManager.h"
+#import "TYDownLoadDataManager.h"
 
 NS_ASSUME_NONNULL_BEGIN
 
 
 #define share_box_download_tableName [[NSString alloc] initWithFormat:@"share_download_tableName_%@",[connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn]
 
-@interface boxDownloadFileManager : NSObject
+@interface boxDownloadFileManager : NSObject <TYDownloadDelegate>
 +(boxDownloadFileManager *)shareInstance;
 
 //数据库数据

+ 96 - 26
创维盒子/双子星云手机/Class/Set/uploadFile/uploadFileManager/boxDownloadFileManager.m

@@ -29,7 +29,7 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
         //[self initManager];
         //某一个YCDownloadItem下载成功通知
         //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskFinishedNoti:) name:mixDownloadTaskExeEnd object:nil];
-        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskFinishedNoti:) name:mixDownloadTaskExeEnd object:nil];
+        //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskFinishedNoti:) name:mixDownloadTaskExeEnd object:nil];
      
         [self initDownloadManagerFun];
     }
@@ -39,7 +39,7 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
 
 - (void)initDownloadManagerFun
 {
-    [mixDownloadManager shareManager].uid = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
+    //[mixDownloadManager shareManager].uid = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
     //[mixDownloadManager shareManager]
     
     [self delItemInDoneTaskFun];
@@ -271,7 +271,12 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
         NSString *urlString = dataModel.fileUrl;
         NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
         
-        [[mixDownloadManager shareManager] cancelDownloadWithUrl:requestURLEncodedString];
+        //[[mixDownloadManager shareManager] cancelDownloadWithUrl:requestURLEncodedString];
+        
+        TYDownloadModel* downloadModel = [[TYDownLoadDataManager manager] downLoadingModelForURLString:requestURLEncodedString];
+        if(downloadModel){
+            [[TYDownLoadDataManager manager] deleteFileWithDownloadModel:downloadModel];
+        }
     }
     
 }
@@ -316,39 +321,72 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
             NSString *urlString = fileModel.fileUrl;
             NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
-            [[mixDownloadManager shareManager] downloadWithURL:[NSURL URLWithString:requestURLEncodedString] complete:^(NSDictionary * _Nonnull respose, NSError * _Nonnull error) {
-                HLog(@"download:%@",respose);
-            }];
+//            [[mixDownloadManager shareManager] downloadWithURL:[NSURL URLWithString:requestURLEncodedString] complete:^(NSDictionary * _Nonnull respose, NSError * _Nonnull error) {
+//                HLog(@"download:%@",respose);
+//            }];
+            
+            NSString*filePath = [HWDataManager documentPathForAccount:[connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn fileFolder:@"TYDownLoadManager"];
+            
+            TYDownloadModel *model = [[TYDownloadModel alloc] initWithURLString:requestURLEncodedString filePath:filePath];
+            
+            //TYDownloadProgress *progress = [[TYDownLoadDataManager manager] progessWithDownloadModel:model];
+            
+            TYDownLoadDataManager *manager = [TYDownLoadDataManager manager];
+            manager.delegate = self;
+            [manager startWithDownloadModel:model];
         }
     }
 }
 
-#pragma mark 下载完成
-- (void)downloadTaskFinishedNoti:(NSNotification *)notification
+#pragma mark - TYDownloadDelegate
+
+- (void)downloadModel:(TYDownloadModel *)downloadModel didUpdateProgress:(TYDownloadProgress *)progress
 {
-    mixDownloadOperation *model = notification.userInfo.allValues.firstObject;
-    
-    if(!model)return;
+    NSLog(@"delegate progress %.3f",progress.progress);
+}
+
+- (void)downloadModel:(TYDownloadModel *)downloadModel didChangeState:(TYDownloadState)state filePath:(NSString *)filePath error:(NSError *)error
+{
+    //NSLog(@"delegate state %ld error%@ filePath%@",state,error,filePath);
     
-    if (model.downloadState == DownloadStateCompleted) {
-        [self handldDownloadDoneDataBy:model];
+    if(state == TYDownloadStateCompleted){
+        [self handldDownloadDoneDataBy:downloadModel.downloadURL];
     }
-    else if (model.downloadState == DownloadStateFailed){
-        [self handldDownloadFailDataBy:model];
+    else if(state == TYDownloadStateFailed){
+        [self handldDownloadFailDataBy:downloadModel.downloadURL];
     }
     
-    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
-        [[NSNotificationCenter defaultCenter] postNotificationName:nasDownloadTaskFinishedNotification object:model];
-    });
+    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+    //        [[NSNotificationCenter defaultCenter] postNotificationName:nasDownloadTaskFinishedNotification object:model];
+    //    });
 }
 
+//#pragma mark 下载完成
+//- (void)downloadTaskFinishedNoti:(NSNotification *)notification
+//{
+//    mixDownloadOperation *model = notification.userInfo.allValues.firstObject;
+//    
+//    if(!model)return;
+//    
+//    if (model.downloadState == DownloadStateCompleted) {
+//        [self handldDownloadDoneDataBy:model];
+//    }
+//    else if (model.downloadState == DownloadStateFailed){
+//        [self handldDownloadFailDataBy:model];
+//    }
+//    
+//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+//        [[NSNotificationCenter defaultCenter] postNotificationName:nasDownloadTaskFinishedNotification object:model];
+//    });
+//}
+
 
 
 #pragma mark 下载完成数据回调处理
-- (void)handldDownloadDoneDataBy:(mixDownloadOperation*)model
+- (void)handldDownloadDoneDataBy:(NSString*)url
 {
     //解码
-    NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+    NSString * urlString = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
     if(_databaseArr && _databaseArr.count == 3 ){
         NSMutableArray *ingArr = _databaseArr[0];
@@ -377,10 +415,10 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
 
 
 #pragma mark 下载失败数据回调处理
-- (void)handldDownloadFailDataBy:(mixDownloadOperation*)model
+- (void)handldDownloadFailDataBy:(NSString*)url
 {
     //解码
-    NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+    NSString * urlString = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
     if(_databaseArr && _databaseArr.count == 3 ){
         NSMutableArray *ingArr = _databaseArr[0];
@@ -425,14 +463,30 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
             }
         }
         
-        [[mixDownloadManager shareManager] suspendAllDownloadTask];
+        //[[mixDownloadManager shareManager] suspendAllDownloadTask];
+        
+        for (TYDownloadModel *downLoadModel in [TYDownLoadDataManager manager].downloadingModels) {
+            if([downLoadModel isKindOfClass:[TYDownloadModel class]]){
+                [[TYDownLoadDataManager manager] suspendWithDownloadModel:downLoadModel];
+            }
+        }
+        
+        for (TYDownloadModel *downLoadModel in [TYDownLoadDataManager manager].waitingDownloadModels) {
+            if([downLoadModel isKindOfClass:[TYDownloadModel class]]){
+                [[TYDownLoadDataManager manager] suspendWithDownloadModel:downLoadModel];
+            }
+        }
+        
     }
     else if(dataModel){//暂停指定任务
         NSString *urlString = dataModel.fileUrl;
         NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
         HLog(@"%@",urlString);
         
-        [[mixDownloadManager shareManager] supendDownloadWithUrl:requestURLEncodedString];
+        //[[mixDownloadManager shareManager] supendDownloadWithUrl:requestURLEncodedString];
+        
+        TYDownloadModel *downLoadModel = [[TYDownLoadDataManager manager] downLoadingModelForURLString:requestURLEncodedString];
+        [[TYDownLoadDataManager manager] suspendWithDownloadModel:downLoadModel];
         
         //2.修改状态
         if (_databaseArr && _databaseArr.count == 3) {
@@ -458,12 +512,22 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
 {
  
     if(isAllType){
-        [[mixDownloadManager shareManager] startAllDownloadTask];
+        //[[mixDownloadManager shareManager] startAllDownloadTask];
         
         if (_databaseArr && _databaseArr.count == 3) {
             NSMutableArray *downloadingArr = _databaseArr[0];
             for (ShareFileDataModel*model in downloadingArr) {
                 model.downloadBoxStateType = downloadBoxStateDownloadloading;
+                
+                NSString *urlString = model.fileUrl;
+                NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+
+                TYDownloadModel *model = [[TYDownloadModel alloc]initWithURLString:requestURLEncodedString];
+                //TYDownloadProgress *progress = [[TYDownLoadDataManager manager] progessWithDownloadModel:model];
+                
+                TYDownLoadDataManager *manager = [TYDownLoadDataManager manager];
+                manager.delegate = self;
+                [manager startWithDownloadModel:model];
             }
             
             if(downloadingArr.count > 0){
@@ -491,8 +555,14 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
         NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
         HLog(@"%@",urlString);
         
-        [[mixDownloadManager shareManager] startDownLoadWithUrl:requestURLEncodedString];
+        //[[mixDownloadManager shareManager] startDownLoadWithUrl:requestURLEncodedString];
+
+        TYDownloadModel *model = [[TYDownloadModel alloc]initWithURLString:requestURLEncodedString];
+        //TYDownloadProgress *progress = [[TYDownLoadDataManager manager] progessWithDownloadModel:model];
         
+        TYDownLoadDataManager *manager = [TYDownLoadDataManager manager];
+        manager.delegate = self;
+        [manager startWithDownloadModel:model];
     }
 }