Unable to show the activity indicator iOS - ios

I can't get to display the activity spinner on UIWebView
Need to show UIActivityIndicatorView while the page is being loaded.
UIActivityIndicatorView is declared but it's never shown on the UIWebView
Please assist.
The code is as follows :
#import <QuartzCore/QuartzCore.h>
#import "MyScreen"
#import "Reachability.h"
#interface MyScreen ()
#end
#implementation MyScreen
{;
UIView *loadingView;
}
#synthesize gotoMainMenu;
- (void)viewDidLoad {
[super viewDidLoad];
loadingView = [[UIView alloc]initWithFrame:CGRectMake(100, 400, 80, 80)];
loadingView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.6];
loadingView.layer.cornerRadius = 5;
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(loadingView.frame.size.width / 2.0, 35);
[activityView startAnimating];
activityView.tag = 100;
[loadingView addSubview:activityView];
UILabel* lblLoading = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, 80, 30)];
lblLoading.text = #"Loading...";
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont fontWithName:lblLoading.font.fontName size:15];
lblLoading.textAlignment = NSTextAlignmentCenter;
[loadingView addSubview:lblLoading];
[self.view addSubview:loadingView];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:true];
self.navigationController.navigationBar.hidden=false;
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus != NotReachable)
{
NSLog(#"Network is Available");
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Network Error" message:#"Please check internet connectivity!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil , nil];
[alertView show];
[self.navigationController popViewControllerAnimated:YES];
}
}
-(id) init
{
if([super init])
{
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, -64.00, self.view.frame.size.width, self.view.frame.size.height+64.0)];
webView.scalesPageToFit = YES;
webView.userInteractionEnabled = YES;
url = [[NSURL alloc] initWithString:#"http://theURL/"];
req = [[NSURLRequest alloc] initWithURL:url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:5.0];
[webView loadRequest:theRequest];
[webView loadRequest:req];
[self.view addSubview:webView];
}
return self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[loadingView setHidden:YES];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[loadingView setHidden:NO];
}
#end

You should not use "self.view" in your init function. When you use it, the view is loaded and viewDidLoad will be invoked before you add webView. So you webView is added after your loading view.
Just try to move this line:
[self.view addSubview:webView];
After:
[self.view addSubview:loadingView];

Related

Wkwebview setDelegate Equivalent

No visible #interface for 'WKWebView' declares the selector 'setDelegate:'
[self.webView setDelegate:self];
[self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:self.url]]];
if(HUD == nil){
HUD = [[MBProgressHUD alloc] initWithView:self.webView];
[self.webView addSubview:HUD];
HUD.delegate = self;
HUD.dimBackground = YES;
[HUD setColor:[UIColor darkGrayColor]];
}
[HUD show:YES];
}
No visible #interface for 'WKWebView' declares the selector 'setDelegate:'

How to change background color of UIWebBrowserView in UIWebView in iOS8 Objective C

