├── .gitignore ├── KeepAppActive ├── KeepAppActive.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── KeepAppActive │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ ├── ViewController.h │ ├── ViewController.m │ ├── main.m │ └── pomodoSound.m4a └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xcuserstate 23 | 24 | ## Obj-C/Swift specific 25 | *.hmap 26 | *.ipa 27 | *.dSYM.zip 28 | *.dSYM 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | 55 | #Code Injection 56 | # 57 | # After new code Injection tools there's a generated folder /iOSInjectionProject 58 | # https://github.com/johnno1962/injectionforxcode 59 | 60 | iOSInjectionProject/ 61 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 59650A991E67C26A00CBABE1 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 59650A981E67C26A00CBABE1 /* main.m */; }; 11 | 59650A9C1E67C26A00CBABE1 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 59650A9B1E67C26A00CBABE1 /* AppDelegate.m */; }; 12 | 59650A9F1E67C26A00CBABE1 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 59650A9E1E67C26A00CBABE1 /* ViewController.m */; }; 13 | 59650AA21E67C26A00CBABE1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 59650AA01E67C26A00CBABE1 /* Main.storyboard */; }; 14 | 59650AA41E67C26A00CBABE1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 59650AA31E67C26A00CBABE1 /* Assets.xcassets */; }; 15 | 59650AA71E67C26A00CBABE1 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 59650AA51E67C26A00CBABE1 /* LaunchScreen.storyboard */; }; 16 | 59650AAF1E67C49700CBABE1 /* pomodoSound.m4a in Resources */ = {isa = PBXBuildFile; fileRef = 59650AAE1E67C49700CBABE1 /* pomodoSound.m4a */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 59650A941E67C26A00CBABE1 /* KeepAppActive.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KeepAppActive.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 59650A981E67C26A00CBABE1 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 22 | 59650A9A1E67C26A00CBABE1 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 23 | 59650A9B1E67C26A00CBABE1 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 24 | 59650A9D1E67C26A00CBABE1 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 25 | 59650A9E1E67C26A00CBABE1 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 26 | 59650AA11E67C26A00CBABE1 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | 59650AA31E67C26A00CBABE1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | 59650AA61E67C26A00CBABE1 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | 59650AA81E67C26A00CBABE1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | 59650AAE1E67C49700CBABE1 /* pomodoSound.m4a */ = {isa = PBXFileReference; lastKnownFileType = file; path = pomodoSound.m4a; sourceTree = ""; }; 31 | /* End PBXFileReference section */ 32 | 33 | /* Begin PBXFrameworksBuildPhase section */ 34 | 59650A911E67C26A00CBABE1 /* Frameworks */ = { 35 | isa = PBXFrameworksBuildPhase; 36 | buildActionMask = 2147483647; 37 | files = ( 38 | ); 39 | runOnlyForDeploymentPostprocessing = 0; 40 | }; 41 | /* End PBXFrameworksBuildPhase section */ 42 | 43 | /* Begin PBXGroup section */ 44 | 59650A8B1E67C26A00CBABE1 = { 45 | isa = PBXGroup; 46 | children = ( 47 | 59650A961E67C26A00CBABE1 /* KeepAppActive */, 48 | 59650A951E67C26A00CBABE1 /* Products */, 49 | ); 50 | sourceTree = ""; 51 | }; 52 | 59650A951E67C26A00CBABE1 /* Products */ = { 53 | isa = PBXGroup; 54 | children = ( 55 | 59650A941E67C26A00CBABE1 /* KeepAppActive.app */, 56 | ); 57 | name = Products; 58 | sourceTree = ""; 59 | }; 60 | 59650A961E67C26A00CBABE1 /* KeepAppActive */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | 59650AAE1E67C49700CBABE1 /* pomodoSound.m4a */, 64 | 59650A9A1E67C26A00CBABE1 /* AppDelegate.h */, 65 | 59650A9B1E67C26A00CBABE1 /* AppDelegate.m */, 66 | 59650A9D1E67C26A00CBABE1 /* ViewController.h */, 67 | 59650A9E1E67C26A00CBABE1 /* ViewController.m */, 68 | 59650AA01E67C26A00CBABE1 /* Main.storyboard */, 69 | 59650AA31E67C26A00CBABE1 /* Assets.xcassets */, 70 | 59650AA51E67C26A00CBABE1 /* LaunchScreen.storyboard */, 71 | 59650AA81E67C26A00CBABE1 /* Info.plist */, 72 | 59650A971E67C26A00CBABE1 /* Supporting Files */, 73 | ); 74 | path = KeepAppActive; 75 | sourceTree = ""; 76 | }; 77 | 59650A971E67C26A00CBABE1 /* Supporting Files */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 59650A981E67C26A00CBABE1 /* main.m */, 81 | ); 82 | name = "Supporting Files"; 83 | sourceTree = ""; 84 | }; 85 | /* End PBXGroup section */ 86 | 87 | /* Begin PBXNativeTarget section */ 88 | 59650A931E67C26A00CBABE1 /* KeepAppActive */ = { 89 | isa = PBXNativeTarget; 90 | buildConfigurationList = 59650AAB1E67C26A00CBABE1 /* Build configuration list for PBXNativeTarget "KeepAppActive" */; 91 | buildPhases = ( 92 | 59650A901E67C26A00CBABE1 /* Sources */, 93 | 59650A911E67C26A00CBABE1 /* Frameworks */, 94 | 59650A921E67C26A00CBABE1 /* Resources */, 95 | ); 96 | buildRules = ( 97 | ); 98 | dependencies = ( 99 | ); 100 | name = KeepAppActive; 101 | productName = KeepAppActive; 102 | productReference = 59650A941E67C26A00CBABE1 /* KeepAppActive.app */; 103 | productType = "com.apple.product-type.application"; 104 | }; 105 | /* End PBXNativeTarget section */ 106 | 107 | /* Begin PBXProject section */ 108 | 59650A8C1E67C26A00CBABE1 /* Project object */ = { 109 | isa = PBXProject; 110 | attributes = { 111 | LastUpgradeCheck = 0820; 112 | ORGANIZATIONNAME = "左博杨"; 113 | TargetAttributes = { 114 | 59650A931E67C26A00CBABE1 = { 115 | CreatedOnToolsVersion = 8.2.1; 116 | DevelopmentTeam = 72X2RKD3B2; 117 | ProvisioningStyle = Automatic; 118 | }; 119 | }; 120 | }; 121 | buildConfigurationList = 59650A8F1E67C26A00CBABE1 /* Build configuration list for PBXProject "KeepAppActive" */; 122 | compatibilityVersion = "Xcode 3.2"; 123 | developmentRegion = English; 124 | hasScannedForEncodings = 0; 125 | knownRegions = ( 126 | en, 127 | Base, 128 | ); 129 | mainGroup = 59650A8B1E67C26A00CBABE1; 130 | productRefGroup = 59650A951E67C26A00CBABE1 /* Products */; 131 | projectDirPath = ""; 132 | projectRoot = ""; 133 | targets = ( 134 | 59650A931E67C26A00CBABE1 /* KeepAppActive */, 135 | ); 136 | }; 137 | /* End PBXProject section */ 138 | 139 | /* Begin PBXResourcesBuildPhase section */ 140 | 59650A921E67C26A00CBABE1 /* Resources */ = { 141 | isa = PBXResourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | 59650AA71E67C26A00CBABE1 /* LaunchScreen.storyboard in Resources */, 145 | 59650AA41E67C26A00CBABE1 /* Assets.xcassets in Resources */, 146 | 59650AAF1E67C49700CBABE1 /* pomodoSound.m4a in Resources */, 147 | 59650AA21E67C26A00CBABE1 /* Main.storyboard in Resources */, 148 | ); 149 | runOnlyForDeploymentPostprocessing = 0; 150 | }; 151 | /* End PBXResourcesBuildPhase section */ 152 | 153 | /* Begin PBXSourcesBuildPhase section */ 154 | 59650A901E67C26A00CBABE1 /* Sources */ = { 155 | isa = PBXSourcesBuildPhase; 156 | buildActionMask = 2147483647; 157 | files = ( 158 | 59650A9F1E67C26A00CBABE1 /* ViewController.m in Sources */, 159 | 59650A9C1E67C26A00CBABE1 /* AppDelegate.m in Sources */, 160 | 59650A991E67C26A00CBABE1 /* main.m in Sources */, 161 | ); 162 | runOnlyForDeploymentPostprocessing = 0; 163 | }; 164 | /* End PBXSourcesBuildPhase section */ 165 | 166 | /* Begin PBXVariantGroup section */ 167 | 59650AA01E67C26A00CBABE1 /* Main.storyboard */ = { 168 | isa = PBXVariantGroup; 169 | children = ( 170 | 59650AA11E67C26A00CBABE1 /* Base */, 171 | ); 172 | name = Main.storyboard; 173 | sourceTree = ""; 174 | }; 175 | 59650AA51E67C26A00CBABE1 /* LaunchScreen.storyboard */ = { 176 | isa = PBXVariantGroup; 177 | children = ( 178 | 59650AA61E67C26A00CBABE1 /* Base */, 179 | ); 180 | name = LaunchScreen.storyboard; 181 | sourceTree = ""; 182 | }; 183 | /* End PBXVariantGroup section */ 184 | 185 | /* Begin XCBuildConfiguration section */ 186 | 59650AA91E67C26A00CBABE1 /* Debug */ = { 187 | isa = XCBuildConfiguration; 188 | buildSettings = { 189 | ALWAYS_SEARCH_USER_PATHS = NO; 190 | CLANG_ANALYZER_NONNULL = YES; 191 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 192 | CLANG_CXX_LIBRARY = "libc++"; 193 | CLANG_ENABLE_MODULES = YES; 194 | CLANG_ENABLE_OBJC_ARC = YES; 195 | CLANG_WARN_BOOL_CONVERSION = YES; 196 | CLANG_WARN_CONSTANT_CONVERSION = YES; 197 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 198 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 199 | CLANG_WARN_EMPTY_BODY = YES; 200 | CLANG_WARN_ENUM_CONVERSION = YES; 201 | CLANG_WARN_INFINITE_RECURSION = YES; 202 | CLANG_WARN_INT_CONVERSION = YES; 203 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 204 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 205 | CLANG_WARN_UNREACHABLE_CODE = YES; 206 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 207 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 208 | COPY_PHASE_STRIP = NO; 209 | DEBUG_INFORMATION_FORMAT = dwarf; 210 | ENABLE_STRICT_OBJC_MSGSEND = YES; 211 | ENABLE_TESTABILITY = YES; 212 | GCC_C_LANGUAGE_STANDARD = gnu99; 213 | GCC_DYNAMIC_NO_PIC = NO; 214 | GCC_NO_COMMON_BLOCKS = YES; 215 | GCC_OPTIMIZATION_LEVEL = 0; 216 | GCC_PREPROCESSOR_DEFINITIONS = ( 217 | "DEBUG=1", 218 | "$(inherited)", 219 | ); 220 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 221 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 222 | GCC_WARN_UNDECLARED_SELECTOR = YES; 223 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 224 | GCC_WARN_UNUSED_FUNCTION = YES; 225 | GCC_WARN_UNUSED_VARIABLE = YES; 226 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 227 | MTL_ENABLE_DEBUG_INFO = YES; 228 | ONLY_ACTIVE_ARCH = YES; 229 | SDKROOT = iphoneos; 230 | TARGETED_DEVICE_FAMILY = "1,2"; 231 | }; 232 | name = Debug; 233 | }; 234 | 59650AAA1E67C26A00CBABE1 /* Release */ = { 235 | isa = XCBuildConfiguration; 236 | buildSettings = { 237 | ALWAYS_SEARCH_USER_PATHS = NO; 238 | CLANG_ANALYZER_NONNULL = YES; 239 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 240 | CLANG_CXX_LIBRARY = "libc++"; 241 | CLANG_ENABLE_MODULES = YES; 242 | CLANG_ENABLE_OBJC_ARC = YES; 243 | CLANG_WARN_BOOL_CONVERSION = YES; 244 | CLANG_WARN_CONSTANT_CONVERSION = YES; 245 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 246 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 247 | CLANG_WARN_EMPTY_BODY = YES; 248 | CLANG_WARN_ENUM_CONVERSION = YES; 249 | CLANG_WARN_INFINITE_RECURSION = YES; 250 | CLANG_WARN_INT_CONVERSION = YES; 251 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 252 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 253 | CLANG_WARN_UNREACHABLE_CODE = YES; 254 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 255 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 256 | COPY_PHASE_STRIP = NO; 257 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 258 | ENABLE_NS_ASSERTIONS = NO; 259 | ENABLE_STRICT_OBJC_MSGSEND = YES; 260 | GCC_C_LANGUAGE_STANDARD = gnu99; 261 | GCC_NO_COMMON_BLOCKS = YES; 262 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 263 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 264 | GCC_WARN_UNDECLARED_SELECTOR = YES; 265 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 266 | GCC_WARN_UNUSED_FUNCTION = YES; 267 | GCC_WARN_UNUSED_VARIABLE = YES; 268 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 269 | MTL_ENABLE_DEBUG_INFO = NO; 270 | SDKROOT = iphoneos; 271 | TARGETED_DEVICE_FAMILY = "1,2"; 272 | VALIDATE_PRODUCT = YES; 273 | }; 274 | name = Release; 275 | }; 276 | 59650AAC1E67C26A00CBABE1 /* Debug */ = { 277 | isa = XCBuildConfiguration; 278 | buildSettings = { 279 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 280 | DEVELOPMENT_TEAM = 72X2RKD3B2; 281 | INFOPLIST_FILE = KeepAppActive/Info.plist; 282 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 283 | PRODUCT_BUNDLE_IDENTIFIER = com.zby.KeepAppActive; 284 | PRODUCT_NAME = "$(TARGET_NAME)"; 285 | }; 286 | name = Debug; 287 | }; 288 | 59650AAD1E67C26A00CBABE1 /* Release */ = { 289 | isa = XCBuildConfiguration; 290 | buildSettings = { 291 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 292 | DEVELOPMENT_TEAM = 72X2RKD3B2; 293 | INFOPLIST_FILE = KeepAppActive/Info.plist; 294 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 295 | PRODUCT_BUNDLE_IDENTIFIER = com.zby.KeepAppActive; 296 | PRODUCT_NAME = "$(TARGET_NAME)"; 297 | }; 298 | name = Release; 299 | }; 300 | /* End XCBuildConfiguration section */ 301 | 302 | /* Begin XCConfigurationList section */ 303 | 59650A8F1E67C26A00CBABE1 /* Build configuration list for PBXProject "KeepAppActive" */ = { 304 | isa = XCConfigurationList; 305 | buildConfigurations = ( 306 | 59650AA91E67C26A00CBABE1 /* Debug */, 307 | 59650AAA1E67C26A00CBABE1 /* Release */, 308 | ); 309 | defaultConfigurationIsVisible = 0; 310 | defaultConfigurationName = Release; 311 | }; 312 | 59650AAB1E67C26A00CBABE1 /* Build configuration list for PBXNativeTarget "KeepAppActive" */ = { 313 | isa = XCConfigurationList; 314 | buildConfigurations = ( 315 | 59650AAC1E67C26A00CBABE1 /* Debug */, 316 | 59650AAD1E67C26A00CBABE1 /* Release */, 317 | ); 318 | defaultConfigurationIsVisible = 0; 319 | }; 320 | /* End XCConfigurationList section */ 321 | }; 322 | rootObject = 59650A8C1E67C26A00CBABE1 /* Project object */; 323 | } 324 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // KeepAppActive 4 | // 5 | // Created by 左博杨 on 2017/3/2. 6 | // Copyright © 2017年 左博杨. 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 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // KeepAppActive 4 | // 5 | // Created by 左博杨 on 2017/3/2. 6 | // Copyright © 2017年 左博杨. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | @property (nonatomic, assign) UIBackgroundTaskIdentifier backgrounTask; 13 | @property (nonatomic,strong) NSTimer *timer; 14 | @end 15 | 16 | @implementation AppDelegate 17 | 18 | 19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | // Override point for customization after application launch. 21 | return YES; 22 | } 23 | 24 | 25 | - (void)applicationWillResignActive:(UIApplication *)application { 26 | // 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. 27 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 28 | } 29 | 30 | 31 | - (void)applicationDidEnterBackground:(UIApplication *)application { 32 | // 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. 33 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 34 | 35 | [self backgroundMode]; 36 | } 37 | 38 | -(void)backgroundMode{ 39 | //创建一个背景任务去和系统请求后台运行的时间 40 | self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 41 | [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask]; 42 | self.backgrounTask = UIBackgroundTaskInvalid; 43 | }]; 44 | self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(applyToSystemForMoreTime) userInfo:nil repeats:YES]; 45 | [self.timer fire]; 46 | } 47 | 48 | - (void)applyToSystemForMoreTime { 49 | if ([UIApplication sharedApplication].backgroundTimeRemaining < 30.0) {//如果剩余时间小于30秒 50 | [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask]; 51 | self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 52 | [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask]; 53 | self.self.backgrounTask = UIBackgroundTaskInvalid; 54 | }]; 55 | } 56 | } 57 | 58 | 59 | 60 | - (void)applicationWillEnterForeground:(UIApplication *)application { 61 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 62 | } 63 | 64 | 65 | - (void)applicationDidBecomeActive:(UIApplication *)application { 66 | // 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. 67 | } 68 | 69 | 70 | - (void)applicationWillTerminate:(UIApplication *)application { 71 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 72 | } 73 | 74 | 75 | @end 76 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/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 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/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 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/Base.lproj/Main.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 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/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 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIBackgroundModes 24 | 25 | audio 26 | 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | Main 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationLandscapeLeft 39 | UIInterfaceOrientationLandscapeRight 40 | 41 | UISupportedInterfaceOrientations~ipad 42 | 43 | UIInterfaceOrientationPortrait 44 | UIInterfaceOrientationPortraitUpsideDown 45 | UIInterfaceOrientationLandscapeLeft 46 | UIInterfaceOrientationLandscapeRight 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // KeepAppActive 4 | // 5 | // Created by 左博杨 on 2017/3/2. 6 | // Copyright © 2017年 左博杨. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // KeepAppActive 4 | // 5 | // Created by 左博杨 on 2017/3/2. 6 | // Copyright © 2017年 左博杨. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import 11 | #import 12 | 13 | @interface ViewController () 14 | 15 | @end 16 | 17 | @implementation ViewController{ 18 | AVAudioPlayer *player; 19 | } 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | // Do any additional setup after loading the view, typically from a nib. 24 | 25 | self.view.backgroundColor = [UIColor redColor]; 26 | 27 | //设置后台模式和锁屏模式下依然能够播放 28 | [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; 29 | [[AVAudioSession sharedInstance] setActive: YES error: nil]; 30 | 31 | //初始化音频播放器 32 | NSError *playerError; 33 | NSURL *urlSound = [[NSURL alloc]initWithString:[[NSBundle mainBundle]pathForResource:@"pomodoSound" ofType:@"m4a"]]; 34 | AVAudioPlayer *playerSound = [[AVAudioPlayer alloc] initWithContentsOfURL:urlSound error:&playerError]; 35 | playerSound.numberOfLoops = -1;//无限播放 36 | player = playerSound; 37 | 38 | } 39 | 40 | -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 41 | 42 | [player play]; 43 | } 44 | 45 | - (void)didReceiveMemoryWarning { 46 | [super didReceiveMemoryWarning]; 47 | // Dispose of any resources that can be recreated. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // KeepAppActive 4 | // 5 | // Created by 左博杨 on 2017/3/2. 6 | // Copyright © 2017年 左博杨. 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 | -------------------------------------------------------------------------------- /KeepAppActive/KeepAppActive/pomodoSound.m4a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BoYangZuo/KeepAppActive/bdaf1305b3f89f3ae96c553583d569dde20f119e/KeepAppActive/KeepAppActive/pomodoSound.m4a -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KeepAppActive 2 | a way to keep app active in background mode and you can also play audio in background mode 3 | --------------------------------------------------------------------------------