Hello,
My app uses 5 arrays and a webview content.
I would like to reload these contents as soon as the app is called from the background.
I'm still using a refresh button, it reloads all the information i need.
But now i want to let the app reload all information automatically.
I suppose, that i have to set up the "applicationwillenterforeground" method, but i don't really know how to do it.
These are my classes:
Can anyone help me?
My app uses 5 arrays and a webview content.
I would like to reload these contents as soon as the app is called from the background.
I'm still using a refresh button, it reloads all the information i need.
But now i want to let the app reload all information automatically.
I suppose, that i have to set up the "applicationwillenterforeground" method, but i don't really know how to do it.
These are my classes:
Code:
testAppDelegate.h
#import
@class testViewController;
@interface testAppDelegate : NSObject {
UIWindow *window;
testViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet testViewController *viewController;
@end
Code:
testAppDelegate.m
#import "testAppDelegate.h"
#import "testViewController.h"
@implementation testAppDelegate
@synthesize window;
@synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
Code:
testViewController.h
#import
@interface testViewController : UIViewController {
IBOutlet UIWebView *webView;
IBOutlet UITextField *addressBar;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UILabel *myField2;
IBOutlet UILabel *myField3;
IBOutlet UILabel *myField4;
IBOutlet UILabel *myField5;
IBOutlet UILabel *myField6;
}
@property(nonatomic,retain) UIWebView *webView;
@property(nonatomic,retain) UITextField *addressBar;
@property(nonatomic,retain) UIActivityIndicatorView *activityIndicator;
-(IBAction) gotoAddress:(id)sender;
-(IBAction) goBack:(id)sender;
-(IBAction) goForward:(id)sender;
-(IBAction) goxxx:(id)sender;
-(IBAction) goxxxx:(id)sender;
-(IBAction) refresh:(id)sender;
@end
Code:
testViewController.m#import "testViewController.h"
@implementation testViewController
@synthesize webView, addressBar, activityIndicator;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = @"http://www.xxxx.de/iphone.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"hgb-iphone2-1.gif"]];
[webView loadRequest:requestObj];
[addressBar setText:urlAddress];
NSString *siteString = @"http://www.xxxx.de/test.txt";
NSURL *siteURL = [NSURL URLWithString:siteString];
myField2.text = [NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil];
NSArray *myArray = [[NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString: @":"];
[myField2 setText: [myArray objectAtIndex: 0]];
[myField3 setText: [myArray objectAtIndex: 1]];
[myField4 setText: [myArray objectAtIndex: 2]];
[myField5 setText: [myArray objectAtIndex: 3]];
[myField6 setText: [myArray objectAtIndex: 4]];
}
- (void)applicationWillEnterForeground:(UIApplication *)viewDidLoad {
}
-(IBAction)gotoAddress:(id) sender {
NSURL *url = [NSURL URLWithString:[addressBar text]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[addressBar resignFirstResponder];
}
-(IBAction)goxxx:(id) sender {
NSString *urlAddress = @"http://www.xxx.de/iphone.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[addressBar resignFirstResponder];
}
-(IBAction)goxxxx:(id) sender {
NSString *urlAddress = @"http://www.xxxx.de/iphone.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[addressBar resignFirstResponder];
}
-(IBAction)refresh:(id) sender {
NSString *urlAddress = @"http://www.xxxx.de/iphone.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"hgb-iphone2-1.gif"]];
[webView loadRequest:requestObj];
[addressBar setText:urlAddress];
NSString *siteString = @"http://www.xxxxx.de/test.txt";
NSURL *siteURL = [NSURL URLWithString:siteString];
myField2.text = [NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil];
NSArray *myArray = [[NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString: @":"];
[myField2 setText: [myArray objectAtIndex: 0]];
[myField3 setText: [myArray objectAtIndex: 1]];
[myField4 setText: [myArray objectAtIndex: 2]];
[myField5 setText: [myArray objectAtIndex: 3]];
[myField6 setText: [myArray objectAtIndex: 4]];
}
-(IBAction) goBack:(id)sender {
[webView goBack];
}
-(IBAction) goForward:(id)sender {
[webView goForward];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"http"]) {
[addressBar setText:[URL absoluteString]];
[self gotoAddress:nil];
}
return NO;
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
@end
Can anyone help me?