Note that there are some explanatory texts on larger screens.

plurals
  1. POUIWebView goBack to particular page
    text
    copied!<p>I'm facing a problem with UIWebView's goBack function.</p> <p>My requirement is when the user presses a button (called "Back Button"), I have to redirect the user to the particular page in certain conditions. Think of it as a "cart system": When the user is at "Menu" page, he selects an item and and "add an item to cart". When he clicks "remove an item", then go to "The Item was removed" page. When the user clicks the "Back Button", the user should go back to the "Menu" instead of empty cart page.</p> <p>To solve this, I thought about using "Queue" to save the URL history, then the user presses the "Back Button" in certain conditions, reload the URL from the "Queue".</p> <p>I made custom NSMutableArray with enqueue/dequeue functions thanks to <a href="https://stackoverflow.com/questions/817469/how-do-i-make-and-use-a-queue-in-objective-c">this answer</a>. Then using it to store the URL history:</p> <pre><code>- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { _currentPage = [[request URL] absoluteString]; NSString *targetURL = [self getTargetURLName:[request URL]]; if ([webViewHistory count] &gt; 5) { [webViewHistory dequeue]; [webViewHistory enqueue:_currentPage]; } else { [webViewHistory enqueue:_currentPage]; } if ([targetURL isEqualToString:@"delete_cart"]) { delBackPage = [webViewHistory objectAtIndex:2]; NSLog(@"URL BEFORE DELETE: %@", delBackPage); } } </code></pre> <p>Then call the "reload" when the "Back Button" was pressed in certain conditions: </p> <pre><code>- (void) backWebView:(id)sender { currentURL = [[_webView request] URL]; NSString *currentTGTName = [self getTargetURLName:currentURL]; if ([_webView canGoBack]) { if ([currentURL isEqual:[[_webView request] URL]]) { if ([currentTGTName isEqualToString:@"my_cart"] &amp;&amp; delBackPage != nil) { NSLog(@"GO BACK FROM MY_CART: %@", delBackPage); _requestURL = delBackPage; [_webView reload]; } else { [_webView goBack]; NSLog(@"WEBVIEW CANGOBACK----"); } } ... </code></pre> <p>Now I can successfully get "delBackPage" as "Menu" page's URL. But when I reload the webView with that URL, it won't work. </p> <p>Am I missing something? Is there any way to achieve this? If there is better way to control WebView's URL, it would be very appreciated.</p> <p><strong>EDIT</strong> </p> <p>Thanks to the tips from Chris, we finally managed controlling "Back Button" like the following: </p> <pre><code>- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { url = [[request URL] absoluteString]; // Store the conditions if ([url rangeOfString:[NSString stringWithFormat:@"%@orders/my_cart", SITE_URL]].location != NSNotFound || [url rangeOfString:[NSString stringWithFormat:@"%@orders/add_cart", SITE_URL]].location != NSNotFound) { if ([currentURLStr rangeOfString:[NSString stringWithFormat:@"%@orders/top", SITE_URL]].location != NSNotFound || ...) { ref = currentURLStr; } } currentURLStr = url; return YES; } </code></pre> <p>And "Back Button"...</p> <pre><code>- (void) backWebView:(id)sender { if ([currentURLStr rangeOfString:[NSString stringWithFormat:@"%@orders/top", SITE_URL]].location != NSNotFound || [currentURLStr rangeOfString:[NSString stringWithFormat:@"%@orders/error", SITE_URL]].location != NSNotFound) { [[self navigationController] popViewControllerAnimated:YES]; } else if ([currentURLStr rangeOfString:[NSString stringWithFormat:@"%@orders/genre", SITE_URL]].location != NSNotFound || ....) { [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@orders/top/%@/%@", SITE_URL, guid, authKey]]]]; // Refer the stored conditions... } else if ([currentURLStr rangeOfString:[NSString stringWithFormat:@"%@orders/my_cart", SITE_URL]].location != NSNotFound) { if ([ref rangeOfString:[NSString stringWithFormat:@"%@orders", SITE_URL]].location != NSNotFound) { [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:ref]]]; ref = @""; } else { [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@orders/top/%@/%@", SITE_URL, guid, authKey]]]]; } } else { if (![_webView canGoBack]) { [[self navigationController] popViewControllerAnimated:YES]; } else { [_webView goBack]; } } } </code></pre> <p>I hope this approach helps all facing the similar problems...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload