iOS Open WebView href in new WebView - ios

I am building an iOS application that uses the SWRevealViewController to display slide-out menus.
In my slide out menu, I have a WebView which contains some links. When a user clicks the link, I want the SWRevealViewController to slide back (I can do that easy enough), and the URL opens in the MainViewController WebView (not so easy for me).
I want to make sure, if possible, that the URL doesn't change at all in the SWRevealViewController. Just starts the action to open the URL in MainViewController.
How can I achieve this?

Add UIWebViewDelegate as an protocol your view controller implements.
Set yourself as the delegate to the UIWebView
Add the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType to your view controller.
Return NO from this method (unless it is for the initial page load) and capture the URL to navigate to in another view controller.
In your header add the UIWebViewDelegate protocol
#interface MyViewController : UIViewController <UIWebViewDelegate>
In your viewDidLoad set the delegate (Assuming you have an #property for the webView with an IBOutlet set to the UIWebView)
self.webView.delegate = self;
Then add the delegate method for loading requests.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString* urlStr = [[request URL] absoluteString];
if(request == nil || [urlStr isEqualToString:#"http://YourInitialURL"])
{
return YES;
}
//Use this url to change the other web view in your main view controller
NSURL* url = [request URL];
return NO;
}

Related

creating some action in webview and showing the result in different part of view in iOS

I have a split view where my master is a table view menu and my detail is UIWebview content view.
Whenever I click on my table cell respective urls are opening in content view , showing the table cell selected.
In one of the content view which is a web page, when I click on a button over there I want to show one of the table view cell selected in my menu view .
If I use touch events it will happen for all the view ,I want to do this action for that particular button. Any idea how to get this done.
As I understand you correctly, you have uiwebview inside detail view controller. UIWebView has html buttons, which redirect to some urls. The aim is notify your master controller about such redirections. In that case, I advice you to use one of the delegate methods of UIWebViewDelegate, which is
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Inside that method your should notify your master controller. You could use your custom protocol (look at that tutorial) or use NSNotificationCenter to send notification. To select row you could use method of table view:
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
If Button is on the webPage and its opening another webPage. then you can manage your code with using delegate methods of webview
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
// Here you can check the URL
NSURL *url = [request URL];
if ([url.absoluteString isEqualToString:#"your url"]) {
// Do something
return NO;
}
return YES;
}

Loading links from with uiwebview into customized webview

I am working with UIWebView, in the detailed view of my app, I open a url into a webview and embed the webview into my detailed view(which is a uiview). Now the content of the embedded webview has web links, which when clicked open up into the same embedded webview, as should be expected.
The concern for me here is that I want to open the second level links(links clicked from within the embedded webview) into another customized webview component, not into the same embedded webview.
I tried implementing the following method of the UIWebViewDelegate, but I could not achieve the desired result.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
Implementation:
{
CustomWebView* iWebView = [[CustomWebView alloc] initWithFrame:CGRectMake(0, 0, webView.frame.size.width, webView.frame.size.height)]; //creating custom webview in same frame
[iWebView loadRequest:request];//loading the request into custom webview
[webView addSubview:iWebView]; // adding custom webview overlapping the webview
return NO; //[stop loading the url into the embedded webview]
}
You also need to set your Custom Web View delegate:
iWebView.delegate = self;
Then you should see that the request will first pass through the
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
delegate method.

WebView:How to detect if I am on the original web page and execute a certain method

I have this method that goes back to previous webpage in a web view under certain circumstance and it works fine. However, I want to implement a function that checks if I am on the original web page and if so, when I push the back button it takes me to a navigation view controller. The original page is this code [webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];. Now I need to figure out how to check if I am on that page and if so I need to execute this code: [self.navigationController popViewControllerAnimated:YES];. Any help will be appreciated.
- (void)back
{
if ([webView canGoBack]) {
[webView goBack];
} else {
[webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];
}
}
You can use the delegate method shouldStartLoadWithRequest of UIWebViewDelegate to check what contains in the url. if you find it then just go NavigationController
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[request.URL absoluteString] rangeOfString:#"http://yourURL"].location!=NSNotFound) {
[self.navigationController popViewControllerAnimated:YES];.
}
}
You need to add
<UIWebViewDelegate>
to the header. Then set webView.delegate to yes;
Once you have it set as a delegate, then you can get the absoluteString and the requestURL, and override the method. With the absoluteString you can check for a substring, text, domain, basically anything you want! You override shouldStartLoadWithRequest or webViewDidFinishLoad:
-(void)webViewDidFinishLoad:(UIWebView *)webView
-(void)shouldStartLoadWithRequest :(UIWebView *)webView
That calls everytime any webpage is loaded.

UIWebView New Url load animation effect

Can we load a new url in webview with animation when it loads effect ??
i.e. I want to give my webview an animation when it loads a new url or any link clicked on the opened website in webview.
I want to give pushviewController type animation (right to left movement of screen ) with back button on my webview to go back (left to right movement of webView view) when user taps on link / loads a new url.
Yes.
You can stack a view in - (void)webViewDidStartLoad:(UIWebView *)webView and close it with - (void)webViewDidFinishLoad:(UIWebView *)webView
e.g.,
WebViewController.h
#interface WebViewController : UIViewController<UIWebViewDelegate> {
IBOutlet UIWebView *webView;
}
WebViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self.navigationController pushViewController:someViewController animated:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.navigationController popViewControllerAnimated:YES];
}
When ever a new URL is loading into UIWebView,it's
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//Code for animating view .
//Code for back button.
}
delegate method will be called .
So do the stuff required for you inside this delegate method.

How to return last page of webview to another view from a view

I have a ios program for iPad. It is used 2 view, A view has webview, another has imageview. When I returned webview from imageview, webview is opened homepage. I want to return last page . How can I do?
If I understand you correctly, you want to make sure, that the last page that was loaded in the UIWebView will be displayed again when you switch back to the UIWebView.
To achieve that you can keep track of the last URL that was loaded in the UIWebView. You can do that in the following UIWebViewDelegate method (declare currentURL as property):
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
self.currentURL = request.URL;
return YES;
}
And then when you bring back your UIWebView:
- (void)showWebView {
[yourWebView loadRequest:[NSURLRequest requestWithURL:currentURL]];
}

Resources