|
1 | 1 | #import "HybridMobileDeploy.h"
|
2 | 2 |
|
| 3 | +#import "RCTRootView.h" |
| 4 | +#import "RCTUtils.h" |
| 5 | + |
3 | 6 | @implementation HybridMobileDeploy
|
4 | 7 |
|
5 | 8 | RCT_EXPORT_MODULE()
|
6 | 9 |
|
7 |
| -RCT_EXPORT_METHOD(test) |
| 10 | ++ (NSString *) getBundleFolderPath |
| 11 | +{ |
| 12 | + NSString* home = NSHomeDirectory(); |
| 13 | + NSString* bundleFolder = [home stringByAppendingPathComponent:@"HybridMobileDeploy"]; |
| 14 | + return bundleFolder; |
| 15 | +} |
| 16 | + |
| 17 | ++ (NSString *) getBundlePath:(NSString*)bundleName |
| 18 | +{ |
| 19 | + NSString * bundleFolderPath = [self getBundleFolderPath]; |
| 20 | + NSString* appBundleName = [bundleName stringByAppendingString:@".jsbundle"]; |
| 21 | + return [bundleFolderPath stringByAppendingPathComponent:appBundleName]; |
| 22 | +} |
| 23 | + |
| 24 | ++ (NSURL *) getNativeBundleURL:(NSString*)bundleName |
8 | 25 | {
|
9 |
| - // Your implementation here |
| 26 | + return [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"jsbundle"]; |
| 27 | +} |
| 28 | + |
| 29 | ++ (NSURL *) appBundleUrl:(NSString*)bundleName { |
| 30 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 31 | + |
| 32 | + NSString *bundlePath = [self getBundlePath:bundleName]; |
| 33 | + if ([fileManager fileExistsAtPath:bundlePath]) { |
| 34 | + return [[NSURL alloc] initFileURLWithPath:bundlePath]; |
| 35 | + } else { |
| 36 | + return [self getNativeBundleURL:bundleName]; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | ++ (void) loadBundle:(NSString*)moduleName |
| 41 | +{ |
| 42 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 43 | + RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:[self appBundleUrl:moduleName] |
| 44 | + moduleName:moduleName |
| 45 | + launchOptions:nil]; |
| 46 | + |
| 47 | + UIViewController *rootViewController = [[UIViewController alloc] init]; |
| 48 | + rootViewController.view = rootView; |
| 49 | + [UIApplication sharedApplication].delegate.window.rootViewController = rootViewController; |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +RCT_EXPORT_METHOD(installUpdateFromUrl:(NSString*)updateUrl |
| 54 | + bundleName:(NSString*)bundleName |
| 55 | + failureCallback:(RCTResponseSenderBlock)failureCallback |
| 56 | + successCallback:(RCTResponseSenderBlock)successCallback) |
| 57 | +{ |
| 58 | + NSError *parameterError; |
| 59 | + NSMutableDictionary *errorData; |
| 60 | + if (!updateUrl) { |
| 61 | + errorData = [NSMutableDictionary dictionary]; |
| 62 | + [errorData setValue:@"missing-updateUrl" forKey:NSLocalizedDescriptionKey]; |
| 63 | + } else if (!bundleName) { |
| 64 | + errorData = [NSMutableDictionary dictionary]; |
| 65 | + [errorData setValue:@"missing-bundleName" forKey:NSLocalizedDescriptionKey]; |
| 66 | + } |
| 67 | + |
| 68 | + if (errorData) { |
| 69 | + parameterError = [NSError errorWithDomain:@"HybridMobileDeploy"code:200 userInfo:errorData]; |
| 70 | + NSDictionary *rctError = RCTMakeError(@"Error with input to installUpdateFromUrl", parameterError, errorData); |
| 71 | + failureCallback(@[rctError]); |
| 72 | + } else { |
| 73 | + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 74 | + NSURL* url = [NSURL URLWithString:updateUrl]; |
| 75 | + NSError *err; |
| 76 | + |
| 77 | + NSString *updateContents = [[NSString alloc] initWithContentsOfURL:url |
| 78 | + encoding:NSUTF8StringEncoding |
| 79 | + error:&err]; |
| 80 | + if (err) { |
| 81 | + failureCallback(@[err]); |
| 82 | + } else { |
| 83 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 84 | + NSError *saveError; |
| 85 | + NSString *bundleFolderPath = [HybridMobileDeploy getBundleFolderPath]; |
| 86 | + if (![[NSFileManager defaultManager] fileExistsAtPath:bundleFolderPath]) { |
| 87 | + [[NSFileManager defaultManager] createDirectoryAtPath:bundleFolderPath withIntermediateDirectories:YES attributes:nil error:&saveError]; |
| 88 | + } |
| 89 | + [updateContents writeToFile:[HybridMobileDeploy getBundlePath:bundleName] |
| 90 | + atomically:YES |
| 91 | + encoding:NSUTF8StringEncoding |
| 92 | + error:&saveError]; |
| 93 | + if (saveError) { |
| 94 | + failureCallback(@[saveError]); |
| 95 | + } else { |
| 96 | + [HybridMobileDeploy loadBundle:bundleName]; |
| 97 | + successCallback(@[]); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
10 | 103 | }
|
11 | 104 |
|
12 | 105 | @end
|
0 commit comments