I'm sure this is a noob question but I've read countless similar problems and not found an explanation specific to this error. I therefore post in the hope of helping future googlers.
I want a clicked button (OSX, Cocoa) to launch a URL in a WebKit WebView. I am using an AppDelegate and the (boilerplate, works-for-everyone-else) code in AppDelegate.m is:
- (IBAction)goClick:(id)sender {
[[WebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"google.com"]]];
}
The compile-time error is No known class method for selector mainFrame.
I discovered if I create an Outlet in my AppDelegate.h:
#property (nonatomic, retain) IBOutlet WebView *wvOutlet;
it works with:
[[self.wvOutlet mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"google.com"]]];
So it all works with an instance (self.wvOutlet) method but not with the method mainFrame on the class WebView.
Why?
[WebView mainFrame] is attempting to call the class method +mainFrame. No such class method exists.
[self.wvOutlet mainFrame] is calling the instance method -mainFrame. This is correct.
It seems I misspelled webView (an instance) as WebView.
Solution: declare the outlet webView in your AppDelegate.h file:
#property (weak) IBOutlet WebView *webView;
Then you should be able to do:
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"google.com"]]];
Disclaimer: not yet tested!
Related
I have a problem here, which I couldn't find a solution yet.
In my app, I have a tableView which parses the youtube videos from my channel. If selecting an item, it pushes to a UIWebView, which shows the youtube website with my video embedded.
However, when I tab play on the iPhone it opens the native fullscreen player. But on the iPad there are two issues:
It doesn't open in fullscreen, the navigation bar still remains visible. Also it's not the native player but the a resized youtube player.
My vids are all in 1080p, however on the iPad I can only select 720p.
Here is the code from my WebView:
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize videoURL = _videoURL;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:self.videoURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
And here from the header file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (nonatomic, strong) NSString *videoURL;
#end
How can I make it to use the native player on iPad as well? And is there a possibility to open the video directly without showing the youtube website? I've already tried to select and deselect "Allow inline rendering" with no change of the behavior.
I appreciate your help...
Here is a link to my solution for a similar Thumbnail -onTouch-> Fullscreen on iPad and iPhone need. https://stackoverflow.com/a/25695708/3397249
I'm making a simple app, to load up a single URL, I have the following code
webberAppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface webberAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
WebView *webber;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet WebView *webber;
#end
webberAppDelegate.m
#import "webberAppDelegate.h"
#implementation webberAppDelegate
#synthesize window;
#synthesize webber;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString *urlString = #"http://www.apple.com";
// Insert code here to initialize your application
[[webber mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
#end
What do I have to do in Interface builder? I've added the Web View, do I need to link it in any way?
Thing is I added simple text field(and linked it to the web view) and it loads the URL just fine when I enter it there, however it doesn't with this code, any ideas?
In your XIB file, you should have an App Delegate object (and some other things).
Right-clicking it should bring up a HUD list, where you'll see each of your outlets (and, again, some other things).Find the webber outlet, and click the circle to its right. Then drag to your WebView.
What you've just done is linked your properties to your XIB objects. Skip this step, and your App Delegate won't know what webber is referring to.
Also, note that in order to populate that HUD list, your properties need to be specifically tagged with IBOutlet.
My program have two classes, first of them is regular app delegate NSObject. The second one is a NSWindowController subclass with NSWindow and WebView on it, which shows html page from application bundle.
Here is how i call window from AppDelegate:
-(IBAction)showWebViewForm:(id)sender
{
webViewForm = [[WebViewForm alloc] initWithWindowNibName:#"WebViewForm"];
[webViewForm showWindow:self];
}
Here is how i render the webpage in WebViewForm : NSWindowController:
-(void)awakeFromNib{
[NSApp activateIgnoringOtherApps:YES];
[webview setUIDelegate: self];
[webview setResourceLoadDelegate: self];
[webview setPolicyDelegate:self];
[webview setFrameLoadDelegate:self];
[[webview mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"data"]]]];
}
It's work fine, but only for the first time. When i close WebViewForm window and reopen it, webpage is disappearing from WebView. Why is so that, and how to fix it?
UPD Even if embed awakeFromNib code in special new method like -(void)refreshWebview and then call [webViewForm refreshWebview] right after [webViewForm showWindow:self];, still — page loads only for first time, and it's really weird ( Any ideas?
I figure it out by myself. The problem was, that webview closed when it's parent window closed. So, there is special method for case like that:
[webview setShouldCloseWithWindow:NO];
This is a Cocoa app I am working on. I'm trying to embed a simple WebView with some basic content in an NSWindow in my app. I've wired my WebView to my NSWindowController, and anytime I try to pass a selector to that WebView, things are fine. But to load content, I need to use the WebFrame. And XCode refuses to recognize any methods in WebFrame (although it seems to recognize that WebFrame is a proper class.) For example:
[[webView mainFrame] loadHTMLString:htmlString baseURL:nil];
generates a 'loadHTMLString: baseURL:' not found compiler warning. It does function at runtime, but of course I'd like to eliminate the compiler warning.
Any thoughts on what's up?
Thanks in advance.
(Edited to add more code)
Below is from my ViewController's header file:
#import <Foundation/Foundation.h>
#import <WebKit/WebView.h>
#interface ReportViewController : NSWindowController {
IBOutlet WebView *webView;
}
#property (nonatomic, assign) WebView *webView;
And below here is a snippit from my Document implementation:
ReportViewController *window = [[ReportViewController alloc] initWithWindowNibName:#"Report"];
[window showWindow:nil];
WebView *webView = [window webView];
[htmlData writeToFile:tmpfile atomically:YES];
Thanks to Bavarious' hint, I realized that I needed to import <WebKit/WebKit.h> as opposed to <WebKit/WebView.h>.
I'm rather new with the whole OSX programming, I wanted to stick a WebView into an empty app. Apparently it isn't as simple as sticking a WebView on a window in interface builder and creating an outlet.
IBOutlet WebView *webView;
It gives me a
expected specifier-qualifier-list before 'WebView'
and when I don't use an outlet, it terminates due to uncaught exception. I'm not too sure what these error messages mean.
Seems it isn't that simple!
You also need to add the WebKit framework to your target and forward declare WebView or import the header file:
// header file:
#class WebView; // forward declaration sufficient in header
#interface WhatEver ... {
WebView* webview;
// ...
#property (assign) IBOutlet WebView *webview;
#end
// implementation file:
#import <WebKit/WebView.h> // if you want to do something with the WebView
#implementation WhatEver
#synthesize webview;
// ...
#end
Did you try to link the WebKit framework, then importing it?
#import "WebKit/WebKit.h"