How can I use progressView during URL is loading? - ios

I want the progressView working while my URL is loading. What should I do for that?
here's my .m code:
-(void)openURL{
UIWebView *webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 320, 460)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
[[self view] addSubview:webView];
}
- (IBAction)goto:(id)sender {
[self openURL];
}

Implement UIWebViewDelegate, specifically:
-(void)openURL{
UIWebView *webView = [[UIWebView alloc] init];
webView.delegate = self;
// ... rest of method as above ...
}
- webViewDidStartLoad:(UIWebView *)webView {
// Start progress.
}
- webViewDidFinishLoad:(UIWebView *)webView {
// Stop progress.
}

You could do something like this
[progressView startAnimating];
dispatch_async(dispatch_get_main_queue(), ^{
[webView loadRequest:yourRequest];
[progressView stopAnimating];
[progressView removeFromSuperView];
}
This will start the progress view before the operation begins, and will stop and remove it when it finishes.

Related

WKWebView reloads and then goes blank for a local index.html file

I have this file that I'm trying to reload but when I reload it I get the screen that is supposed to show up and then a blank screen until I hit reload again. I'm not sure why I'm getting this blank screen. Also, I do not get anything from
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {}
nor
-(void)didReceiveMemoryWarning
Here's my viewController.m file I have the index.html file in the same directory as it does load once but I'm not sure if webkit is crashing or where
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
#interface ViewController ()
#end
#implementation ViewController
NSString *localURL;
NSURLRequest *urlRequest;
- (void)viewDidLoad {
[super viewDidLoad];
JSContext *ctx = [[JSContext alloc] init];
ctx[#"console"][#"log"] = ^(NSString *message) {
NSLog(#"Javascript log: %#", message);
};
// Do any additional setup after loading the view, typically from a nib.
// [UIView setAnimationsEnabled:NO];
//_webView = [[WKWebView alloc] initWithFrame:self.view.frame];
// _webView.navigationDelegate = self.webView ;
// _webView.UIDelegate = self;
//[self.view addSubview:_webView ];
_webView= [[WKWebView alloc] initWithFrame:CGRectMake(0, 100, 500, 500)];
localURL = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:localURL]];
[_webView loadRequest:urlRequest];
[self.view addSubview:_webView];
//button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(reload) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Reload" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 20.0, 100.0, 50.0);
button.backgroundColor = UIColor.blackColor;
[self.view addSubview:button];
}
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
// Reload current page, since we have crashed the WebContent process
// (most likely due to memory pressure)
NSLog(#"most likely due to memory pressure reloaded");
[webView reload];
}
-(void)reload{
NSLog(#"reloading now");
[self.webView loadRequest:urlRequest];
//[self.webView stopLoading];
// [self loadView];
//[_webView loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
NSLog(#"memory issue");
}
//- (IBAction)rButton:(UIButton *)sender {
// NSLog(#"Well we didnt crash");
// [self.webView reload];
//}
#end
If the problem is due to a termination of the web the delegate function webViewWebContentProcessDidTerminate will be called.
In your case it looks that you didn't set the viewController as the navigationDelegate of the webview:
webview.navigationDelegate = self

Activity Indicator will not load after UIWebView Has Loaded

I am trying to get the activity indicator to stop animating once the view has loaded. Currently, the view has loaded but the activity indicator does not stop circulating. Here is my code:
UIWebView *elionWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 375, 579)];
[self.view addSubview:elionWebView];
elionWebView.delegate = self;
//Start the Activity Indicator
[self webViewDidStartLoad:elionWebView];
//Load Website
NSString* url = #"https://google.com/";
NSURL* nsUrl = [NSURL URLWithString:url];
NSURLRequest* request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[self webViewDidFinishLoad:elionWebView];
[elionWebView loadRequest:request];
}
-(void)webViewDidStartLoad:(UIWebView *)webView {
//Start the Activity Indicator
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center = CGPointMake(187.5, 370);
[activityView startAnimating];
activityView.tag = 100;
[self.view addSubview:activityView];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self.view viewWithTag:100].hidden = YES;
There are a few problems with your code. Firstly, NSURLRequest does not actually load the URL. There are different ways to do the download. Look at NSURLConnection for the simplest way to do a synchronous download.
Secondly, I suggest you declare your UIActivityIndicatorView as a member variable of your class rather than tagging the view.
There are UIActivityIndicatorView methods for starting and stopping animations. You should use stopAnimating to stop the animation. If you have set the property hidesWhenStopped to YES on the view, then you will get the behavior you want. If you do not want the indicator after animation has stopped, use removeFromSuperview to get rid of it.

