judaco / iPhone

0 stars 0 forks source link

iPhone - Class 11 #7

Open judaco opened 7 years ago

judaco commented 7 years ago
//
//  AppDelegate.m
//  Running Long Task
//
//  Created by hackeru on 21/05/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate
{
    UIBackgroundTaskIdentifier backTaskIdentifier;
    NSTimer * timer;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    backTaskIdentifier = UIBackgroundTaskInvalid;

    return YES;
}
-(void)tick:(NSTimer *)sender{

    NSTimeInterval timeRemaining = [UIApplication sharedApplication].backgroundTimeRemaining;

    NSLog(@"tick %e", timeRemaining);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // 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.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:NSSelectorFromString(@"tick:") userInfo:nil repeats:YES];
    backTaskIdentifier = [application beginBackgroundTaskWithName:@"task1" expirationHandler:^{
        [self endBackgroundTask];
    }];
}
-(void)endBackgroundTask{

    dispatch_async(dispatch_get_main_queue(), ^{
    [timer invalidate];
    timer = nil;
    [[UIApplication sharedApplication] endBackgroundTask:backTaskIdentifier];
    backTaskIdentifier = UIBackgroundTaskInvalid;
    });

}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // 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.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 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.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
judaco commented 7 years ago

capabilities backgroud modes on background fetch

judaco commented 7 years ago
//
//  AppDelegate.m
//  Background Fetch
//
//  Created by hackeru on 21/05/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "AppDelegate.h"
#import "NewsItem.h"

@implementation AppDelegate
{
    NSMutableArray<NewsItem *> * newsItems;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    newsItems = [NSMutableArray arrayWithObject:[[NewsItem alloc] initWithDate:[NSDate date] andContent:@"News item 1"]];
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 10);
    dispatch_after(popTime, dispatch_get_main_queue(), ^{
        NSNotification * notification = [NSNotification notificationWithName:@"notification1" object:nil];
        [NSNotificationCenter defaultCenter] postnotif
    });

    return YES;
}
-(BOOL)fetchNewsItem{
    NewsItem * newsItem = [[NewsItem alloc] initWithDate:[NSDate date] andContent:[NSString stringWithFormat:@"News item 1 %lu", newsItems.count+1]];
    //TODO:send notification 
    return YES;
}
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

    if ([self fetchNewsItem]) {
        completionHandler(UIBackgroundFetchResultNewData);
    }else{
        completionHandler(UIBackgroundFetchResultNoData);
    }

}

- (void)applicationWillResignActive:(UIApplication *)application {
    // 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.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 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.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // 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.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 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.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end