Activity Indicator problems? (SVProgress HUD) - objective-c

I am trying to load a UIWebview and am trying to implement the SVProgress HUD when the webview is loading and then dismissing it when its loaded.I have almost completed it however when it loads the progress hud shows for a second and then dosent show and then shows for a second and so fourth, like its blinking I'm kinda stuck, check out my code.
In my .h:
#import <UIKit/UIKit.h>
#import "SVProgressHUD.h"
#interface ViewController2 : UIViewController <UIWebViewDelegate> {
//Instantiated the timer varibale inside the interface.
NSTimer *timer1;
IBOutlet UIWebView *webView;
}
//Instantiate the webview ans NSString link.
#property(nonatomic,retain) UIWebView *webView;
#property (strong, nonatomic) NSString *link;
and in my .m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
#synthesize webView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Getting the link from the NSString called "link" and putting in the request.
NSString *link = [NSString stringWithString:self.link];
NSURL *URL = [NSURL URLWithString:link];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[webView loadRequest:request];
//Set the time for the loagind modal view.
timer1 = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loading) userInfo:nil repeats:YES];
//Webview Properties
//Webview's name is web, which was instantianted in the propeties section.
webView.ScalesPageToFit = true;
webView.backgroundColor = [UIColor pomegranateColor];
}
//When the UIWebview is loading present "Fetching Homework" alert, thanks to SVProgressHUD
-(void)loading {
if(!webView.loading)
[SVProgressHUD dismiss];
else
[SVProgressHUD showWithStatus:#"Loading Homework"];
}
I think that it could just be the timer, but I'm not sure, anyhelp would be greatly appreciated.
Thanks in advance.

You should use the web view delegate to know when the page ends loading instead of using a timer to check it each 0.5 seconds. Try it this way:
Begin the SVProgressHUD when inits the page loading
[SVProgressHUD showWithStatus:#"Loading Homework"];
Then remove it when the page finish loading.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[SVProgressHUD dismiss];
}
Don't forget that you also have to set your webview delegate as this:
webView.delegate = self;

Related

How to restart the scheduled NSTimer in UIViewController when UIView.xib is dismissed in ios 9?

I have created a timer that is fired after 10.0 seconds and calls targetMethod: on self, which is a pointer to the NSTimer instance.This timer should only work when its associated viewcontroller screen is in the view.I have stopped the timer while loading .xib file called PhotoView.xib because timer blocks the UI. But I don't know how to start the timer again when PhotoView.xib is removed/dismissed from the Viewcontroller.
Can someone please tell me how to restart the timer(of parentViewcontroller) when .xib is dismissed from parentViewcontroller?
SampleView.h
#import <UIKit/UIKit.h>
#interface SampleView : UIView
#property(nonatomic,retain)IBOutlet UIView* view;
#property(nonatomic,retain)IBOutlet UIImageView* imageview;
#property(nonatomic,retain)IBOutlet UIButton* dismissButton;
#end
SampleView.m
#import "GroupPhotoView.h"
#implementation SampleView
#synthesize imageview,view,dismissButton;
-(IBAction) dismissView: (id) sender{
[self removeFromSuperview];
}
#end
SampleViewController.h
#import <UIKit/UIKit.h>
#interface SampleViewController : UIViewController
#property(nonatomic,retain)NSTimer *timer;
#end
SampleViewController.m
#import "SampleViewController.h"
#interface SampleViewController ()
#end
#implementation SampleViewController
#synthesize timer;
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated{
self.timer=[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(sampleMethod:) userInfo:nil repeats:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
//stop the timer when SampleVC is not in the view
if ([self.timer isValid])
{
[self.timer invalidate];
self.timer=nil;
}
}
- (IBAction)displayAction:(id)sender
{
[self loadSampleImage];
}
-(void)loadSampleImage:(UIImage*)aImage{
if ([self.timer isValid])
{
[self.timer invalidate];
self.timer=nil;
}
SampleView *customView =(SampleView *)[[[NSBundle mainBundle] loadNibNamed:#"PhotoView" owner:self options:nil] objectAtIndex:0];
customView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
customView.imageview.image=[UIImage imageNamed:#"cs"];
[customView setFrame:self.view.frame];
[self.navigationController.view addSubview:customView];
}
}
#end
Thanks in advance.
You can use Delegate to restart NSTimer again. A delegate allows one object to send messages to another object when an event happens. A delegate is use for similar problem when object A calls to object B but once action completed A should know that B has completed the task and take necessary action.
Create SampleView :
// Delegate to respond back in SampleView.m
id <SampleView> _delegate;
// OR in SampleView.h
#property (strong, nonatomic) id _delegate;
After that add action completed call in SampleView.m :
-(IBAction) dismissView: (id) sender{
[self removeFromSuperview];
[_delegate startTimer];
}
Add method start Timer in SampleViewController.m:
customView.delegate = self;
-(void)startTimer
{
self.timer=[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(sampleMethod:) userInfo:nil repeats:YES];
}
It will start timer again after .xib is dismissed from parentViewcontroller.

Reload WebView from Menu Item (OS X)

I want to reload a WebView whenever a certain menu item is clicked. Here's my code that's not working. (Assume normal declarations in .h files.)
// AppDelegate.m
#import "AppDelegate.h"
// #import "ViewController.h" (in AppDelegate.h)
#interface AppDelegate ()
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
// Menu Item triggers this:
- (IBAction)reloadWebView:(id)sender {
NSLog(#"AppDelegate reloadWebView");
ViewController * vc = [[ViewController alloc] init];
[vc reloadWebView:sender]; // tried this,
[vc.webview reload:sender]; // and this,
[[vc.webview mainFrame] reload]; // and this,
[[vc.webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // and this.
}
#end
// ViewController.m
#import "ViewController.h"
// #property (assign) IBOutlet WebView *webview; (in ViewController.h)
#implementation ViewController
#synthesize webview;
- (void)viewDidLoad {
[super viewDidLoad];
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // this works
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
// Called in AppDelegate:
- (void)reloadWebView:(id)sender {
NSLog(#"ViewController reloadWebView");
// The problem here seems to be: webview == nil
[webview reload:sender]; // also tried this,
[[webview mainFrame] reload]; // and this,
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // and this.
}
#end
This is all my code. I've just started working on this project.
I've tried using a button in the ViewController. That works, but I need this to be a menu item.
Thanks in advance!
instead of creating a new view controller:
ViewController * vc = [[ViewController alloc] init];
try:
ViewController * vc = (ViewController *)[[[NSApplication sharedApplication] mainWindow] contentViewController];
to call the current view controller

Custom Segue doesn't Play Sound

I made a custom segue and I want it to play sound when changing views. Unfortunately, it doesn't. Method playSound works fine when I use it in View Controller. But i don't want to export it in every View Controller i need. How can i improve my code and why segue doesn't want to play sound?
#import "CustomSegue.h"
#import AVFoundation;
#interface CustomSegue()
#property (nonatomic, strong) AVAudioPlayer *player;
#end
#implementation CustomSegue
#synthesize player;
- (void) perform
{
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[UIView transitionWithView:src.navigationController.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[src.navigationController pushViewController:dst animated:NO];
}
completion:NULL];
[self playSound];
}
- (void) playSound
{
int r = arc4random_uniform(7) + 1;
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%i", r] ofType:#"caf"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[self.player prepareToPlay];
[self.player play];
}
I bet your custom segue is released (by ARC) before the sound has a chance to play, and your audio player is released with it. The Life Cycle of a Segue is such that it is deallocated immediately after -performis executed.
What you need to do is keep the audio player around for a longer time. One way to do this is to add a (strong) reference to your AVAudioPlayer from an object that is not going to be deallocated for at least as long as the sound will play. For example, place the player in your app delegate, which sticks around as long as the app is running.
Try moving the player property and the -playsound method into your app delegate. The property should look something like:
#property (strong, nonatomic) AVAudioPlayer *player;
Then, in this segue, in place of [self playsound]; make a call to the app delegate with something like:
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate playsound];

web view did finish load labelname.hidden = yes not working?

I am very new to iOS programming and am making a simple app. the first UITabBarItem loads a page but before it loads there is a label. I am trying to make the label disappear after the web page loads but it doesn't work. I believe I need to set the web view delegate but I don't know how.
firstcontroller.h
#import <UIKit/UIKit.h>
#interface OTFFirstViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webPage;
#property (strong, nonatomic) IBOutlet UILabel *pageLoading;
#end
firstcontroller.m
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *fullURL = #"http://asdf.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webPage loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)_webPage
{
_pageLoading.hidden = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Insert _webPage.delegate = self; before calling loadRequest:.
You should alse modify interface definition to #interface OTFFirstViewController : UIViewController <UIWebViewDelegate>.

How Do I Update UIWebView After viewDidLoad?

This is my first iOS app, so I am probably missing something very simple. Please be kind. I have been tearing my hair out and I could really use some help.
Overview Of App
Basically, this is a single page application that just loads a UIWebView. I have an external accessory (bluetooth barcode scanner) that I connect and basically what I want to do is when the the app receives a scan, I want to call a method in my ViewController and update the UIWebView accordingly.
What Is Working
I am able to connect the scanner, load the first view, which loads the initial webpage, scan a barcode and call the method in my controller.
My Problem
I can't seem to figure out how to update the UIWebView from the method in my controller. It logs the url string to my debugger area, but never actually updates the webview. I am pretty sure I have some delegation wrong or something with my webview instance. There must be some glue code here that I am missing.
My Code HelloWorldViewController.h
#import <UIKit/UIKit.h>
#import "KScan.h"
#interface HelloWorldViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *page;
IBOutlet UILabel *myLabel;
Boolean IsFirstTime;
KScan *kscan;
}
- (void)setFirstTime;
- (void)DisplayConnectionStatus;
- (void)DisplayMessage:(char *)Message;
- (void)newBarcodeScanned:(NSString *)barcode;
- (void)loadBarcodePage:(NSString *)barcode;
#property (nonatomic, retain) KScan *kscan;
#property (nonatomic, retain) UIWebView *page;
#property (nonatomic, retain) UILabel *myLabel;
#end
My Code HelloWorldViewController.m
#import "HelloWorldViewController.h"
#import "common.h"
#implementation HelloWorldViewController
#synthesize myLabel;
#synthesize page;
#synthesize kscan;
- (void)setFirstTime
{
IsFirstTime = true;
}
- (void)viewDidLoad
{
self.kscan = [[KScan alloc] init];
[super viewDidLoad];
page.scrollView.bounces = NO;
//page.delegate = self;
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://192.168.0.187:3000"]]];
}
- (void) newBarcodeScanned:(NSString *)barcode
{
NSLog(#"%s[%#]",__FUNCTION__, barcode);
[self loadBarcodePage:barcode];
}
- (void)loadBarcodePage:(NSString *)barcode
{
NSLog(#"%s",__FUNCTION__);
NSString *url = [[NSString alloc] initWithFormat:#"http://www.google.com/%#", barcode];
NSLog(#"%#", url);
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
- (void)viewDidUnload
{
[myLabel release];
myLabel = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)dealloc {
[page release];
[kscan release];
[myLabel release];
[super dealloc];
}
#end
Basically, I am just trying to load google.com into my page webview when scanning a barcode. My log statements are being logged with the correct URL, but this line of code doesn't work.
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
I am not getting any errors and my xCode debugging skills are not the greatest.
Any help would be greatly appreciated!
It looks like your webview is never allocated, or added to your main view. You are probably talking to a nil instance.
Unless your web view comes from a XIB file (which I doubt since it is not declared as an IBOutlet in your heder file) try adding something like this to your viewDidLoad:
self.page = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.page];