ObjectiveCeeds › iOS 7: Background Fetch

iOS 7: Background Fetch

Von Manfred Kreß
Code
Seite 3 von 3



Und hier noch einmal der komplette Code des Mamutprojekts. Eine Klasse mit zwei Methoden. OCAppDelegate.h
#import 

@interface OCAppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;

@end
OCAppDelegate.m

#import "OCAppDelegate.h"


@interface OCAppDelegate ()

@property (readonly) UIViewController *viewController;

@end

@implementation OCAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    /*
        Enable Background Fetch
     */
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    _viewController = [[UIViewController alloc] init];
    [[_viewController view] setBackgroundColor: [UIColor yellowColor]];
    [_viewController setTitle: @"ObjectiveCeeds"];
    
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController: _viewController];
    [[self window] setRootViewController: nc];
    
    self.window.backgroundColor = [UIColor blackColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"Background fetch");
    
    NSError *e = nil;
    
    NSURL *url = [NSURL URLWithString: @"http://objectiveceeds.com/app/color.php"];
    
    NSData *d = [[NSData alloc] initWithContentsOfURL: url
                                              options: NSDataReadingUncached
                                                error: &e];
    
    if (e != nil) // Primitive Error Handling
    {
        NSLog(@"Fetch error %@", e);
        completionHandler (UIBackgroundFetchResultFailed);
        return;
    }
    
    e = nil;
    NSDictionary *colorDict = [NSJSONSerialization JSONObjectWithData: d
                                                              options: 0
                                                                error: &e];
    
    if (e != nil)
    {
        NSLog(@"JSON error %@", e);
        completionHandler (UIBackgroundFetchResultFailed);
        return;
    }
    
    CGFloat red = [[colorDict objectForKey: @"red"] floatValue];
    CGFloat green = [[colorDict objectForKey: @"green"] floatValue];
    CGFloat blue = [[colorDict objectForKey: @"blue"] floatValue];
    
    
    // update UI
    UIColor *newColor = [UIColor colorWithRed: red
                                        green: green
                                         blue: blue
                                        alpha: 1.0];
    
    [[_viewController view] setBackgroundColor: newColor];
    
    // fire Notification
    UILocalNotification *note = [[UILocalNotification alloc] init];
    [note setAlertBody: @"Color did change"];
    [[UIApplication sharedApplication] scheduleLocalNotification: note];

    // call Completition Handler
    completionHandler (UIBackgroundFetchResultNewData);
}

@end


« vorige Seite