-
Notifications
You must be signed in to change notification settings - Fork 4
/
AppDelegate.m
363 lines (309 loc) · 12.1 KB
/
AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// AppDelegate.m
// GreekInterlinearBible
//
// Created by Kerri Shotts on 1/20/12.
// Copyright photoKandy Studios LLC 2012. All rights reserved.
//
#import "AppDelegate.h"
#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PhoneGapViewController.h>
#else
#import "PhoneGapViewController.h"
#endif
@implementation AppDelegate
@synthesize invokeString;
- (id) init
{
/** If you need to do any extra app-specific initialization, you can do it here
* -jm
**/
return [super init];
}
/**
* This is main kick off after the app inits, the views and Settings are setup here. (preferred - iOS4 and up)
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Uncomment to enable remote debugging
//[NSClassFromString(@"WebView") _enableRemoteInspector];
[NSClassFromString(@"WebView") performSelector:@selector(_enableRemoteInspector)];
NSURL* url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
if (url && [url isKindOfClass:[NSURL class]])
{
self.invokeString = [url absoluteString];
NSLog(@"GreekInterlinearBible launchOptions = %@",url);
}
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
// this happens while we are running ( in the background, or from within our own app )
// only valid if GreekInterlinearBible.plist specifies a protocol to handle
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
// must call super so all plugins will get the notification, and their handlers will be called
// super also calls into javascript global function 'handleOpenURL'
return [super application:application handleOpenURL:url];
}
-(id) getCommandInstance:(NSString*)className
{
/** You can catch your own commands here, if you wanted to extend the gap: protocol, or add your
* own app specific protocol to it. -jm
**/
return [super getCommandInstance:className];
}
/**
Called when the webview finishes loading. This stops the activity view and closes the imageview
*/
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
// only valid if GreekInterlinearBible.plist specifies a protocol to handle
if(self.invokeString)
{
// this is passed before the deviceready event is fired, so you can access it in js when you receive deviceready
NSString* jsString = [NSString stringWithFormat:@"var invokeString = \"%@\";", self.invokeString];
[theWebView stringByEvaluatingJavaScriptFromString:jsString];
}
// Black base color for background matches the native apps
theWebView.backgroundColor = [UIColor blackColor];
for (id subview in theWebView.subviews)
{
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
{
((UIScrollView *)subview).bounces = NO;
}
}
// register for keyboard show event
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
return [ super webViewDidFinishLoad:theWebView ];
}
// hiding UIWebviewAccessary
// http://ios-blog.co.uk/iphone-development-tutorials/rich-text-editing-a-simple-start-part-1/
- (void)keyboardWillShow:(NSNotification *)note {
[self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
}
- (void)removeBar {
// Locate non-UIWindow.
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView.
for (UIView *possibleFormView in [keyboardWindow subviews]) {
// iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) {
[subviewWhichIsPossibleFormView removeFromSuperview];
}
}
}
}
}
/*
*
* PERSISTENT LOCALSTORAGE NATIVE SOLUTION for phonegap (tested on 1.3)
* iOS 5.1 puts LocalStorage in a vulnerable location (Libary/Caches). This will copy it to
* the documents folder as "appdata.db" so that it will be appropriately backed-up and not
* overwritten. If "appdata.db" doesn't exist, localStorage will not be overwritten.
* Also, upon application suspend (resignActive) or termination, localStorage is saved, so
* there should not be any chance that changes to localStorage won't be persisted. [[ short of a power-cycle ]]
*
* The following should be placed in AppDelegate.m. Note that it //partially// replaces
* webViewDidStartLoad:. The remainder of webViewDidStartLoad: (as in phonegap 1.3) is below,
* replace/remove as appropriate
*
* @Author: Kerri Shotts (2012)
* @License: MIT License
*
* Notes: USES ARC. IF USING MRC, apply the appropriate releases.
*
* Based partially on http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html
*
*/
- (BOOL)fileExists: (NSString*) theFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
return [fileManager fileExistsAtPath:theFile];
// MRC: make sure to release
}
- (void)copyFile:(NSString*) sourceFile to:(NSString*) targetPath withName:(NSString*) targetFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:targetPath withIntermediateDirectories:YES attributes:nil error:NULL];
NSString *fullTargetFile = [targetPath stringByAppendingPathComponent:targetFile];
NSLog(@"Source File for Copy: %@", sourceFile);
NSLog(@"Target File for Copy: %@", fullTargetFile);
if ( [self fileExists:fullTargetFile] )
{
// remove the file first. (Ick! I wish there was a better way...
if ( [fileManager removeItemAtPath:fullTargetFile error:nil] == YES )
{
NSLog (@"Target successfully removed.");
}
else
{
NSLog (@"Target could not be removed prior to copy. No copy will occur.");
return;
}
}
if ( [fileManager copyItemAtPath:sourceFile toPath:fullTargetFile error:nil] == YES)
{
NSLog(@"Copy successful.");
}
else
{
NSLog(@"Copy unsuccessful.");
}
// MRC: don't forget to release fileManager where necessary!
}
- (BOOL)isIOS5_1OrHigher
{
// based on: http://stackoverflow.com/a/9320041
NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ( [[versionCompatibility objectAtIndex:0] intValue] > 5 )
{
return YES; // iOS 6+
}
if ( [[versionCompatibility objectAtIndex:0] intValue] < 5 )
{
return NO; // iOS 4.x or lower
}
if ( [[versionCompatibility objectAtIndex:1] intValue] >= 1 )
{
return YES; // ios 5.<<1>> or higher
}
return NO; // ios 5.<<0.x>> or lower
}
- (void)copyPersistentStorageToLocalStorage
{
// build localStorage path: ~/Library/WebKit/LocalStorage/file__0.localstorage (for iOS < 5.1)
// ~/Library/Caches/file__0.localstorage (for iOS >= 5.1 )
NSString *localStoragePath;
if ( [self isIOS5_1OrHigher] )
{
// for IOS >= 5.1
localStoragePath =
[
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"Caches"
];
}
else
{
// for IOS < 5.1;
localStoragePath =
[
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"WebKit/LocalStorage"
];
}
// build persistentStorage path: ~/Documents/appdata.db
NSString *persistentStoragePath =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *persistentStorageFile = [persistentStoragePath stringByAppendingPathComponent:@"appdata.db"];
// does the persistent store exist?
if ([self fileExists:persistentStorageFile ])
{
// it does, copy it over localStorage
NSLog(@"Copying persistent storage to local storage.");
[self copyFile:persistentStorageFile to:localStoragePath withName: @"file__0.localstorage"];
}
else
{
NSLog(@"No persistent storage to copy. Using local storage only.");
}
}
- (void)copyLocalStorageToPersistentStorage
{
// build localStorage path: ~/Library/WebKit/LocalStorage/file__0.localstorage (for iOS < 5.1)
// ~/Library/Caches/file__0.localstorage (for iOS >= 5.1 )
NSString *localStoragePath;
if ( [self isIOS5_1OrHigher] )
{
// for IOS >= 5.1
localStoragePath =
[
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"Caches"
];
}
else
{
// for IOS < 5.1;
localStoragePath =
[
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"WebKit/LocalStorage"
];
}
NSString *localStorageFile = [localStoragePath stringByAppendingPathComponent:@"file__0.localstorage"];
// build persistentStorage path: ~/Documents/appdata.db
NSString *persistentStoragePath =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// does the local store exist? (it almost always will)
if ([self fileExists:localStorageFile ])
{
// it does, copy it over persistent Storage
NSLog(@"Copying local storage to persistent storage.");
[self copyFile:localStorageFile to:persistentStoragePath withName:@"appdata.db"];
}
else
{
NSLog(@"No local storage to copy. Using local storage only.");
}
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// move the local storage data to persistent storage
// while we're resigning so that we know our data is safe...
[self copyLocalStorageToPersistentStorage];
return;
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// move the local storage data to persistent storage
// while we're terminating so that we know our data is safe...
[self copyLocalStorageToPersistentStorage];
return;
}
- (void)webViewDidStartLoad:(UIWebView *)theWebView
{
[self copyPersistentStorageToLocalStorage];
//
// END Persistent LocalStorage solution. Remainder of webViewDidStartLoad: should be used.
//
return [ super webViewDidStartLoad:theWebView ];
}
/**
* Fail Loading With Error
* Error - If the webpage failed to load display an error with the reason.
*/
- (void)webView:(UIWebView *)theWebView didFailLoadWithError:(NSError *)error
{
// return [ super webView:theWebView didFailLoadWithError:error ];
}
/**
* Start Loading Request
* This is where most of the magic happens... We take the request(s) and process the response.
* From here we can redirect links and other protocols to different internal methods.
*/
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
- (BOOL) execute:(InvokedUrlCommand*)command
{
return [ super execute:command];
}
- (void)dealloc
{
//[ super dealloc ];
}
@end