UIWebView content how to resize/scale it?

I came to a point where I was able to lock the horizontal scroll as I wanted however when I rotate from landscape to portrait the content is still to large. I need to resize/scale it down myself but I don't know how to so far I use this :
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://igo.nl";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
_webView.scrollView.bounces = false;
_webView.scalesPageToFit = YES;
_webView.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait)
{
//_webView.scrollView.contentSize = CGSizeMake(320, _webView.scrollView.contentSize.height);
}
else
{
//[_webView reload];
_webView.bounds = CGRectMake(0, 0, 320,_webView.bounds.size.height);
_webView.frame = CGRectMake(0, 0, 320, _webView.frame.size.height);
_webView.scrollView.contentSize = CGSizeMake(320, _webView.scrollView.contentSize.height);
}
}
EDIT:
What I seem to need is that the content is zoomed out
Use the delegate methods of UIWebViewDelegate
#interface YOUR_CLASS : PARENT_CLASS <UIWebViewDelegate>
In viewDidLoad
...
[_webView setDelegate:self];
...
Call the delegate method
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView.scrollView setContentSize: CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[_webView reload]; //Or use meta tag
[_webView.scrollView setContentSize: CGSizeMake([_webView.scrollView.contentSize.width, _webView.scrollView.contentSize.height)];
}
You have to add meta tag to your HTML file
<meta name="viewport" content="width=device-width" />
or simply reload the webView
[_webView reload];
try in
-(void)viewWillLayoutSubviews
{
------
}
calculate using
self.view.bounds.size.width,
self.view.bounds.size.height
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation==UIInterfaceOrientationPortrait && toInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
{
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight)
{
}
}

NSURLErrorDomain error -999 IOS7

