I am facing an issue while loading a UIWebView.I have a webview(1st) where a url is loaded.when that page is loaded,I am creating a small view which has webview (2nd) on top of the first webview,which is loading a second url.Basically I am loading a webview inside a webview.On click of a url in the second webview,the resulting url has to open in safari,outside the app.
IF i am not setting [webview setDelegate:self],then the page is loaded in the small webview as intended but on click,nothing is happening..
However if i set delegae of second UIWebView as self,the url which needs to be loaded in second UIWebView is getting loaded in first url.
Has anyone done this kind of webview loading earlier.
Please help.your help is deeply appreciated
Thanks
In your current class, you should test wich UIWebView delegate is dealing with
First set:
webView.delegate = self;
then in shouldStartLoadWithRequest:
//UIWebView *smallWebView;
//UIWebView *mainWebView;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(webView == smallWebView
{
//Load stuff on safari
}
else
{
//This means that link was pressed in the mainWebView
//Depending on your logic, you will load the smallWebView
}
}
Related
Is there a way to have two webview synchronized.
I would like to display on a second screen using Airplay the same WebView but in 16:9.
I managed to create a separate window for the second screen (which is 16:9) and load my local html file but the actions on my iPad screen are not synced with this second screen.
I just have two independent windows.
I really want to use the second screen to display a larger HTML file.
"Synchronized" is underspecified in the OP, but given the most essential representation of a web view's state is it's request, you could try the following: In the controller of the webView that the user is controlling (call it "webViewA"), declare conformance to UIWebViewDelegate:
self.webViewA.delegate = self;
then:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
UIWebView *webViewB = // presuming you have a way to get this pointer
[webViewB loadRequest:request];
}
You can achieve scrolling synchronization (probably the next most essential part of state) by declaring the same controller as conforming to UIScrollViewDelegate, and then:
self.webViewA.scrollView.delegate = self;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIWebView *webViewB = // ...
[webViewB setContentOffset:scrollView.contentOffset animated:YES];
}
I have a webview that can be cycled through different URLs. When I switch from one to the other I want the old web page to disappear before loading the next one. How can I do this without re allocating the webview?
If I try to [self.webView loadHTMLString:#"" baseURL:nil]; and load my URL in the same function the previous web page still remains:
[self.webView loadHTMLString:#"" baseURL:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.pageURL]];
EDIT: this apparently isn't clear enough for you. the below code doesn't clear the webview, it displays the previous page until the new one is loaded:
- (void) startLoadOfNextURL:(NSURL*)url
{
// clear:
[self.webView loadHTMLString:#"" baseURL:nil]; //DOESNT WORK
// Load real next URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
You can write the code below while your controller dismisses.
webView.load(URLRequest(url: URL(string:"about:blank")!))
You can make it load a blank page
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
Use JavaScript instead.
webView.evaluateJavaScript("document.body.remove()")
The following code clears the screen and then navigates
Swift
webView.evaluateJavaScript("document.documentElement.remove()") { (_, _) in
self.webView.load(urlRequest)
}
To clear old contents of webview
When you call - loadHTMLString:baseURL: it doesn't block until the load is complete. Once it records your request, it returns and loads in the background.
As a result, you would need to wait for the first load to finish before kicking off a new load request.
With UIWebView you would use UIWebViewDelegate's
- webViewDidFinishLoad:.
With WKWebView you would use WKNavigationDelegate's
- webView:didFinishNavigation:
Another approach if you really wanted to clear the contents without a delegate method would be to use JavaScript (eg https://stackoverflow.com/a/4241420/3352624). Then for UIWebView you could invoke - stringByEvaluatingJavaScriptFromString:. That method will block execution until the JavaScript executes and returns.
For WKWebView, you would need to do something like https://stackoverflow.com/a/30894786/3352624 since its - evaluateJavaScript:completionHandler: doesn't block.
To make old contents "disappear"
If you really just want to make "the old web page to disappear", you could cover the content area of the webview with a blank UIView temporarily. You could hide the contents when you initiate the load and then show the contents using the delegate methods above after the load completes.
Swift
webView.load(URLRequest(url: URL(string: "about:blank")!))
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
//Load request or write code here
})
I'd like to open new web pages in new UIWebViews. For example, when you go to Google that would count as one web page, but when you click on Google Images within Google it would count as a new web page.
All new web pages will be loaded in new UIWebViews and will be put in a History array that contains all the UIWebViews.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeBackForward) {
// The UIWebView already exists for that page in the history array, call it and display it.
}
else if (navigationType == UIWebViewNavigationTypeFormResubmitted) {
// No problem, load it in the current UIWebView.
}
else if (navigationType == UIWebViewNavigationTypeFormSubmitted) {
// It's a new page, load it in a new UIWebView.
}
else if (navigationType == UIWebViewNavigationTypeLinkClicked) {
// It's a new page, load it in a new UIWebView
}
else if (navigationType == UIWebViewNavigationTypeReload) {
// Just reload the current UIWebView.
}
else if (navigationType == UIWebViewNavigationTypeOther) {
// There's no way this is a new page. Do nothing.
}
}
As you probably have guessed, it didn't work. All you have to do is follow this path:
Go to http://www.9gag.com
Click the share on twitter icon under any post.
The link to share on twitter will look something like this:
https://twitter.com/intent/tweet?original_referer=http%3A%2F%2F9gag.com%2Fgag%2FaRQpqj2%3Fref%3Dt.mw&source=tweetbutton&text=I%20don%27t%20throw%20the%20word%20%27genius%27%20around%20loosely%2C%20but...&url=http%3A%2F%2F9gag.com%2Fgag%2FaRQpqj2%3Fref%3Dt.mw&via=9GAG
Although, someone would expect this link to be of type LinkClicked because you clicked the twitter icon to go there, it's actually of type Other, any desktop web browser will consider this link to be a pop up and would have opened it in a new window. Safari on iOS opens this link in a new tab. I would like to open it in a new UIWebView but the problem is that there's nothing differentiating an ad request from a pop up request in the UIWebView's delegate.
How is Safari doing it?
EDIT: The answer bellow is exactly the problem with UIWebView. It fools everyone into thinking that the delegate is the answer, when in fact you can do nothing without hacking your way through, even the simplest stuff.
EDIT 2: I have checked with other browsers as well like Mercury and they get this right. They understand that Twitter is a pop up and they open it in a new tab. So, this is not Apple's fault.
Set the delegate of your first web view to your view controller.
After that, add the following code to your view controller.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"%#", request.URL);
NSLog(#"%#", [request.URL scheme]);
if ([[request.URL scheme] isEqualToString:#"http"])
{
// Create web view here, because you're catching all requests
// Don't forget to set the delegate.
NSLog(#"Create new web view with %#", request.URL);
return NO;
}
return YES;
}
As you can see, you should replace the part where I added the NSLog. You're probably going to want to create a new web view at that point, and add it to the current view as a subview.
Why would you want to check on LinkClicked and not catch all requests?
I'm facing a problem trying to take screenshots form UIWebViews. I need to take some screenshots of my UIWebView and it works but the screenshot is not correct because they are taken in the event webViewDidFinishLoad, but it calls webViewDidFinishLoad when the UIWebView is not loaded fully, I mean, I take the screenshot in the event webViewDidFinishLoad but the UIWebView is not correctly and fully loaded so it takes a screenshot and it makes right but the screenshot is not totally correct because its the event is triggered (webViewDidFinishLoad) but the UIWebView is not totally loaded. Any ideas?
Thank you very much
A user was having a issue with their use of a activity indicator that apears until a page has loaded but theirs would come back after the main content had loaded the question is here UIWebView not finishing loading? and the answer code is used below. After the code I will go into detail about how this could be used for your situation.
(In the code below the web page truly starts loading on //show UIActivityIndicator and truly finishes loading the main content (not the extra content you are struggling with) on //hide UIActivityIndicator)
//Define the NSStrings "lastURL" & "currentURL" in the .h file.
//Define the int "falsepositive" in the .h file. (You could use booleans if you want)
//Define your UIWebView's delegate (either in the xib file or in your code)
- (void)webViewDidFinishLoad:(UIWebView *)webView {
lastURL = [[NSString alloc] initWithFormat:#"%#", webView.request.mainDocumentURL];
if (falsepositive != 1) {
NSLog(#"Loaded");
//hide UIActivityIndicator
} else {
NSLog(#"Extra content junk (i.e. advertisements) that the page loaded with javascript has finished loading");
//This method may be a good way to prevent ads from loading hehe, but we won't do that
}
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *requestURL =[request mainDocumentURL];
currentURL = [[NSString alloc] initWithFormat:#"%#", requestURL]; //not sure if "%#" should be used for an NSURL but it worked...
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
if ([currentURL isEqualToString:lastURL]) {
falsepositive = 1;
NSLog(#"The page is loading extra content with javascript or something, ignore this");
} else {
falsepositive = 0;
NSLog(#"Loading");
//show UIActiviyIndicator
}
}
When a page loads in iOS, webviewdidstart and webviewdidfinish are called but not knowing the difference in your main page/html content objective-c calls this again for extra content such as ads or frames. Using this if I were in your situation I would create a BOOL such as pageIsLoading and set it to true in webviewdidstart and then set it to false in webviewdidfinish. After the BOOL is turned off in webviewdidfinish I would end webviewdidfinish by calling a method that will check after a short delay if the BOOL pageIsLoading == YES and if it is, do nothing because more content is loading. If pageIsloading == no then all content must be loaded and now would be a good time to take your snapshot.
Rather than taking screenshot in the event webViewDidFinishLoad you can try to take the screenshot explicitly using UIButton. Let this button be disabled and you can just set this button's enabled property to YES after web view fully loads its content.
On button click to can implement your code to take screenshot.
Edited : Webview's webViewDidFinishLoad method is called when it finishes loading its content. Might be its possible that it may be taking some time to render some images/content on its view.
If possible you can use NSTimer in your webViewDidFinishLoad method to wait for sufficient time(1 min) so that mean while webview can load its content fully.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60.0
target: self
selector: #selector(MethodName:)
userInfo: nil
repeats: NO];
In Selector method you can implement your code of taking screenshot.
Hope this will help you...
My goal is to allow UIWebView load initial link, but disallow any further navigation.
I saw question:
Disable hyperlinks in UIWebView
So, I wired referenced webView property from interface builder.
I specified that my UIViewController uses UIWebViewDelegate:
#interface LegalViewController : UIViewController<UIWebViewDelegate>
In the code, on viewDidLoad, I do following:
mFirstLoad = TRUE;
webView.delegate = self;
And I have following code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (!mFirstLoad)
return FALSE;
mFirstLoad = FALSE;
return TRUE;
}
shouldStartLoadWithRequest is called both initial call and when I click on the link. However, even if shouldStartLoadWithRequest returns FALSE, UIWebView still proceeds and loads a new page.
I see this problem on iOS Simulator 6.0
How can I fix this behavior?
return NO based on type to exclude links
return !(navigationTpe==UIWebViewNavigationTypeLinkClicked);
Is it possible your boolean is getting reset somehow? What happens if you just return NO for (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType?
Here is the same thing which I wrote in comments:
I think I found the reason for the problem. I am not sure how to properly call it (not a big expert on HTML and Javascript). However the link has following code : "<a href="/something" data-transition="flip"</a> As result, callback is called, but it looks like results of this callback is ignored.
As result most likely solution is here: Remove hyperlinks in uiwebview