Files
react-native-code-push/HybridMobileDeploy.m
2015-07-24 09:25:36 -07:00

97 lines
3.6 KiB
Objective-C

#import "HybridMobileDeploy.h"
#import "RCTRootView.h"
#import "RCTUtils.h"
@implementation HybridMobileDeploy
RCT_EXPORT_MODULE()
+ (NSString *) getBundleFolderPath
{
NSString* home = NSHomeDirectory();
NSString* bundleFolder = [home stringByAppendingPathComponent:@"HybridMobileDeploy"];
return bundleFolder;
}
+ (NSString *) getBundlePath
{
NSString * bundleFolderPath = [self getBundleFolderPath];
NSString* appBundleName = @"main.jsbundle";
return [bundleFolderPath stringByAppendingPathComponent:appBundleName];
}
+ (NSURL *) getNativeBundleURL
{
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
}
+ (NSURL *) getBundleUrl
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *bundlePath = [self getBundlePath];
if ([fileManager fileExistsAtPath:bundlePath]) {
return [[NSURL alloc] initFileURLWithPath:bundlePath];
} else {
return [self getNativeBundleURL];
}
}
+ (void) loadBundle:(NSString*)rootComponent
{
dispatch_async(dispatch_get_main_queue(), ^{
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:[self getBundleUrl]
moduleName:rootComponent
launchOptions:nil];
UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view = rootView;
[UIApplication sharedApplication].delegate.window.rootViewController = rootViewController;
});
}
RCT_EXPORT_METHOD(getConfiguration:(RCTResponseSenderBlock)callback)
{
callback(@[[NSNull null], [HybridMobileDeployConfig getConfiguration]]);
}
RCT_EXPORT_METHOD(installUpdateFromUrl:(NSString*)updateUrl
callback:(RCTResponseSenderBlock)callback)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL* url = [NSURL URLWithString:updateUrl];
NSError *err;
NSString *updateContents = [[NSString alloc] initWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&err];
if (err) {
// TODO send download url
callback(@[RCTMakeError(@"Error downloading url", err, [[NSDictionary alloc] initWithObjectsAndKeys:updateUrl,@"updateUrl", nil])]);
} else {
dispatch_async(dispatch_get_main_queue(), ^{
NSError *saveError;
NSString *bundleFolderPath = [HybridMobileDeploy getBundleFolderPath];
if (![[NSFileManager defaultManager] fileExistsAtPath:bundleFolderPath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:bundleFolderPath withIntermediateDirectories:YES attributes:nil error:&saveError];
}
[updateContents writeToFile:[HybridMobileDeploy getBundlePath]
atomically:YES
encoding:NSUTF8StringEncoding
error:&saveError];
if (saveError) {
// TODO send file path
callback(@[RCTMakeError(@"Error saving file", err, [[NSDictionary alloc] initWithObjectsAndKeys:[HybridMobileDeploy getBundlePath],@"bundlePath", nil])]);
} else {
[HybridMobileDeploy loadBundle:[HybridMobileDeployConfig getRootComponent]];
callback(@[[NSNull null]]);
}
});
}
});
}
@end