Internet Connection Check UIWebView? - ios

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.

Related

WKWebView can not load url of flash message

WKWebView *wb = [WKWebView new];
[wb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://file8a445fb7811e.iamh5.cn/v3/idea/u5UfsotV?suid=ohAJ7wa8J4S-rj0SFOVmrDhOBotM&from=singlemessage"]]];
This urlStr is a flash URL. It stops after load 99%. How do I fix it?
I tried to use UIWebview, it worked perfectly, but WKWebView not.

Use a WebView to run JavaScript

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!
}

UIWebView doesn't loadRequest second time

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.

Load WebView as soon as app starts

I have a WebView in my app that works fine but very slow to load. Is there a way to actually start loading the web view as soon as the app starts?
Thanks
[EconCalWebView setDelegate:self];
// Do any additional setup after loading the view.
NSURL *myURL = [NSURL URLWithString:#"http://www.forexyard.com/en/calendar.iframe?zone_id=9493"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[EconCalWebView loadRequest:myRequest];
You can write your code for loading webview in:
applicationDidFinishLaunching method available in AppDelegate Class.
Here you require to load your viewController first and then load webView.
Please find more details at : UIApplicationDelegate

unable to display .doc from server

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.

Resources