I have a method that loads the content in my WebView:
-(void)loadContent:(NSURLRequest *)requestObj{
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
webView.delegate = self;
NSLog(#"LOAD WEBSITE!!!");
}
when I first call it like this:
[self loadContent:requestObj];
it works fine! But when I call it again for a new URL, it does not work.
I have readed that it will help to call the following before loading the new URL:
[webView stringByEvaluatingJavaScriptFromString:#"document.open();document.close()"];
But it doesn't change the result. Does anyone know how to handle this? Please be patient with me, I'm new in the iOS-world.
Ok i find out, my WebView is Null, but why? I call it from the ViewController and it has to be there. Does someone know how to handle this?
UPDATE: It is not Null any more cause i do this:
if(!webView)
{
webView = [[UIWebView alloc]init];
}
But it does not change the content when i do this: [webView loadRequest:requestObj];
try reloading the view like self reload.nameOfTheView
Is your webview created in a nib? If so, it may not be connected.
http://sweng.epfl.ch/tutorials/getting-started-with-ios-developement-xcode-and-objective-c
you can go through the above link and find a step by step procedure
http://www.raywenderlich.com/2712/using-properties-in-objective-c-tutorial
you can download a sample code from the above link.
Related
I want to use a simple WebView to fetch the content of a website, which includes JavaScript. I don't want to really show the WebView, I only want to get the source code of the site. The view where I will load the request from, is not in the window hierarchy. The method is getting called from another view.
Something like this:
MainView.m:
[[HelperView alloc]LoadWebview];
HelperView.m:
-(void)LoadWebview {
webView = [[UIWebView alloc]init];
[webView setDelegate:self];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com/"]]];
[self.view addSubview:webView];
}
Is it possible to use the delegate methods from this WebView in a view, which is not in the window hierarchy, or is there a better way to solve this problem?
If all you need is to get the source code then try this:
-(void)loadWebview{
//Create an NSURL object
NSURL *url = [NSURL URLWithString:#"http://www.yourWebsiteHere.com"];
//Create a string to hold the source code
NSString *sourceCode= [NSString stringWithContentsOfURL:url];
//Do what you want with the source code here!
}
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 m just trying to fix all the contents of html according to the device screen.
I have to put html data at proper place for iPhone & iPad in webview.
Please help me!!!
I am going to assume that this question is a very basic one.
Suppose that you have placed a UIWebView in a view in your storyboard, and you have set layout constraints so that the UIWebView fills the view.
Then
Add an IBOutlet to your view controller, and connect it to the web view.
In the view controllers viewDidLoad: method, tell the web view what to display. Here is an example:
-(void)viewDidLoad {
[super viewDidLoad];
NSURL* url = [NSURL URLWithString:#"http://www.apple.com"];
NSURLRequest* r = [NSURLRequest requestWithURL:url];
[_webView loadRequest:r];
}
This loadRequest: call is asynchronous, meaning that the page may not show up immediately.
If this is not answering your question, please add more information.
If I have a UIWebView with mediaPlaybackRequiresUserAction = YES, then later in my app create a new UIWebView and set mediaPlaybackRequiresUserAction = NO on it, it also changes the value of that property on the first instance.
e.g. I have a UIWebView and then present a second UIWebView modally (for an ad), changing mediaPlaybackRequiresUserAction on the modal webView affects the presenting UIWebView.
Any ideas why this is? Are UIWebViews all backed by a single instance?
Link to sample project here.
not sure your app purpose, just try this way:
- (IBAction)unwind:(UIStoryboardSegue *)unwindSegue
{
[self TS_updateLabel];
[[self webView] setMediaPlaybackRequiresUserAction:YES];
[self TS_reloadWebView];
}
....
in method TS_reloadWebView
if (self.webView.isLoading) {
[self.webView stopLoading];
}
[self.webView loadHTMLString:htmlString baseURL:baseURL];
I guess it also is UIWebView bug .... but now this way maybe can solve your problem.
i've some javascript code on the webpage in the uiwebview that i want to use to call one of my objective c methods.
i found some code online which i decided to use. but it still doesn't seem to be working. can anyone see where the problem is?
javascript code:
function someMethod() {
window.location = "ios:webToNativeCall";
}
objective c code:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:#"ios:"]) {
// Call the given selector
[self performSelector:#selector(webToNativeCall)];
// Cancel the location change
return NO;
}
return YES;
}
-(void)webToNativeCall
{
//my code here
}
i'm not sure, how to use this method so it might be that i have implemented it incorrectly.
does anyone have any ideas about what could be causing this?
Thanks in advanced.
This code looks ok, please check whether delegate for UIWebView is set or not.
Otherwise you can use EasyJSWebView download it from Github, it is easy to use.
You must have missed to link the delegate.
Either connect the delegate of the webView to the file owner in the .xib file
or
Use Following code
webView = [[UIWebView alloc] init];
webView.delegate = self;
in your viewDidLoad also write below code
[webView stringByEvaluatingJavaScriptFromString:#"yourJavascriptFunction()"];
Hope it helps you...