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.
Related
I want to load a URL on a UIViewController with an UIWebView and UIActivityIndicatorView, but UIActivityIndicator never appears and UIWebView never loads the URL.
This is my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = #"Web";
[self displayURL:[NSURL URLWithString:#"(Website)"]];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[self.loadView stopAnimating];
self.loadView.hidden = YES;
}
-(void) displayURL:(NSURL *) aURL {
self.web.delegate = self;
self.loadView.hidden = NO;
[self.loadView startAnimating];
[self.web loadRequest:[NSURLRequest requestWithURL:aURL]];
}
If you add your webView by code, you need add
[self.view addSubview: web];
after you created webView, and of course you need to add UIActivityIndicatorView as a subView of webView or superView, so you need to add [web addSubview: loadView] or [self.view insertSubview: loadView aboveSubview: web];.
And about the URL, if your website URL is like www.google.com, you need to change #"(Website)" to the real URL. If your website is a file copied in your project, you need to change [NSURL URLWithString: ]; to [NSURL fileURLWithPath:[NSBundle mainBundle][pathForResource: ofType:]];.
Really poorly formulated question.
UIWebViewController does not work
UIWebViewController does not exists, you are talking about a UIWebView in a UIViewController
Code is very incomplete
I'm trying to make it so that the back button will only be enabled when the user presses a link and/or if canGoBack = TRUE. I tried using an if statement in the body but that didn't work. I then tried using what I could find online and it still didn't work. My code is below. What am I doing wrong? Thanks!
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSString *url = webView.request.URL.absoluteString;
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
backButton.enabled = TRUE;
}
return TRUE;
}
shouldStartLoadWithRequest() won't get called if you haven't set the web view's delegate.
In order to use UIWebView its typically set as an outlet property of a view controller, so you should have something like this:
IBOutlet UIWebView* webView;
The webView has a delegate property which needs setting to your view controller, you can set delegates in a storyboard, but its more explicit to set it in code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
99% of the time you just set delegates, but UIWebView's delegate is funny in that it needs setting to nil when finished with. So add this:
- (void)viewDidUnload
{
[self.webView stopLoading];
self.webView.delegate = nil;
UIWebView and WKWebView both have a canGoBack and canGoForward property on them. You can use KVO to see when these are enabled, and enable/disable your back and forward buttons accordingly.
http://nshipster.com/key-value-observing/
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.
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.
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.