I am doing this in my app :
-
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.i
NSURL *url = [NSURL URLWithString:#"http://www.mySite.fr"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request];
self.myWebView.scalesPageToFit = YES;
}
When i have not the network ( no connexion) nothing is shown ( just a white page witch is the web view).
My question is should i put an alert to the user when there is no network ? how i ca do this ? thanks ? will my app rejected if i don't show an alert to the user ?
Thanks for your answers
While it makes sense to check for reachability, I think you have a better approach using the delegate method webView:didFailLoadWithError: which will tell you if the web view has failed to load your page. In such a case, rather than popping an alert view load some kind of local HTML page indicating that the load has failed if you wish and set a timer to trigger a reload after a while.
As such I don't think Apple will reject you for this unless it is the only thing the App does. But you will have to give a thought on the user experience when the load fails.
I do not think that not checking for a connection will cause your app to be rejected, but you should do it anyway.
Ed Marty has pointed out that my original suggestion of using
- (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error
will not work in iOS (thanks!).
This StackOverflow question appears to have a working solution to the problem.
Related
I'm reviewing someone else's code and noticed a web form gets refreshed every time you attach an image(click 'add image', find and choose image, return to form, form goes blank). And, it's because the url gets reloaded. In tracking this issue down I noticed that the original dev overrode the viewDidAppear instance method in WebViewController like so:
- (void) viewDidAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:self.defaultUrl];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Apple's documentation says
You can override this method to perform additional tasks associated with presenting the view. If you override this method, you must call super at some point in your implementation.
I see super isn't called and I think putting in an NSURLRequest is not good practice. I removed the code, added the URL call a button action and all's well so this is mostly a stylistic/academic question.
Do you agree that loadRequest shouldn't be in there? Thanks for your help.
Why shouldnt it be there? loadRequest does its work asynchronously on another thread, so it doesn't block the main thread.
Connects to a given URL by initiating an asynchronous client request.
If it behaves the intended way, is for you to decide.
(Oh and yeah, you should call super in viewDidAppear)
I am using UIWebView to display textual content in my app (I store the content in local HTML files that I pack with the app). All together, I have three web views whose content I change dynamically based on user feedback.
Although some might argue that this is not the most accepted way, I find UIWebView very convenient to display formatted text, and modify that text using HTML if necessary. While this works 99% of the time, on occasion, I experience problems that generally fall into one of these categories:
Sometimes, the web view content loads slow and is late a second or so.
The content loads but is not showing. However, as long as, I touch the view (try to scroll or something) the content pops in.
A few times I received memory warnings (usually not long after the app's initial loading) that in no way affected the performance of my app. It logged the memory warning but the app worked like nothing happened.
This is the method I use to load content to my web views:
- (void)loadSimpleDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
Aside from this, the shouldStartLoadWithRequest delegate method is also implemented, always returning a YES.
My question is: Is there a way to improve the reliability/performance of my web views (in particular loading)? Is there something I have overlooked, did wrong, or do not know about?
Some additional info:
I am on iOS6 SDK, use ARC, and do everything programmatically (do not use IB or storyboard).
You have 2 options to check what's going on:
Implement webViewDidStartLoad & webViewDidFinishLoad delegate methods to check why the content isn't showing (may be the content isn't loaded yet).
read the html content to an NSString then use loadHTMLString:baseURL instead of using loadRequest and check if it loads faster.
Hope this could help.
Some help please,
I am putting a web app together and I am stuck on a few things.
I have tabviewcontrollers which load different uiwebviews.
Each time I navigate on the app and re-click the tab it remains where I was on that page is there a way to re-load it so it always goes from the orginal ur (not just a refresh)?
Any advice on the best way to handle this would be appreciated.
Thank you
Steve
Load the request in your view controller's viewWillAppear: method.
When you want to refresh, use
[webView reload]
or for if you want t reload with a specific url
NSURL *theURL = [NSURL URLWithString:#"http://urltoreload.html"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
For legal reasons, I'm obligated to show Terms of Service in my application, a PDF on an external server. What I believe would be easiest to do would be to create a UIBarButton item and then create an IBAction that launches the link in Safari.
So I create a button:
IBOutlet UIBarButtonItem *legal;
Then I make it into a nonatomic property and synthesize it in my implementation file, right? I go on to create an IBAction:
-(IBAction)legalButtonPressed:(id)sender;
I go into my implementation file, and here's where the issue comes. When it comes to defining those actions, I become confused. As I am new to iOS development, I could use some guidance. I don't know how to force the link into safari in the action. Any help would be greatly appreciated!
Thanks!
IBOutlet and IBActions are used for Interface Builder connections. If you have your UIBarButtonItem on a xib file, you can connect its action to the controller by right clicking in the files owner object. The same with the outlet.
Once you have the action in the controller connected to the button in the xib file, (I see no need to get an outlet here), you just implement the method as follows:
-(IBAction)legalButtonPressed:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://yourdomain.com/legal.pdf"]];
}
this will automatically open safari with the given url
Take a look at UIApplication's documentation, it has a openURL method you might find useful.
Another option is to include your legal text in a UITextView.
Example:
By clicking Join you agree to company's
Terms of Service, found here: http://www.site.com/tos.html and
Privacy Policy, found here: http://www.site.com/privacy.html
Make sure Link Detection is turned on in the UITextView and it will automatically recognize URLs. Any clicks on those URLs will automatically launch Safari.
Although you can do this approach (i.e. launch the link in Safari). I would suggest that you try to keep the user as much as possible in your app. I am guessing this LEGAL TERMS is a HTML doc?
You can do that using UIWebView. Init a webView and do this -
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
This will open the url in your app only! You can make the UIWebView open up as a modal window or in many other ways...
HI, WHat am trying to do is load a google direction page directly in webview by passing the lat long values for source and destination (am getting these stored_long lat values that i have previously stored. and am not using google api) everything works fine but the prob is as soon as webview loads the direction page, it starts getting memory warning and on further browsing get crash need a solution to fix these ...am setting webview delegate to nil and also releasing webview also sometimes it crashes wen back btn is pressed ....do need urgent help...guys
NSString* url = [NSString stringWithFormat:#"http://maps.google.com/maps/m?saddr=%f,%f&daddr=%f,%f&view=map&z=13",Stored_lat3,Stored_long3,Stored_lat4,Stored_long4];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
aWebView.delegate = self;
[aWebView loadRequest:request];
Have you tried profiling your application for Allocations using Instruments?
Memory warnings tend to imply your application is using too much memory. It could be that before you even open your web view you are using lots of memory. Opening the webview could be pushing your application over the edge so to speak.
Overriding didReceiveMemoryWarning in your view controllers to clean up memory is also a good tactic to prevent your application from crashing.
I hope this helps!