From firstViewController I send a url to load on secondViewController. The url loads correctly. They, I press cancel button to go back to firstViewController:
[self dismissViewControllerAnimated:YES completion:nil];
Then again if I come back to secondViewController from firstViewController, I see a blank space with horizontal scrollbar.
I've used the following code to load url:
NSString *urlAddress = #"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:requestObj];
I've implemented webview delegate methods and they are being called without any problem. for example following methods are called:
– webViewDidStartLoad:
– webViewDidFinishLoad:
Even there is no error to load and I've printed html source codes in -webViewDidFinishLoad and it Logged right codes.
My device iOS version is: iOS 9.3.2
Update:
The same app works well on my simulator. But doesn't work on real device.
I have been using AMSlidemenu with multiple AMSlideMenuContentSegues.
They all work fine at the moment as they just display a label.
The first view controller I have added a web view and given it its own custom class .h and .m file which point to a url but every time I launch the application crashes.
.....
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[websiteWebview loadRequest:requestURL]; **Thread 1: Breakpoint 1.1**
[super viewDidLoad];
// Do any additional setup after loading the view.
}
......
I dont understand whats happening ... I m a novice when it comes to Xcode so any help will be appreciated.
I hope I have contextualized my problem.
I have an app where on the very last UIViewController there is a UIWebView... the user navigates through the app and at the very end they can transition (modal) to the final UIViewController with a UIWebView... in it's viewDidLoad I have the UIWebView load the page.
The problem is it takes about 8 seconds to load this page and that annoys users.
How can I load the UIWebView way ahead of time (like when the app is first launched!) if that UIViewController hasn't even been presented yet?
I had your same problem for UIWebView inside a UIPageViewController and I found a better solution.
If you are in FirstViewController and you have your webView in your SecondViewController put the loadHTMLString or loadRequest method in the viewDidLoad of your SecondViewController and call [secondViewController.view layoutSubviews] for start loading in your FirstViewController.
Ex:
FirstViewController
-(void)viewDidLoad{
[super viewDidLoad];
// _secondViewController -> An instance of SecondVieController
[_secondViewController.view layoutSubviews];
}
SecondViewController
-(void)viewDidLoad{
[super viewDidLoad];
NSURL *url =[NSURL URLWithString:#"http://www.google.it"];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
What u can do is, load the html string
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:self.path ofType:#"htm"];
NSError * error;
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];
and when u display the webView load this content in your webView
[self.webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];
then your webview need only render your content and doesn't load the data
/// In Swift 2.0
do{
let path = NSBundle.mainBundle().pathForResource("theName", ofType: "html")!
let html = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
webView.loadHTMLString(html, baseURL: nil)
}catch{
print("Error is happend")
}
Note: It works the same way for WKWebView
Not sure how much work you want to do for this but I think you could cache/store the data from the URL in something like a NSDictionary, or something similar, using [NSUserDefaults standardUserDefaults] and use that information before actually loading the page.
So, load the first time you use it, after it has been seen then store the data using NSUserDefaults. Next time you go to this page, load from the NSUserDefaults first and call the URL/API in the back ground then update NSUserDefaults with this new data.
If you are requesting or pulling specific information each time, then I don't think this will work. If that is your case, you can probably try loading the data/website in an earlier view controller (probably one that takes the user a while to navigate to) and then send that data to the webview. Something like delegate/protocol or maybe even NSNotificationCenter. Hope this helps.
UIWebView is very tricky object. Simple answer would be: no you can't.
UIWebView won't load a document or an URL if it is not in views hierarchy.
Don't try doing it in different thread/queue either.
What you can do is to add UIWebView to the views hierarchy at the very beginning (as you've mentioned) and then pass it to the last view controller with preloaded data. This may work, but it's not an elegant way.
Side questions is: why does it take 8 secs to load a page? Maybe you can download the content of this page earlier? Is it static or dynamic?
I have a UIWebView that loads a PDF document, like this:
NSURL *myUrl = [NSURL URLWithString:#"http://www.site.com/myPDF.pdf"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
// Clear Cache, to insure prompt update of PDF
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:myRequest];
[myWebView loadRequest:myRequest];
When my internet connection is off, i get nothing. Just a blank UIView. Is there any way i can detect when there is not internet connection, or check if the URLContents is empty? I am trying to call a method -(void)showErrorText to show text in the center screen. How would i do this? (I got the method complete, just not the internet check). THANKS.
You can try to set the delegate of the webView to self and implements the webView:didFailLoadWithError and the others delagate methods tout have more details.
I'm trying to load documents into my app for display and this works if they're stored locally but not from the server. I'm testing with MAMP. Here's my viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *myUrl = [NSURL URLWithString:#"http://localhost:8888/FilesForApp/Docs/MS.doc"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[_webView loadRequest:myRequest];
}
Can anyone point tell me if there's something missing from this?
-EDIT-
Forgot to add, it doesn't crash, just shows a plain, white display.
Make sure you can access that doc through your web browser(make sure the doc exists).
Try to add the http:// prefix.
Don't forget to add _webView as subView to your view.
Don't forget to set the _webView's frame.