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.
Related
I embedded an UIWebView to my view.
A default UIImagePicker will be presented if any file field in this webview's page is tapped.
How can I get this default picker or set its delegate?
This is my Object-C code
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://jsfiddle.net/wowaqpcf/embedded/result/"]]];
[webView setDelegate:self];
[self.view addSubview:webView];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
return YES; // => set breakpoint at this line -> it doesn't stop when picker opens
}
#end
Or opening this page by safari on iPhone, you will understand what I mean.
<form>
<input type="file">
</form>
I tried catching webView:shouldStartLoadWithRequest:navigationType: but it seems that presenting this picker doesn't through delegate methods. (I thought that this picker might be presented by navigating to a special scheme)
Many thanks.
Edit: What you want to do is not possible with default picker. If you want to crop selected image you should provide code in Javascript in your web page.
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;
}
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;
}
When you use the Facebook App and go to the Terms and Conditions view, there is a WebView. When you choose an option in the WebView you'll go to a new view.
Now I wanna make a login system with PHP. When the user login is succeded he'll go to an webpage. How can I set for example: When the UIWebView is on www.myserver.com/ok that the new view in my app will show?
Set your UIWebView with delegate and check :-
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"Loading: %#", [request URL]); //if url matches www.myserver.com/ok open your view
return YES;
}
Hope it helps you..
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]];
}