I am wondering how to integrate a WebView in a cocoa application:
- How to call javascript function from cocoa in the WebView
- How to handle in cocoa the click in a link inside the WebView
...
Thanks in advance for your help
First of all, there is no NSWebView in cocoa, it's called UIWebView on the iphone and WebView on Mac.
If you want to simply load a page with the webview, create an NSURLRequest (using NSURL) then call - (void)loadRequest:(NSURLRequest *)request
If you want to use javascript functions, just make an NSString containing your script, then call - (NSString*)stringByEvaluatingJavaScriptFromString:(NSString *)script.
I don't fully understand why you want to handle a click inside the webview... If you want to detect a redirect, there is a delegate method that can help you:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Related
I am adopting my iOS widget to my Mac app, and one thing I can't figure out is when I do a deep-link from the widget to the Mac app, where would that get detected? On iOS, it calls:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
But I can't find any equivalent method in the NSApplicationDelegate. The closest method is
- (void)application: (NSApplication *)application openURLs:(nonnull NSArray<NSURL *> *)urls
But this doesn't get invoked from the widget's Link.
Is there some other way to handle this on macOS? Do I need to configure something in particular for the openURLs method to be called?
EDIT:
After more testing, I've realized that the application: openURLs method should be called, whether through the widget Link or directly called using the URL scheme (as defined in the Info.plist). I verified this with another test app as well, where I can use [[NSWorkspace sharedWorkspace] openURL: testURL] to open the test app and the application: openURLs DOES get called. But it's not working for my main app for some reason, even though I've followed the same steps and defined the URL scheme in CFBundleURLTypes in the Info.plist file. The main app was created many years ago, whereas the test app was created just today.
Any ideas why the openURLs method isn't getting called in my main app?
Hello everyone,
I have a question concerning an app I'm developing for Mac OS X right now that uses a simple sqlite database. My question is how should I handle the datasource component into my app ?
Is there a simple way of making one datasource available from within different view controllers other than by using delegates that send messages to the AppDelegate ?
This might seem an odd question but the fact is that I have been developping on IOS only before and you only had to pass the objectcontext to the viewController to make it available to the view controller. On MacosX, I cannot figure out how to do the same...
Any help would be much appreciated...
How to pass the context from one view controller to another:
newViewController.managedObjectContext = self.managedObjectContext;
Alternatively, you can keep the context in the app delegate and override initWithCoder which should be called when a view controller is initialised from storyboard or xib:
AppDelegate *delegate = (AppDelegate*) [[NSApplication sharedApplication] delegate];
self.managedObjectContext = delegate.managedObjectContext;
I am building a web browser in Xcode cocoa application, using the interface builder. I have it all working when it comes to typing in web address and clicking go.
but I would like for it to automatically load a web page instead of seeing white when I open my project, I looked in inspector to see if you can set a specific web address but I couldn't find anything to do this.
How about if you load the URL that you would like in the viewdidload function:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *targetURL = [NSURL fileURLWithPath:WebsitePath];
NSURLRequest *WebRequest = [NSURLRequest requestWithURL:targetURL];
[self.WebViewer loadRequest:WebRequest];
self.WebViewer.scalesPageToFit = YES;
}
Maybe you can try to load the WebView when the application finish launching,just add the URL Request in the applicationDidFinishLauching parameter,else if your WebView is on the MainView you can add a loadingScreen that appear while the webView is loading!
Lucky!
I've created a new Mac OSX Application in Xcode, included a WebView via Interface Builder and now I'm trying to programmatically tell it what URL to load. Here's how far I got:
Create the project
Include Webkit.framework
Include the WebView in Interface Builder
Then from there I've included what I think is necessary to get access to the WebView, including:
#import <WebKit/WebKit.h>
#synthesize webView;
// etc, I think this is all good
Where I fall over is getting access to the WebView itself and telling it what URL to load. I'm trying:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString *urlAddress = #"http://www.example.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[webView mainFrame] loadRequest:requestObj];
}
But I think I'm supposed to connect some stuff in Interface Builder first?
My end goal is to create a Mac OSX Application that's essentially just a View-Based Application (which is simple to do using the iPhone SDK). From there I want to include a local htdocs folder with my own local .html files but the first step is to tell the WebView what URL to load :)
Thanks!
The code you've written above is correct, I just tested it then and successfully loaded a URL.
I'm not sure what you've declared in your header file but you should have something like this:
//usual #import(s)
#import <WebKit/WebKit.h>
#interface myAppDelegate : NSObject <NSApplicationDelegate> {
WebView *myWebView;
//other instance variables
}
#property (retain, nonatomic) IBOutlet WebView *myWebView;
//other properties and methods
#end
As #pulazzo says, you also need to connect your code with the control defined in Interface Builder.
To do this, in Interface Builder, you need to find your application delegate class, right-click (or control-click) on it and find myWebView in the list of outlets. Drag from the circle on the same line as myWebView onto your web view. I don't think my explanation is much chop so you'll probably have a lot more luck reading the Interface Builder Quick Start Guide on the Apple developer site.
In your implementation file, you need to implement the getter and setter methods for your new property, myWebView. You (probably) don't need to do any customisation to this so you can just use #synthesize:
#implementation myAppDelegate
#synthesize window;
#synthesize myWebView;
//your function etc
#end
Did you declare your webView property as an IBOutlet and connect it to your WebView in Interface Builder?
If not, then webView isn't set and your code will just silently fail to do anything.
yes, you says right! he declared like this:
IBOutlet WebView webView;
but it doesn't need necessary coding with #sinthesize,
you can just use a awakeFromNib method in your appController implementation.
-(void)awakeFromNib{
//etc etc
}
Symptom:
View the flash website with any browser, the links inside the flash content work and launch a new window
view the flash with my custom WebView, click the same links. No response.
I tried delegating "decidePolicyForNewWindowAction" and "decidePolicyForNavigationAction" but ironically, these are only called after I launch a URL from my code, NOT as a response to the user clicking the link from the page.
I am sure the fact this is a flash app content has something to do with it. I don't know how to resolve this. Help will be appreciated.
You'll need to set a UIDelegate for the WebView, and then implement - (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request in that delegate.
If you then set a policy delegate for the new WebView that you return (that WebView can be in a hidden window), the delegate method that will be called is:
- (void)webView:(WebView *)aWebView
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id < WebPolicyDecisionListener >)listener`
The URL to which the Flash app is trying to send the window is in the WebActionOriginalURLKey of the actionInformation dictionary, and you can at that point decide if you want the navigation to proceed, or send an ignore to the WebPolicyDecisionListener and handle the URL some other way.