ios version
[wl-mobile.git] / Classes / AppDelegate.m
1 //
2 //  AppDelegate.m
3 //  wl-mobi
4 //
5 //  Created by FNP on 10/19/11.
6 //  Copyright __MyCompanyName__ 2011. All rights reserved.
7 //
8
9 #import "AppDelegate.h"
10 #ifdef PHONEGAP_FRAMEWORK
11         #import <PhoneGap/PhoneGapViewController.h>
12 #else
13         #import "PhoneGapViewController.h"
14 #endif
15
16 @implementation AppDelegate
17
18 @synthesize invokeString;
19
20 - (id) init
21 {       
22         /** If you need to do any extra app-specific initialization, you can do it here
23          *  -jm
24          **/
25     return [super init];
26 }
27
28 /**
29  * This is main kick off after the app inits, the views and Settings are setup here. (preferred - iOS4 and up)
30  */
31 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
32 {
33         
34         NSArray *keyArray = [launchOptions allKeys];
35         if ([launchOptions objectForKey:[keyArray objectAtIndex:0]]!=nil) 
36         {
37                 NSURL *url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
38                 self.invokeString = [url absoluteString];
39                 NSLog(@"wl-mobi launchOptions = %@",url);
40         }
41         
42         return [super application:application didFinishLaunchingWithOptions:launchOptions];
43 }
44
45 // this happens while we are running ( in the background, or from within our own app )
46 // only valid if wl-mobi.plist specifies a protocol to handle
47 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
48 {
49     // must call super so all plugins will get the notification, and their handlers will be called 
50         // super also calls into javascript global function 'handleOpenURL'
51     return [super application:application handleOpenURL:url];
52 }
53
54 -(id) getCommandInstance:(NSString*)className
55 {
56         /** You can catch your own commands here, if you wanted to extend the gap: protocol, or add your
57          *  own app specific protocol to it. -jm
58          **/
59         return [super getCommandInstance:className];
60 }
61
62 /**
63  Called when the webview finishes loading.  This stops the activity view and closes the imageview
64  */
65 - (void)webViewDidFinishLoad:(UIWebView *)theWebView 
66 {
67         // only valid if wl-mobi.plist specifies a protocol to handle
68         if(self.invokeString)
69         {
70                 // this is passed before the deviceready event is fired, so you can access it in js when you receive deviceready
71                 NSString* jsString = [NSString stringWithFormat:@"var invokeString = \"%@\";", self.invokeString];
72                 [theWebView stringByEvaluatingJavaScriptFromString:jsString];
73         }
74         return [ super webViewDidFinishLoad:theWebView ];
75 }
76
77 - (void)webViewDidStartLoad:(UIWebView *)theWebView 
78 {
79         return [ super webViewDidStartLoad:theWebView ];
80 }
81
82 /**
83  * Fail Loading With Error
84  * Error - If the webpage failed to load display an error with the reason.
85  */
86 - (void)webView:(UIWebView *)theWebView didFailLoadWithError:(NSError *)error 
87 {
88         return [ super webView:theWebView didFailLoadWithError:error ];
89 }
90
91
92 /**
93  * Start Loading Request
94  * This is where most of the magic happens... We take the request(s) and process the response.
95  * From here we can re direct links and other protocalls to different internal methods.
96  */
97 - (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
98 {
99         NSURL *url = [request URL];
100         
101         // Intercept the external http requests and forward to Safari.app
102         // Otherwise forward to the PhoneGap WebView
103         if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
104                 [[UIApplication sharedApplication] openURL:url];
105                 return NO;
106         }
107         else {
108                 return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
109         }
110 }
111
112
113
114 - (BOOL) execute:(InvokedUrlCommand*)command
115 {
116         return [ super execute:command];
117 }
118
119 - (void)dealloc
120 {
121         [ super dealloc ];
122 }
123
124
125 + (NSString*) startPage 
126
127         return @"WolneLektury.html"; 
128 }
129
130 @end