0

I have a single view iOS app, that contains a webView that displays my website. When the app is returned to from multitasking (when it is brought back as the active app) I would like the webView to get the URL it is currently on, and refresh (or reload) that current page in the webView (I'm thinking using this method):

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"applicationDidBecomeActive");
}

How would I do this? Do I need to subscribe to some sort of notification? Please help point me in the right direction, and provide some code.

adamdehaven
  • 5,890
  • 10
  • 61
  • 84

1 Answers1

0

There are some point to get noticed are like:

  1. Application should allow user to run in background mode. You can define this in info.plist

  2. Implement UIWebViewDelegate in your view controller. There is one method: webViewDidStartLoad: Inside that method get the current url

    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"]; (For more details refer this.)

  3. Save this url string to some where, and while relaunching your application the method which you mentioned in the question gets called. Inside that method, load your web view with this url string.

Hope this is what you required.

Enjoy coding :)

Community
  • 1
  • 1
Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • How do you save the currentURL to somewhere so that it can be accessed in my AppDelegate.m file by the `applicationDidBecomeActive` method? – adamdehaven Jul 25 '12 at 16:22
  • Either save this in AppDelegate or go with NSUserDefault. You can easily get tutorial on net for saving data using this two ways. – Mrunal Jul 25 '12 at 16:38
  • I have `NSString *currentURL = webView.request.URL.absoluteString; NSLog(@"Current URL: %@", currentURL);` in my `webViewDidFinishLoad` to set the current URL to a string. Now how can I access this in `applicationDidBecomeActive` ? – adamdehaven Jul 25 '12 at 16:40
  • Try to store in app delegate class one of the variable. And use it again using [[UIApplication shareApplication] delegate]. I guess it should work not sure. If it is not working then use NSUserDefaults. You can find examples/tutorials for the same. – Mrunal Jul 25 '12 at 16:52
  • I figured out how to store the `NSSTring` using the `NSUserDefault`, but what code would I put in `applicationDidBecomeActive` to reload the webView? Also, is the "hides instance" error for webView an issue or ignore it? – adamdehaven Jul 25 '12 at 17:01
  • use loadURL method of uiwebview and pass [NSURL urlwithstring: yourFetchString from userdefaults.] – Mrunal Jul 25 '12 at 17:08