I wanna change UIColor of UIWebBrowserView in web view in iOS8 Objective C.
Please help me.
Thank you.
plz use this code
UIWebView * webView_article = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
webView_article.backgroundColor =yourClolor;
webView_article.delegate=self;
webView_article.scrollView.bounces = NO;
webView_article.scrollView.showsVerticalScrollIndicator = NO;
webView_article.scrollView.delaysContentTouches = NO;
webView_article.scrollView.delegate = self;
[webView_article setTag:11];
[webView_article setOpaque:NO];
you have to set setOpaque to No
UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
web.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:32.0/255.0 blue:32.0/255.0 alpha:0.5];
web.delegate=self;
web.scrollView.bounces = NO;
web.scrollView.showsVerticalScrollIndicator = NO;
web.scrollView.delaysContentTouches = NO;
web.scrollView.delegate = self;
[web setOpaque:NO];
pragma -web view method
-(void)webViewDidStartLoad:(UIWebView *)webView{
[SVProgressHUD showWithStatus:#"Loading..." ];
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
NSLog(#"start ...");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[SVProgressHUD dismiss];
NSLog(#"finish");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[SVProgressHUD dismiss];
NSLog(#"Error for WEBVIEW: %#", [error description]);
}

show progress bar until video loads IOS7

Hi in my application I'm playing the video using URL. I'm passing the video URL form my server now the problem its taking to much time to play the video. So i want to show progress bar until its load the video .
So i have used the MBProgressHUD for progress bar its showing the progress bar but the video is not playing please tell where I'm doing wrong.
My code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * currentVideo = [videoarray1 objectAtIndex:indexPath.row];
NSString *strurl=[self urlencode:currentVideo];
NSURL *url=[NSURL URLWithString:strurl];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
NSURLConnection *con=[NSURLConnection connectionWithRequest:req delegate:self];
if (con) {
datas=[[NSMutableData alloc]init];
}
spinner = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
spinner.mode = MBProgressHUDModeCustomView;
[spinner setLabelText:#"Loading file....."];
[spinner setLabelFont:[UIFont systemFontOfSize:15]];
[spinner show:YES];
}
-(NSString *)urlencode:(NSString *)str
{
NSString *encodeString=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL, (CFStringRef)#"", kCFStringEncodingUTF8));
return encodeString;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(#"%lld",[response expectedContentLength]);
self.length = [response expectedContentLength];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1{
[datas appendData:data1];
float progress = (float)[datas length]/(float)self.length;
NSLog(#"%f",progress);
float check=progress*100;
if (check==100) {
[spinner hide:YES];
[spinner removeFromSuperViewOnHide];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSData *data = datas;
NSLog(#"%#",data);
NSString* myurl;
myurl = [[NSString alloc] initWithData:datas encoding:NSASCIIStringEncoding];
NSLog(#"%#",myurl);
_movieplayer = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:myurl]];
[[_movieplayer view] setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview: [_movieplayer view]];
[_movieplayer setShouldAutoplay:YES];
[_movieplayer prepareToPlay];
[_movieplayer play];
}
I have used the above code its not playing the video please tell where I'm doing wrong in the above code how to achieve this one.
Thanks.
1.create new view control
2.pass your videoarray1 value to nextvieww control i mean your video url
3.add following code nextview control
- (void)viewDidLoad
{
[super viewDidLoad];
self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
self.indicator.center = self.view.center;
[self.view addSubview:self.indicator];
[self.indicator startAnimating];
_movieplayer = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:[self urlencode:self.strPlayUrl]]];
[[_movieplayer view] setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview: [_movieplayer view]];
[_movieplayer setShouldAutoplay:YES];
[_movieplayer prepareToPlay];
//this is the line we need to do
[self.view insertSubview:self.movieplayer.view belowSubview:self.indicator];
[self.movieplayer play];
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(#"VIEW DID LOAD");
// Register to receive a notification that the movie is now in memory and ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieLoadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
-(void)movieLoadStateDidChange:(id)sender
{
NSLog(#"STATE CHANGED");
if(MPMovieLoadStatePlaythroughOK ) {
NSLog(#"State is Playable OK");
NSLog(#"Enough data has been buffered for playback to continue uninterrupted..");
self.indicator.hidden = YES;
[ self.indicator stopAnimating];
}
}
-(NSString *)urlencode:(NSString *)str
{
NSString *encodeString=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL, (CFStringRef)#"", kCFStringEncodingUTF8));
return encodeString;
}

How to show alertview indicator before pulling json data( iOS6 )

Could any one please help me with this code i'm trying to call alertview indicator before pulling json data in my iOS app but i don't know how to check if the data already loaded and then disappear the alertview indicator, For now it always appears even all data aleady loaded. here is my code:
//
// Firstcine.m
//
//
//
//
#import "Firstcine.h"
#interface Firstcine (){
UIAlertView *loading;
}
#end
#implementation Firstcine
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Loading Show Alert View...
loading = [[UIAlertView alloc] initWithTitle:#"" message:#"Please Wait..."
delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView
alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loading addSubview:progress];
[progress startAnimating];
[loading show];
// dis play all data after first loaded
NSURL *url = [NSURL URLWithString:#"http://localhost/App/first.php"];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSArray *json = [NSJSONSerialization JSONObjectWithData:
jsonData options:NSJSONReadingAllowFragments error:nil];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg_tile.png"]];
NSDictionary *dict = [json objectAtIndex:0];
name.text = [dict valueForKey:#"name"];
time.text = [dict valueForKey:#"time"];
date.text = [dict valueForKey:#"date"];
price.text = [dict valueForKey:#"price"];
// then display photo
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(75, 87, 150,200)];
image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[dict valueForKey:#"photo" ]]]];
[self.view addSubview:image];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Have you tried something like this
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Configuring Preferences\nPlease Wait..."
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[alertView show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alertView.bounds.size.width / 2, alertView.bounds.size.height - 50);
[indicator startAnimating];
[alertView addSubview:indicator];
dispatch_queue_t downloadQueue = dispatch_queue_create("saver", NULL);
dispatch_async(downloadQueue, ^{
// do some heavy lifting ? like a fetch
dispatch_async(dispatch_get_main_queue(), ^{
// hide the view
[alertView dismissWithClickedButtonIndex:0 animated:YES];
});
});

ios : UIWebview and its delegate methods in same class not working

I want to open links of uiwebview in to the safari browser my code is working perfectly if I implement shouldStartLoadWithRequest method in viewController but when I implement shouldStartLoadWithRequest in same class and set UIWebView's delegate to self it doesn't work it get halt in between and shows assembly level code with error EXC_BAD_ACCESS(code=2, address=0x9) my files are as follows
//content of ShowView.h file
#import <UIKit/UIKit.h>
#interface ShowView : UIView <UIWebViewDelegate> {
}
- (void) showViewFunction;
#property (nonatomic, assign) UIViewController *mainViewContObj;
#end
//content of ShowView.m file is :
#import "ShowView.h"
#implementation ShowView
- (void) showViewFunction {
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[aWebView setDelegate:self];
NSString *urlAddress = #"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
aWebView.delegate = self;
[aWebView loadRequest:requestObj];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[[[self mainViewContObj] view] addSubview:aWebView];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"In shouldStartLoadWithRequest method");
if ([[[request URL] absoluteString] isEqual:#"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
#end
// Content of ViewController.h
#import "ViewController.h"
#import "ShowView.h"
#interface mnetViewController ()
#end
#implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Some times html page shows but when I click on link it opens in same UIWebview window, and not even going into the shouldStartLoadWithRequest method, am I doing anything wrong?
Your code isn't clear, might need more info's, but from what i see, the ShowView class is never instanciated, so it shouldn't even show.
you should make something like this i guess :
//mnetViewController.m
#import "mnetViewController.h"
#import "ShowView.h"
#interface mnetViewController ()
#end
#implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
ShowView* theShowView = [[ShowView alloc] initWithFrame:CGRectMake(insert the frame you want your webview to have)];
theShowView.autoresizesSubviews = YES;
[self.view addSubview:theShowView];
[theShowView release];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
now for the ShowView class, try something like this :
//ShowView.h
#import <UIKit/UIKit.h>
#interface ShowView : UIView <UIWebViewDelegate> {
}
#end
//ShowView.m
#import "ShowView.h"
#implementation ShowView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
aWebView.scalesPageToFit = YES;
[aWebView setDelegate:self];
[self addSubview:aWebView];
NSString *urlAddress = #"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
[aWebView release];
}
return self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"In shouldStartLoadWithRequest method for URL : %#",[request [URL absolutString]]);
if ([[[request URL] absoluteString] isEqual:#"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
This should work, i didn't try it, i'll comeback tomorrow to try it if necessary.

Resources