@@ -15,7 +15,7 @@ @implementation HybridMobileDeploy
15
15
+ (NSString *) getBundleFolderPath
16
16
{
17
17
NSString * home = NSHomeDirectory ();
18
- NSString * bundleFolder = [home stringByAppendingPathComponent: @" HybridMobileDeploy" ];
18
+ NSString * bundleFolder = [home stringByAppendingPathComponent: @" HybridMobileDeploy/bundle " ];
19
19
return bundleFolder;
20
20
}
21
21
@@ -26,6 +26,21 @@ + (NSString *) getBundlePath
26
26
return [bundleFolderPath stringByAppendingPathComponent: appBundleName];
27
27
}
28
28
29
+ + (NSString *) getPackageFolderPath
30
+ {
31
+ NSString * home = NSHomeDirectory ();
32
+ NSString * packageFolder = [home stringByAppendingPathComponent: @" HybridMobileDeploy/package" ];
33
+ return packageFolder;
34
+ }
35
+
36
+ + (NSString *) getPackagePath
37
+ {
38
+ NSString * packageFolderPath = [self getPackageFolderPath ];
39
+ NSString * appPackageName = @" localpackage.json" ;
40
+ return [packageFolderPath stringByAppendingPathComponent: appPackageName];
41
+ }
42
+
43
+
29
44
+ (NSURL *) getNativeBundleURL
30
45
{
31
46
return [[NSBundle mainBundle ] URLForResource: @" main" withExtension: @" jsbundle" ];
@@ -43,11 +58,16 @@ + (NSURL *) getBundleUrl
43
58
}
44
59
}
45
60
46
- - (void ) reloadBundle
61
+ + (void ) loadBundle : ( NSString *) rootComponent
47
62
{
48
63
dispatch_async (dispatch_get_main_queue (), ^{
49
- self.bridge .bundleURL = [HybridMobileDeploy getBundleUrl ];
50
- [self .bridge reload ];
64
+ RCTRootView *rootView = [[RCTRootView alloc ] initWithBundleURL: [self getBundleUrl ]
65
+ moduleName: rootComponent
66
+ launchOptions: nil ];
67
+
68
+ UIViewController *rootViewController = [[UIViewController alloc ] init ];
69
+ rootViewController.view = rootView;
70
+ [UIApplication sharedApplication ].delegate .window .rootViewController = rootViewController;
51
71
});
52
72
}
53
73
@@ -56,27 +76,28 @@ - (void) reloadBundle
56
76
callback (@[[NSNull null ], [HybridMobileDeployConfig getConfiguration ]]);
57
77
}
58
78
59
- RCT_EXPORT_METHOD (installUpdateFromUrl:(NSString *)updateUrl
79
+ RCT_EXPORT_METHOD (installUpdate:(NSDictionary *)updatePackage
80
+ packageJsonString:(NSString *) packageJsonString
60
81
callback:(RCTResponseSenderBlock)callback)
61
82
{
62
83
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
63
- NSURL * url = [NSURL URLWithString: updateUrl ];
84
+ NSURL * url = [NSURL URLWithString: updatePackage[ @" downloadUrl " ] ];
64
85
NSError *err;
65
86
66
87
NSString *updateContents = [[NSString alloc ] initWithContentsOfURL: url
67
88
encoding: NSUTF8StringEncoding
68
89
error: &err];
69
-
70
90
if (err) {
71
91
// TODO send download url
72
- callback (@[RCTMakeError (@" Error downloading url" , err, [[NSDictionary alloc ] initWithObjectsAndKeys: updateUrl ,@" updateUrl" , nil ])]);
92
+ callback (@[RCTMakeError (@" Error downloading url" , err, [[NSDictionary alloc ] initWithObjectsAndKeys: [url absoluteString ] ,@" updateUrl" , nil ])]);
73
93
} else {
74
94
dispatch_async (dispatch_get_main_queue (), ^{
75
95
NSError *saveError;
76
96
NSString *bundleFolderPath = [HybridMobileDeploy getBundleFolderPath ];
77
97
if (![[NSFileManager defaultManager ] fileExistsAtPath: bundleFolderPath]) {
78
98
[[NSFileManager defaultManager ] createDirectoryAtPath: bundleFolderPath withIntermediateDirectories: YES attributes: nil error: &saveError];
79
99
}
100
+
80
101
[updateContents writeToFile: [HybridMobileDeploy getBundlePath ]
81
102
atomically: YES
82
103
encoding: NSUTF8StringEncoding
@@ -85,12 +106,55 @@ - (void) reloadBundle
85
106
// TODO send file path
86
107
callback (@[RCTMakeError (@" Error saving file" , err, [[NSDictionary alloc ] initWithObjectsAndKeys: [HybridMobileDeploy getBundlePath ],@" bundlePath" , nil ])]);
87
108
} else {
88
- [self reloadBundle ];
89
- callback (@[[NSNull null ]]);
109
+ // Save the package info too.
110
+ NSString *packageFolderPath = [HybridMobileDeploy getPackageFolderPath ];
111
+ if (![[NSFileManager defaultManager ] fileExistsAtPath: packageFolderPath]) {
112
+ [[NSFileManager defaultManager ] createDirectoryAtPath: packageFolderPath withIntermediateDirectories: YES attributes: nil error: &saveError];
113
+ }
114
+
115
+ [packageJsonString writeToFile: [HybridMobileDeploy getPackagePath ]
116
+ atomically: YES
117
+ encoding: NSUTF8StringEncoding
118
+ error: &saveError];
119
+
120
+ if (saveError) {
121
+ callback (@[RCTMakeError (@" Error saving file" , err, [[NSDictionary alloc ] initWithObjectsAndKeys: [HybridMobileDeploy getPackagePath ],@" packagePath" , nil ])]);
122
+ } else {
123
+ [HybridMobileDeploy loadBundle: [HybridMobileDeployConfig getRootComponent ]];
124
+ callback (@[[NSNull null ]]);
125
+ }
90
126
}
91
127
});
92
128
}
93
129
});
94
130
}
95
131
132
+
133
+
134
+ RCT_EXPORT_METHOD (getLocalPackage: (RCTResponseSenderBlock)callback)
135
+ {
136
+
137
+ NSString *path = [HybridMobileDeploy getPackagePath ];
138
+
139
+ dispatch_async (dispatch_get_main_queue (), ^{
140
+
141
+ NSError * readError;
142
+ NSString *content = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: &readError];
143
+ if (readError) {
144
+ callback (@[RCTMakeError (@" Error finding local package " , readError, [[NSDictionary alloc ] initWithObjectsAndKeys: path,@" packagePath" , nil ]), [NSNull null ]]);
145
+ } else {
146
+ NSError * parseError;
147
+ NSData *data = [content dataUsingEncoding: NSUTF8StringEncoding];
148
+ NSDictionary * json = [NSJSONSerialization JSONObjectWithData: data
149
+ options: kNilOptions
150
+ error: &parseError];
151
+ if (parseError) {
152
+ callback (@[RCTMakeError (@" Error parsing contents of local package " , parseError, [[NSDictionary alloc ] initWithObjectsAndKeys: path,@" packagePath" , nil ]), [NSNull null ]]);
153
+ } else {
154
+ callback (@[[NSNull null ], json]);
155
+ }
156
+ }
157
+ });
158
+ }
159
+
96
160
@end
0 commit comments