├── README.md ├── RJDownloaderDemo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ ├── Ryan.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── Rylan.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ ├── Ryan.xcuserdatad │ └── xcschemes │ │ ├── RJDownloaderDemo.xcscheme │ │ └── xcschememanagement.plist │ └── Rylan.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── RJDownloaderDemo.xcscheme │ └── xcschememanagement.plist └── RJDownloaderDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── RJHTTPDownloader ├── RJHTTPDownloader.h └── RJHTTPDownloader.m ├── ViewController.h ├── ViewController.m └── main.m /README.md: -------------------------------------------------------------------------------- 1 | # RJHTTPDownloader 2 | Sample project for blog post. 3 | 4 | Demonstrated and illustrated the implementation of NSOperation with Concurrency Programming. 5 | 6 | This is the code repository associated with blog post, please check out http://www.jianshu.com/p/ebb3e42049fd to view this article. 7 | -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | EE1292441BFA0A91001C5E4B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = EE1292431BFA0A91001C5E4B /* main.m */; }; 11 | EE1292471BFA0A91001C5E4B /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = EE1292461BFA0A91001C5E4B /* AppDelegate.m */; }; 12 | EE12924A1BFA0A91001C5E4B /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = EE1292491BFA0A91001C5E4B /* ViewController.m */; }; 13 | EE12924D1BFA0A91001C5E4B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EE12924B1BFA0A91001C5E4B /* Main.storyboard */; }; 14 | EE12924F1BFA0A91001C5E4B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = EE12924E1BFA0A91001C5E4B /* Assets.xcassets */; }; 15 | EE1292521BFA0A91001C5E4B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EE1292501BFA0A91001C5E4B /* LaunchScreen.storyboard */; }; 16 | EE12925C1BFA0AE4001C5E4B /* RJHTTPDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = EE12925B1BFA0AE4001C5E4B /* RJHTTPDownloader.m */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | EE12923F1BFA0A91001C5E4B /* RJDownloaderDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RJDownloaderDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | EE1292431BFA0A91001C5E4B /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 22 | EE1292451BFA0A91001C5E4B /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 23 | EE1292461BFA0A91001C5E4B /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 24 | EE1292481BFA0A91001C5E4B /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 25 | EE1292491BFA0A91001C5E4B /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 26 | EE12924C1BFA0A91001C5E4B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | EE12924E1BFA0A91001C5E4B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | EE1292511BFA0A91001C5E4B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | EE1292531BFA0A91001C5E4B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | EE12925A1BFA0AE4001C5E4B /* RJHTTPDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RJHTTPDownloader.h; sourceTree = ""; }; 31 | EE12925B1BFA0AE4001C5E4B /* RJHTTPDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RJHTTPDownloader.m; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | EE12923C1BFA0A91001C5E4B /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | EE1292361BFA0A91001C5E4B = { 46 | isa = PBXGroup; 47 | children = ( 48 | EE1292411BFA0A91001C5E4B /* RJDownloaderDemo */, 49 | EE1292401BFA0A91001C5E4B /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | EE1292401BFA0A91001C5E4B /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | EE12923F1BFA0A91001C5E4B /* RJDownloaderDemo.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | EE1292411BFA0A91001C5E4B /* RJDownloaderDemo */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | EE1292591BFA0AE4001C5E4B /* RJHTTPDownloader */, 65 | EE1292451BFA0A91001C5E4B /* AppDelegate.h */, 66 | EE1292461BFA0A91001C5E4B /* AppDelegate.m */, 67 | EE1292481BFA0A91001C5E4B /* ViewController.h */, 68 | EE1292491BFA0A91001C5E4B /* ViewController.m */, 69 | EE12924B1BFA0A91001C5E4B /* Main.storyboard */, 70 | EE12924E1BFA0A91001C5E4B /* Assets.xcassets */, 71 | EE1292501BFA0A91001C5E4B /* LaunchScreen.storyboard */, 72 | EE1292531BFA0A91001C5E4B /* Info.plist */, 73 | EE1292421BFA0A91001C5E4B /* Supporting Files */, 74 | ); 75 | path = RJDownloaderDemo; 76 | sourceTree = ""; 77 | }; 78 | EE1292421BFA0A91001C5E4B /* Supporting Files */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | EE1292431BFA0A91001C5E4B /* main.m */, 82 | ); 83 | name = "Supporting Files"; 84 | sourceTree = ""; 85 | }; 86 | EE1292591BFA0AE4001C5E4B /* RJHTTPDownloader */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | EE12925A1BFA0AE4001C5E4B /* RJHTTPDownloader.h */, 90 | EE12925B1BFA0AE4001C5E4B /* RJHTTPDownloader.m */, 91 | ); 92 | path = RJHTTPDownloader; 93 | sourceTree = ""; 94 | }; 95 | /* End PBXGroup section */ 96 | 97 | /* Begin PBXNativeTarget section */ 98 | EE12923E1BFA0A91001C5E4B /* RJDownloaderDemo */ = { 99 | isa = PBXNativeTarget; 100 | buildConfigurationList = EE1292561BFA0A91001C5E4B /* Build configuration list for PBXNativeTarget "RJDownloaderDemo" */; 101 | buildPhases = ( 102 | EE12923B1BFA0A91001C5E4B /* Sources */, 103 | EE12923C1BFA0A91001C5E4B /* Frameworks */, 104 | EE12923D1BFA0A91001C5E4B /* Resources */, 105 | ); 106 | buildRules = ( 107 | ); 108 | dependencies = ( 109 | ); 110 | name = RJDownloaderDemo; 111 | productName = RJDownloaderDemo; 112 | productReference = EE12923F1BFA0A91001C5E4B /* RJDownloaderDemo.app */; 113 | productType = "com.apple.product-type.application"; 114 | }; 115 | /* End PBXNativeTarget section */ 116 | 117 | /* Begin PBXProject section */ 118 | EE1292371BFA0A91001C5E4B /* Project object */ = { 119 | isa = PBXProject; 120 | attributes = { 121 | LastUpgradeCheck = 0710; 122 | ORGANIZATIONNAME = ArcSoft; 123 | TargetAttributes = { 124 | EE12923E1BFA0A91001C5E4B = { 125 | CreatedOnToolsVersion = 7.1; 126 | }; 127 | }; 128 | }; 129 | buildConfigurationList = EE12923A1BFA0A91001C5E4B /* Build configuration list for PBXProject "RJDownloaderDemo" */; 130 | compatibilityVersion = "Xcode 3.2"; 131 | developmentRegion = English; 132 | hasScannedForEncodings = 0; 133 | knownRegions = ( 134 | en, 135 | Base, 136 | ); 137 | mainGroup = EE1292361BFA0A91001C5E4B; 138 | productRefGroup = EE1292401BFA0A91001C5E4B /* Products */; 139 | projectDirPath = ""; 140 | projectRoot = ""; 141 | targets = ( 142 | EE12923E1BFA0A91001C5E4B /* RJDownloaderDemo */, 143 | ); 144 | }; 145 | /* End PBXProject section */ 146 | 147 | /* Begin PBXResourcesBuildPhase section */ 148 | EE12923D1BFA0A91001C5E4B /* Resources */ = { 149 | isa = PBXResourcesBuildPhase; 150 | buildActionMask = 2147483647; 151 | files = ( 152 | EE1292521BFA0A91001C5E4B /* LaunchScreen.storyboard in Resources */, 153 | EE12924F1BFA0A91001C5E4B /* Assets.xcassets in Resources */, 154 | EE12924D1BFA0A91001C5E4B /* Main.storyboard in Resources */, 155 | ); 156 | runOnlyForDeploymentPostprocessing = 0; 157 | }; 158 | /* End PBXResourcesBuildPhase section */ 159 | 160 | /* Begin PBXSourcesBuildPhase section */ 161 | EE12923B1BFA0A91001C5E4B /* Sources */ = { 162 | isa = PBXSourcesBuildPhase; 163 | buildActionMask = 2147483647; 164 | files = ( 165 | EE12925C1BFA0AE4001C5E4B /* RJHTTPDownloader.m in Sources */, 166 | EE12924A1BFA0A91001C5E4B /* ViewController.m in Sources */, 167 | EE1292471BFA0A91001C5E4B /* AppDelegate.m in Sources */, 168 | EE1292441BFA0A91001C5E4B /* main.m in Sources */, 169 | ); 170 | runOnlyForDeploymentPostprocessing = 0; 171 | }; 172 | /* End PBXSourcesBuildPhase section */ 173 | 174 | /* Begin PBXVariantGroup section */ 175 | EE12924B1BFA0A91001C5E4B /* Main.storyboard */ = { 176 | isa = PBXVariantGroup; 177 | children = ( 178 | EE12924C1BFA0A91001C5E4B /* Base */, 179 | ); 180 | name = Main.storyboard; 181 | sourceTree = ""; 182 | }; 183 | EE1292501BFA0A91001C5E4B /* LaunchScreen.storyboard */ = { 184 | isa = PBXVariantGroup; 185 | children = ( 186 | EE1292511BFA0A91001C5E4B /* Base */, 187 | ); 188 | name = LaunchScreen.storyboard; 189 | sourceTree = ""; 190 | }; 191 | /* End PBXVariantGroup section */ 192 | 193 | /* Begin XCBuildConfiguration section */ 194 | EE1292541BFA0A91001C5E4B /* Debug */ = { 195 | isa = XCBuildConfiguration; 196 | buildSettings = { 197 | ALWAYS_SEARCH_USER_PATHS = NO; 198 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 199 | CLANG_CXX_LIBRARY = "libc++"; 200 | CLANG_ENABLE_MODULES = YES; 201 | CLANG_ENABLE_OBJC_ARC = YES; 202 | CLANG_WARN_BOOL_CONVERSION = YES; 203 | CLANG_WARN_CONSTANT_CONVERSION = YES; 204 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 205 | CLANG_WARN_EMPTY_BODY = YES; 206 | CLANG_WARN_ENUM_CONVERSION = YES; 207 | CLANG_WARN_INT_CONVERSION = YES; 208 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 209 | CLANG_WARN_UNREACHABLE_CODE = YES; 210 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 211 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 212 | COPY_PHASE_STRIP = NO; 213 | DEBUG_INFORMATION_FORMAT = dwarf; 214 | ENABLE_STRICT_OBJC_MSGSEND = YES; 215 | ENABLE_TESTABILITY = YES; 216 | GCC_C_LANGUAGE_STANDARD = gnu99; 217 | GCC_DYNAMIC_NO_PIC = NO; 218 | GCC_NO_COMMON_BLOCKS = YES; 219 | GCC_OPTIMIZATION_LEVEL = 0; 220 | GCC_PREPROCESSOR_DEFINITIONS = ( 221 | "DEBUG=1", 222 | "$(inherited)", 223 | ); 224 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 225 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 226 | GCC_WARN_UNDECLARED_SELECTOR = YES; 227 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 228 | GCC_WARN_UNUSED_FUNCTION = YES; 229 | GCC_WARN_UNUSED_VARIABLE = YES; 230 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 231 | MTL_ENABLE_DEBUG_INFO = YES; 232 | ONLY_ACTIVE_ARCH = YES; 233 | SDKROOT = iphoneos; 234 | }; 235 | name = Debug; 236 | }; 237 | EE1292551BFA0A91001C5E4B /* Release */ = { 238 | isa = XCBuildConfiguration; 239 | buildSettings = { 240 | ALWAYS_SEARCH_USER_PATHS = NO; 241 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 242 | CLANG_CXX_LIBRARY = "libc++"; 243 | CLANG_ENABLE_MODULES = YES; 244 | CLANG_ENABLE_OBJC_ARC = YES; 245 | CLANG_WARN_BOOL_CONVERSION = YES; 246 | CLANG_WARN_CONSTANT_CONVERSION = YES; 247 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 248 | CLANG_WARN_EMPTY_BODY = YES; 249 | CLANG_WARN_ENUM_CONVERSION = YES; 250 | CLANG_WARN_INT_CONVERSION = YES; 251 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 252 | CLANG_WARN_UNREACHABLE_CODE = YES; 253 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 254 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 255 | COPY_PHASE_STRIP = NO; 256 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 257 | ENABLE_NS_ASSERTIONS = NO; 258 | ENABLE_STRICT_OBJC_MSGSEND = YES; 259 | GCC_C_LANGUAGE_STANDARD = gnu99; 260 | GCC_NO_COMMON_BLOCKS = YES; 261 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 262 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 263 | GCC_WARN_UNDECLARED_SELECTOR = YES; 264 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 265 | GCC_WARN_UNUSED_FUNCTION = YES; 266 | GCC_WARN_UNUSED_VARIABLE = YES; 267 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 268 | MTL_ENABLE_DEBUG_INFO = NO; 269 | SDKROOT = iphoneos; 270 | VALIDATE_PRODUCT = YES; 271 | }; 272 | name = Release; 273 | }; 274 | EE1292571BFA0A91001C5E4B /* Debug */ = { 275 | isa = XCBuildConfiguration; 276 | buildSettings = { 277 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 278 | INFOPLIST_FILE = RJDownloaderDemo/Info.plist; 279 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 280 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 281 | PRODUCT_BUNDLE_IDENTIFIER = ArcSoft.RJDownloaderDemo; 282 | PRODUCT_NAME = "$(TARGET_NAME)"; 283 | }; 284 | name = Debug; 285 | }; 286 | EE1292581BFA0A91001C5E4B /* Release */ = { 287 | isa = XCBuildConfiguration; 288 | buildSettings = { 289 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 290 | INFOPLIST_FILE = RJDownloaderDemo/Info.plist; 291 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 292 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 293 | PRODUCT_BUNDLE_IDENTIFIER = ArcSoft.RJDownloaderDemo; 294 | PRODUCT_NAME = "$(TARGET_NAME)"; 295 | }; 296 | name = Release; 297 | }; 298 | /* End XCBuildConfiguration section */ 299 | 300 | /* Begin XCConfigurationList section */ 301 | EE12923A1BFA0A91001C5E4B /* Build configuration list for PBXProject "RJDownloaderDemo" */ = { 302 | isa = XCConfigurationList; 303 | buildConfigurations = ( 304 | EE1292541BFA0A91001C5E4B /* Debug */, 305 | EE1292551BFA0A91001C5E4B /* Release */, 306 | ); 307 | defaultConfigurationIsVisible = 0; 308 | defaultConfigurationName = Release; 309 | }; 310 | EE1292561BFA0A91001C5E4B /* Build configuration list for PBXNativeTarget "RJDownloaderDemo" */ = { 311 | isa = XCConfigurationList; 312 | buildConfigurations = ( 313 | EE1292571BFA0A91001C5E4B /* Debug */, 314 | EE1292581BFA0A91001C5E4B /* Release */, 315 | ); 316 | defaultConfigurationIsVisible = 0; 317 | defaultConfigurationName = Release; 318 | }; 319 | /* End XCConfigurationList section */ 320 | }; 321 | rootObject = EE1292371BFA0A91001C5E4B /* Project object */; 322 | } 323 | -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/project.xcworkspace/xcuserdata/Ryan.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rjinxx/RJHTTPDownloader/b441ed861a0b320530a30fe72b3f030d919c8ea2/RJDownloaderDemo.xcodeproj/project.xcworkspace/xcuserdata/Ryan.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/project.xcworkspace/xcuserdata/Rylan.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rjinxx/RJHTTPDownloader/b441ed861a0b320530a30fe72b3f030d919c8ea2/RJDownloaderDemo.xcodeproj/project.xcworkspace/xcuserdata/Rylan.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/xcuserdata/Ryan.xcuserdatad/xcschemes/RJDownloaderDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/xcuserdata/Ryan.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | RJDownloaderDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | EE12923E1BFA0A91001C5E4B 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/xcuserdata/Rylan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/xcuserdata/Rylan.xcuserdatad/xcschemes/RJDownloaderDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /RJDownloaderDemo.xcodeproj/xcuserdata/Rylan.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | RJDownloaderDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | EE12923E1BFA0A91001C5E4B 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /RJDownloaderDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // RJDownloaderDemo 4 | // 5 | // Created by Ryan Jin on 11/16/15. 6 | // Copyright © 2015 ArcSoft. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /RJDownloaderDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // RJDownloaderDemo 4 | // 5 | // Created by Ryan Jin on 11/16/15. 6 | // Copyright © 2015 ArcSoft. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /RJDownloaderDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /RJDownloaderDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /RJDownloaderDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /RJDownloaderDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | NSAppTransportSecurity 26 | 27 | NSAllowsArbitraryLoads 28 | 29 | 30 | UILaunchStoryboardName 31 | LaunchScreen 32 | UIMainStoryboardFile 33 | Main 34 | UIRequiredDeviceCapabilities 35 | 36 | armv7 37 | 38 | UISupportedInterfaceOrientations 39 | 40 | UIInterfaceOrientationPortrait 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /RJDownloaderDemo/RJHTTPDownloader/RJHTTPDownloader.h: -------------------------------------------------------------------------------- 1 | // 2 | // RJHTTPDownloader.h 3 | // RJDownloaderDemo 4 | // 5 | // Created by Ryan Jin on 11/2/15. 6 | // Copyright (c) 2015 ArcSoft. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @class RJHTTPDownloader; 12 | 13 | @protocol RJHTTPDownloaderDelegate 14 | 15 | @optional 16 | 17 | - (void)RJHTTPDownloader:(RJHTTPDownloader *)downloader downloadProgress:(double)progress; 18 | - (void)RJHTTPDownloader:(RJHTTPDownloader *)downloader didFinishWithData:(NSData *)data; 19 | - (void)RJHTTPDownloader:(RJHTTPDownloader *)downloader didFailWithError:(NSError *)error; 20 | 21 | @end 22 | 23 | @interface RJHTTPDownloader : NSOperation 24 | 25 | - (id)initWithRequestURL:(NSURL *)URL delegate:(id)delegate; 26 | - (id)initWithRequestURL:(NSURL *)URL 27 | progress:(void (^)(float percent))progress 28 | completion:(void (^)(id response, NSError *error))completion; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /RJDownloaderDemo/RJHTTPDownloader/RJHTTPDownloader.m: -------------------------------------------------------------------------------- 1 | // 2 | // RJHTTPDownloader.m 3 | // RJDownloaderDemo 4 | // 5 | // Created by Ryan Jin on 11/2/15. 6 | // Copyright (c) 2015 ArcSoft. All rights reserved. 7 | // 8 | 9 | #import "RJHTTPDownloader.h" 10 | 11 | #define DELEGATE_HAS_METHOD(delegate, method) delegate && [delegate respondsToSelector:@selector(method)] 12 | 13 | typedef NS_ENUM(NSInteger, RJRequestState) { 14 | RJRequestStateReady = 0, 15 | RJRequestStateExecuting = 1, 16 | RJRequestStateFinished = 2, 17 | }; 18 | 19 | static const NSTimeInterval kRequestTimeout = 20.f; 20 | 21 | @interface RJHTTPDownloader () 22 | 23 | @property (nonatomic, strong) NSMutableData *fileData; 24 | @property (nonatomic, strong) NSMutableURLRequest *request; 25 | @property (nonatomic, strong) NSURLConnection *connection; 26 | 27 | @property (nonatomic, assign) float expectedLength; 28 | @property (nonatomic, assign) float receivedLength; 29 | @property (nonatomic, assign) RJRequestState state; 30 | @property (nonatomic, assign) CFRunLoopRef operationRunLoop; 31 | 32 | @property (nonatomic, weak) id delegate; 33 | @property (nonatomic, copy) void (^completion)(id response, NSError *error); 34 | @property (nonatomic, copy) void (^progress)(float percent); 35 | 36 | @end 37 | 38 | @implementation RJHTTPDownloader 39 | @synthesize state = _state; 40 | 41 | #pragma mark - Initialize Methods 42 | - (id)initWithRequestURL:(NSURL *)URL delegate:(id)delegate 43 | { 44 | return [self initWithRequestURL:URL delegate:delegate progress:nil completion:nil]; 45 | } 46 | 47 | - (id)initWithRequestURL:(NSURL *)URL 48 | progress:(void (^)(float percent))progress 49 | completion:(void (^)(id response, NSError *error))completion 50 | { 51 | return [self initWithRequestURL:URL delegate:nil progress:progress completion:completion]; 52 | } 53 | 54 | - (id)initWithRequestURL:(NSURL *)URL 55 | delegate:(id)delegate 56 | progress:(void (^)(float percent))progress 57 | completion:(void (^)(id response, NSError *error))completion 58 | { 59 | if (self = [super init]) { 60 | self.delegate = delegate; 61 | self.progress = progress; 62 | self.completion = completion; 63 | 64 | self.request = [NSMutableURLRequest requestWithURL:URL 65 | cachePolicy:NSURLRequestReloadIgnoringCacheData 66 | timeoutInterval:kRequestTimeout]; 67 | } 68 | return self; 69 | } 70 | 71 | - (void)dealloc 72 | { 73 | [self.connection cancel]; 74 | } 75 | 76 | #pragma mark - NSOperation Methods 77 | - (void)start 78 | { 79 | if (self.isCancelled) { [self finish]; return; } 80 | 81 | /* 82 | if (![NSThread isMainThread]) 83 | { 84 | [self performSelectorOnMainThread:@selector(start) 85 | withObject:nil 86 | waitUntilDone:NO]; 87 | return; 88 | } 89 | 90 | or 91 | 92 | [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 93 | self.connection = [NSURLConnection connectionWithRequest:self.request delegate:self]; 94 | }]; 95 | */ 96 | 97 | [self willChangeValueForKey:@"isExecuting"]; 98 | self.state = RJRequestStateExecuting; 99 | [self didChangeValueForKey:@"isExecuting"]; 100 | 101 | self.connection = [[NSURLConnection alloc] initWithRequest:self.request 102 | delegate:self 103 | startImmediately:NO]; 104 | 105 | NSOperationQueue *currentQueue = [NSOperationQueue currentQueue]; 106 | BOOL backgroundQueue = (currentQueue != nil && currentQueue != [NSOperationQueue mainQueue]); 107 | NSRunLoop *targetRunLoop = (backgroundQueue)?[NSRunLoop currentRunLoop]:[NSRunLoop mainRunLoop]; 108 | 109 | [self.connection scheduleInRunLoop:targetRunLoop forMode:NSRunLoopCommonModes]; 110 | [self.connection start]; 111 | 112 | // make NSRunLoop stick around until operation is finished 113 | if (backgroundQueue) { 114 | self.operationRunLoop = CFRunLoopGetCurrent(); CFRunLoopRun(); 115 | } 116 | } 117 | 118 | - (void)cancel 119 | { 120 | if (![self isExecuting]) return; 121 | 122 | [super cancel]; [self finish]; 123 | } 124 | 125 | - (BOOL)isConcurrent 126 | { 127 | return YES; 128 | } 129 | 130 | - (BOOL)isFinished 131 | { 132 | return self.state == RJRequestStateFinished; 133 | } 134 | 135 | - (BOOL)isExecuting 136 | { 137 | return self.state == RJRequestStateExecuting; 138 | } 139 | 140 | #pragma mark - Request Status 141 | - (void)finish 142 | { 143 | [self.connection cancel]; self.connection = nil; 144 | 145 | [self willChangeValueForKey:@"isExecuting"]; 146 | [self willChangeValueForKey:@"isFinished"]; 147 | self.state = RJRequestStateFinished; 148 | [self didChangeValueForKey:@"isExecuting"]; 149 | [self didChangeValueForKey:@"isFinished"]; 150 | } 151 | 152 | - (RJRequestState)state 153 | { 154 | @synchronized(self) { 155 | return _state; 156 | } 157 | } 158 | 159 | - (void)setState:(RJRequestState)newState 160 | { 161 | @synchronized(self) { 162 | [self willChangeValueForKey:@"state"]; 163 | _state = newState; 164 | [self didChangeValueForKey:@"state"]; 165 | } 166 | } 167 | 168 | #pragma mark - NSURLConnectionDelegate 169 | - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 170 | { 171 | self.expectedLength = response.expectedContentLength; 172 | self.receivedLength = 0; 173 | self.fileData = [NSMutableData data]; 174 | } 175 | 176 | - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 177 | { 178 | [self.fileData appendData:data]; self.receivedLength += data.length; 179 | 180 | float percent = self.receivedLength / self.expectedLength; 181 | 182 | if (self.progress) self.progress(percent); 183 | 184 | if (DELEGATE_HAS_METHOD(self.delegate, RJHTTPDownloader:downloadProgress:)) { 185 | [self.delegate RJHTTPDownloader:self downloadProgress:percent]; 186 | } 187 | } 188 | 189 | - (void)connectionDidFinishLoading:(NSURLConnection *)connection 190 | { 191 | [self downloadFinishedWithResponse:self.fileData error:nil]; 192 | } 193 | 194 | - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 195 | { 196 | [self downloadFinishedWithResponse:nil error:error]; 197 | } 198 | 199 | #pragma mark - Download Finished 200 | - (void)downloadFinishedWithResponse:(id)response error:(NSError *)error 201 | { 202 | if (self.operationRunLoop) CFRunLoopStop(self.operationRunLoop); 203 | 204 | if (self.isCancelled) return; 205 | 206 | if (self.completion) self.completion(self.fileData, error); 207 | 208 | if (response && DELEGATE_HAS_METHOD(self.delegate, RJHTTPDownloader:didFinishWithData:)) 209 | { 210 | [self.delegate RJHTTPDownloader:self didFinishWithData:response]; 211 | } 212 | else if (!response && DELEGATE_HAS_METHOD(self.delegate, RJHTTPDownloader:didFailWithError:)) 213 | { 214 | [self.delegate RJHTTPDownloader:self didFailWithError:error]; 215 | } 216 | 217 | [self finish]; 218 | } 219 | 220 | @end 221 | -------------------------------------------------------------------------------- /RJDownloaderDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // RJDownloaderDemo 4 | // 5 | // Created by Ryan Jin on 11/16/15. 6 | // Copyright © 2015 ArcSoft. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /RJDownloaderDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // RJDownloaderDemo 4 | // 5 | // Created by Ryan Jin on 11/16/15. 6 | // Copyright © 2015 ArcSoft. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "RJHTTPDownloader.h" 11 | 12 | NSString * const URL_STRING = @"http://sanjosetransit.com/extras/SJTransit_Icons.zip"; 13 | 14 | @interface ViewController () 15 | 16 | @property (nonatomic, strong) RJHTTPDownloader *downloader; 17 | @property (nonatomic, strong) NSOperationQueue *operationQueue; 18 | 19 | @property (nonatomic, weak) IBOutlet UIButton *button; 20 | @property (nonatomic, weak) IBOutlet UIProgressView *progress; 21 | 22 | @end 23 | 24 | @implementation ViewController 25 | 26 | - (void)viewDidLoad 27 | { 28 | [super viewDidLoad]; 29 | // Do any additional setup after loading the view, typically from a nib. 30 | self.operationQueue = [[NSOperationQueue alloc] init]; 31 | self.operationQueue.maxConcurrentOperationCount = 1; 32 | 33 | } 34 | 35 | - (IBAction)buttonAction:(UIButton *)sender 36 | { 37 | if ([sender.titleLabel.text isEqualToString:@"Download"]) { 38 | [self.button setTitle:@"Cancel" forState:UIControlStateNormal]; 39 | self.progress.progress = 0.f; 40 | 41 | NSURL *URL = [NSURL URLWithString:URL_STRING]; 42 | self.downloader = [[RJHTTPDownloader alloc] initWithRequestURL:URL progress:^(float percent) { 43 | dispatch_async(dispatch_get_main_queue(), ^{ 44 | self.progress.progress = percent; 45 | }); 46 | } completion:^(id response, NSError *error) { 47 | dispatch_async(dispatch_get_main_queue(), ^{ 48 | [self.button setTitle:@"Download" forState:UIControlStateNormal]; 49 | if (error) self.progress.progress = 0.f; 50 | }); 51 | }]; 52 | 53 | [self.operationQueue addOperation:self.downloader]; 54 | [self.operationQueue addOperationWithBlock:^{ 55 | NSLog(@"next operation"); 56 | }]; 57 | } 58 | else { 59 | [self.button setTitle:@"Download" forState:UIControlStateNormal]; 60 | self.progress.progress = 0.f; [self.downloader cancel]; 61 | } 62 | } 63 | 64 | - (void)didReceiveMemoryWarning { 65 | [super didReceiveMemoryWarning]; 66 | // Dispose of any resources that can be recreated. 67 | } 68 | 69 | @end 70 | -------------------------------------------------------------------------------- /RJDownloaderDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // RJDownloaderDemo 4 | // 5 | // Created by Ryan Jin on 11/16/15. 6 | // Copyright © 2015 ArcSoft. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------