I am experimenting this issue while trying to open a webpage in an ios app. Before ios7 it was working perfect but now we are upgrading our app and this is the only serious problem we have.
About this problem I found that it is produced when a call is made twice. And I think this is what is happening here because if I write a NSLog text in didFailLoadWithError, it shows twice.
I tried ignoring the error but the loading circle is shown ethernally and webpage not loads.
I paste here my code, maybe you can help us or give us a clue.
#import "WebPoiViewController.h"
#import "FavoriteManager.h"
#interface WebPoiViewController ()
#end
#implementation WebPoiViewController
#synthesize webView,urlStr, activityIndicator,title2,poi=_poi,position=_position;
- (void)viewDidLoad { //We have a NIB file in play here, so I dropped the loadView here. Just make sure that your loadView is not getting called twice!
[super viewDidLoad];
if (self.poi!=nil){
self.position = [[FavoriteManager sharedInstance] isPoiInFavoriteList:self.poi];
if (self.position==-1){
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,30,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"Favorites_icon_inactive.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(makeFavorite:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
//self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Favorites_icon_inactive.png"] style: UIBarButtonItemStylePlain target:self action:#selector(makeFavorite:)];
} else {
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,30,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"Favorites_icon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(deleteFavorite:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
//self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Favorites_icon.png"] style: UIBarButtonItemStylePlain target:self action:#selector(deleteFavorite:)];
}
}
if (nil!=title2&& ![title2 isEqualToString:#""]){
self.title=self.title2;
}
[self loadView];
}
- (IBAction)makeFavorite:(id)sender {
self.position=[[FavoriteManager sharedInstance] addFavotitePOI:self.poi];
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,30,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"Favorites_icon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(deleteFavorite:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
}
- (IBAction)deleteFavorite:(id)sender {
[[FavoriteManager sharedInstance] deleteFavotitePOIat:self.position];
self.position=-1;
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,30,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"Favorites_icon_inactive.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(makeFavorite:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
}
- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = contentView;
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y = 0.0f;
webView = [[UIWebView alloc] initWithFrame:webFrame];
webView.backgroundColor = [UIColor blueColor];
webView.scalesPageToFit = YES;
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
webView.delegate = self;
if([webView respondsToSelector:#selector(scrollView)]){
webView.scrollView.bounces = NO;
}
[self.view addSubview: webView];
if (nil!=self.urlStr && ![urlStr isEqualToString:#""]){
NSLog(#"NO ES NIL");
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
//activityIndicator.center = self.view.center;
activityIndicator.center = CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2);
[self.view addSubview: activityIndicator];
} else{
[webView loadHTMLString:#"<html><center><br /><br /><font size=+5>No info<br /><br /></font></center></html>" baseURL:nil];
}
}
#pragma mark WEBVIEW Methods
- (void)webViewDidStartLoad:(UIWebView *)myWebView
{
// starting the load, show the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)myWebView
{
// finished loading, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[activityIndicator stopAnimating];
}
- (void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error
{
if([error code] == NSURLErrorCancelled) return; // Ignore this error
NSLog(#"FAIL");
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
#"<html><center><br /><br /><font size=+5 color='red'>Error<br /><br />Your request %#</font></center></html>",
error.localizedDescription];
[webView loadHTMLString:errorString baseURL:nil];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
- (BOOL)shouldAutorotate {
return [self.navigationController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
return [self.navigationController supportedInterfaceOrientations];
}
#end
I solved it by myself. I want to share the answer because maybe it helps others with the same problem.
I just added in didFailWithError this code:
- (void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error
{
if([error code] != NSURLErrorCancelled) // only goes in if it is not -999
{
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
#"<html><center><br /><br /><font size=+5 color='red'>Error<br /><br />Your request %#</font></center></html>",
error.localizedDescription];
[webView loadHTMLString:errorString baseURL:nil];
}
else{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] ]];
}
}
Now, if it fail, it tries to repeat the load until it loads. It can be improved by using a counter. Just trying it 5 or 10 times and not more like mine.

How can I add a UIActivityMonitor to my interface?

I have a view controller that houses a UIWebView. I want to display an Activity indicator in the top right so the user knows the webpage is loading; however on build&run, the activity monitor does not show.
I have a UIWebView outlet called webView and a UIActivityIndicatorView outlet called activityIndicator.
Here is my implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate=self;
[self.view addSubview:activityIndicator];
NSString *fullURL = #"http://www.oncologyeducation.com"; NSURL *url = [NSURL URLWithString:fullURL]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
- (void)viewDidUnload
{
[self setWebView:nil];
[self setActivityIndicator:nil];
[self setActivityIndicator:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
return NO;
} else {
return YES;
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
-(void) webViewDidStartLoad:(UIWebView *)webView{
NSLog(#"load started");
[activityIndicator startAnimating];
activityIndicator.hidden = NO;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView{
NSLog(#"load finished");
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
#end
On my storyboard, I have an activity indicator view which is connected to File's Owner. Thank you for any and all advice!
#CodaFi - you're right, that was not necessary. No need to add the subview.
Anybody who wants to reuse the code, lose the line:
[self.view addSubview:activityIndicator];
Turns out xCode was acting kind of wonky and copying the wrong storyboard into the project on compile. I renamed my storyboard, set it to the new name in the project settings, and the indicator spun with glee!

Resources