Resurrected 128-pixel icon for About page
[zxing.git] / iphone / Classes / MessageViewController.m
1 //
2 //  MessageViewController.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 30/07/2008.
6 /*
7  * Copyright 2008 ZXing authors
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #import "MessageViewController.h"
23
24
25 @implementation MessageViewController
26
27 @synthesize callbackTarget;
28 @synthesize callbackSelectorSuccess;
29 @synthesize callbackSelectorFailure;
30 @synthesize contentURL;
31
32 - (UIWebView *)webView {
33   return (UIWebView *)self.view;
34 }
35
36 - (id)initWithMessageFilename:(NSString *)filename 
37                        target:(id)cbt
38                     onSuccess:(SEL)ss 
39                     onFailure:(SEL)fs  {
40         if (self = [super initWithNibName:@"Message" bundle:nil]) {
41     self.callbackTarget = cbt;
42     self.callbackSelectorSuccess = ss;
43     self.callbackSelectorFailure = fs;
44     self.contentURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:filename 
45                                                                              ofType:@"html"]];
46         }
47         return self;
48 }
49
50 - (void)loadView {
51   [super loadView];
52   self.webView.delegate = self;
53   [self.webView loadRequest:[NSURLRequest requestWithURL:self.contentURL]];
54 }
55
56 - (void)viewDidLoad {
57   [super viewDidLoad];
58 }
59
60
61 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
62         // Return YES for supported orientations
63         return (interfaceOrientation == UIInterfaceOrientationPortrait);
64 }
65
66 - (void)didReceiveMemoryWarning {
67         [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
68         // Release anything that's not essential, such as cached data
69 }
70
71
72 - (void)dealloc {
73         [super dealloc];
74 }
75
76 // open a URL, asynchronously
77 - (void) openURL:(NSURL *)url {
78   [url autorelease];
79   [[UIApplication sharedApplication] openURL:url];
80 }
81
82
83 // UIWebViewDelegate methods
84
85 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
86   if ([[request URL] isFileURL]) {
87     // only load 'file' URL requests ourselves
88     return true;
89   } else {
90     // any other url:s are handed off to the system
91     NSURL *url = [[request URL] retain];
92     [self performSelectorOnMainThread:@selector(openURL:) withObject:url waitUntilDone:false];
93     return false;
94   }
95 }
96
97 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
98   NSLog(@"failed to load content, performing failure callback");
99   [self.callbackTarget performSelector:self.callbackSelectorFailure withObject:self afterDelay:0.0];
100 }
101
102 - (void)webViewDidFinishLoad:(UIWebView *)webView {
103   NSLog(@"finished loading content, performing success callback");
104   [self.callbackTarget performSelector:self.callbackSelectorSuccess withObject:self afterDelay:0.0];
105 }
106
107
108 @end