Reload WebView from Menu Item (OS X) - objective-c

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

Related

Activity Indicator problems? (SVProgress HUD)

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;

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];

iOS 5 movie player

Hi everyone i got a problem to play a video in iOS 5. When I'm building the program i get a warning "local declaration of hides instance variables" and when i run the program and tap the button i get a "SIGABRT". And i don't know what to do, all the help i can get is aprriciated!!
Thx in advance
MY .h file
#import <MediaPlayer/MediaPlayer.h>
#interface MainViewController : UIViewController {
MPMoviePlayerController *moviePlayer;
}
-(IBAction)Video:(id)sender;
#end
MY .m file
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
-(IBAction)Video:(id)sender {
MPMoviePlayerController *moviePlayer;
NSString *path = [[NSBundle mainBundle] pathForResource:#"big-buck-bunny-clip" ofType:#"m4v"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer setControlStyle:MPMovieControlStyleDefault];
[moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
local declaration of hides instance variables
remove the first line of your -(IBAction)Video:(id)sender method.
MPMoviePlayerController *moviePlayer; //this line;
because you already have declared such variable in your .h file.
Means you declared the same variable names two times, one in .h file and another in .m file.

Why does my UIWebView not Allow User Interaction?

I'm new to these forums so I apologize for my noobieness. I did as thorough a search as I could, but I couldn't find anyone else with this issue, applogise if this has been covered elsewhere.
I've created a very simple example of my problem. I'm sure I'm missing something but I can't for the life of me figure out what.
I'm creating a UIWebView and adding it to a custom view controller that inherits from UIViewController. When the app loads in the iPad simulator, the uiwebview loads the desired page, but the UIWebView is entirely unresponsive. The webview does not pan or scroll and none of the in page links can be clicked. However, if you change the orientation of the webview suddleny everything works.
Thanks in advance for your help!!
AppDelegate header
#import <UIKit/UIKit.h>
#import "EditorViewController.h"
#interface FixEditorTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
EditorViewController *editorView;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) EditorViewController *editorView;
#end
AppDelegate Implementation
#import "FixEditorTestAppDelegate.h"
#import "EditorViewController.h"
#implementation FixEditorTestAppDelegate
#synthesize window;
#synthesize editorView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"application is loading");
editorView = [[EditorViewController alloc] init];
[window addSubview:[editorView view]];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[window release];
[editorView release];
[super dealloc];
}
#end
View Controller header
#import <UIKit/UIKit.h>
#interface EditorViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
View Controller Implementation
#import "EditorViewController.h"
#implementation EditorViewController
#synthesize webView;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSLog(#"loadView called");
UIView *curView = [[UIView alloc] init];
webView = [[UIWebView alloc] init];
webView.frame = CGRectMake(20, 40, 728, 964);
webView.delegate = self;
webView.backgroundColor = [UIColor redColor];
[curView addSubview: webView];
self.view = curView;
[curView release];
}
//Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad called");
NSURL *url = [[NSURL alloc] initWithString:#"http://www.nytimes.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
[url autorelease];
[request release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
webView.delegate = nil;
[webView release];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
You should really be loading this from a nib, but if you're going to create it programmatically, you probably want to add this to loadView:
webView.userInteractionEnabled = YES;
webView.scalesPageToFit = YES;
I'm going to go out on a limb here, as you aren't getting much response..
There is nothing wrong with this code (out on a limb! i haven't tested it). Neither is there anything wrong with not using a .nib (but really, you would have to have a reason, right?).
Using a nib doesn't do anything magic, just pretty much what you have done here.
Your problem must be elsewhere.. can you interact with a different kind of view? A button, say? Maybe it is something to do with the window setup?
The issue is with the extra UIView you have in there. It isn't needed, and I think the touch events aren't getting enabled. I tried out the code and it didn't work like you said. Then I changed the EditorViewController to this, and it works now (all I did was to remove the extra UIView) dragging and select links now works.
- (void)loadView {
NSLog(#"loadView called");
webView = [[UIWebView alloc] init];
webView.frame = CGRectMake(20, 40, 728, 964);
webView.delegate = self;
webView.backgroundColor = [UIColor redColor];
self.view = webView;
}