Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Forked from arbesfeld/AppDelegate.m
Last active October 29, 2015 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhrrgn/3b80a0f0954d11369ec8 to your computer and use it in GitHub Desktop.
Save dhrrgn/3b80a0f0954d11369ec8 to your computer and use it in GitHub Desktop.
Update app on first load + display alert when new build becomes available.
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <AppHub/AppHub.h>
#import "AppDelegate.h"
#import "RCTBridge.h"
#import "RCTJavaScriptLoader.h"
#import "RCTRootView.h"
@interface AppDelegate() <RCTBridgeDelegate, UIAlertViewDelegate>
@end
@implementation AppDelegate {
RCTBridge *_bridge;
}
- (void)loadAppHubView:(NSDictionary *)launchOptions
{
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:_bridge
moduleName:@"AppHubStarterProject"
initialProperties:launchOptions];
self.window.rootViewController.view = rootView;
// Register a callback for when a new build becomes available.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(newBuildDidBecomeAvailable:)
name:AHBuildManagerDidMakeBuildAvailableNotification
object:nil];
}
- (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// TODO: replace the following with your ApplicationID.
[AppHub setApplicationID:@"4ZmZkkGddeEmB7qb3UTu"];
_bridge = [[RCTBridge alloc] initWithDelegate:self
launchOptions:launchOptions];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
// Check whether this is the first time that the user has booted the app.
// If they have already been seen, just show the cached view.
// If not, poll AppHub for the latest build and show the view on completion.
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ALREADY_SEEN"]) {
[self loadAppHubView:launchOptions];
} else {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ALREADY_SEEN"];
/* TODO: show a UIViewController with a loading view here. */
// rootViewController.view = [[MyLoadingViewController alloc] init];
__block int maxTries = 3; // How many times to poll AppHub for the latest build.
__block float pollSeconds = 0.1; // The interval at which to poll AppHub.
__block AHBuildResultBlock block = ^(AHBuild *result, NSError *error) {
/*
* If result is not nil, then it is guaranteed to be the most up-to-date build, or it is
* the App Store build if there are no new AppHub builds.
*
* If we poll AppHub 3 times with no success, just give up.
*/
if (result || maxTries <= 0) {
dispatch_async(dispatch_get_main_queue(), ^{
// Show the UI on the main thread.
[self loadAppHubView:launchOptions];
block = nil;
});
} else {
maxTries--;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(pollSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[AppHub buildManager] fetchBuildWithCompletionHandler: block];
});
};
};
[[AppHub buildManager] fetchBuildWithCompletionHandler:block];
}
return YES;
}
#pragma mark - RCTBridgeDelegate
- (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge
{
NSURL *sourceURL;
/**
* Loading JavaScript code - uncomment the one you want.
*
* OPTION 1
* Load from development server. Start the server from the repository root:
*
* $ npm start
*
* To run on device, change `localhost` to the IP address of your computer
* (you can get this by typing `ifconfig` into the terminal and selecting the
* `inet` value under `en0:`) and make sure your computer and iOS device are
* on the same Wi-Fi network.
*/
// sourceURL = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle"];
/**
* OPTION 2 - AppHub
*
* Load cached code and images from AppHub. Use this when deploying to test
* users and the App Store.
*
* Make sure to re-generate the static bundle by navigating to your Xcode project
* folder and running
*
* $ curl 'http://localhost:8081/index.ios.bundle' -o main.jsbundle
*
* then add the `main.jsbundle` file to your project.
*/
AHBuild *build = [[AppHub buildManager] currentBuild];
sourceURL = [build.bundle URLForResource:@"main"
withExtension:@"jsbundle"];
return sourceURL;
}
- (void)loadSourceForBridge:(RCTBridge *)bridge
withBlock:(RCTSourceLoadBlock)loadCallback
{
[RCTJavaScriptLoader loadBundleAtURL:[self sourceURLForBridge:bridge]
onComplete:loadCallback];
}
#pragma mark - NSNotificationCenter
-(void) newBuildDidBecomeAvailable:(NSNotification *)notification {
// Show an alert view when a new build becomes available. The user can choose to "Update" the app, or "Cancel".
// If the user preses "Cancel", their app will update when they restart the app.
AHBuild *build = notification.userInfo[AHBuildManagerBuildKey];
NSString *alertMessage = [NSString stringWithFormat:@"There's a new update available.\n\nUpdate description:\n\n %@", build.buildDescription];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Great news!"
message:alertMessage
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Update", nil];
dispatch_async(dispatch_get_main_queue(), ^{
// Show the alert on the main thread.
[alert show];
});
}
#pragma mark - UIAlertViewDelegate
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
// The user pressed "update".
[_bridge reload];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment