How can I expose an Objective-C function to JavaScript using WebKit on Mac OS X? - objective-c

I understand to do this on the iPhone you need to trap link requests (as per my other iPhone question UIWebView Expose JavaScript) and you can easily do the reverse and access JavaScript from Obj-C code.
However, I would like to have JavaScript be able to call some Objective-C functions that would somehow be registered with WebKit. I assume that you can do this better than trapping links like on the iPhone as on Mac OS X we have access to the full WebKit.
I wish to basically do the reverse of Using JavaScript from Objective-C
Update: For example is it possible to expose the objective-c method in JavaScript like self.external.objcmethod();

Have a look at my answer to this question, which is very similar. I provide code that shows you exactly how to do what you want.
Essentially, you need to implement the WebScripting protocol as per the documentation.
You don't need to define your own URL scheme handler, that is only necessary for iPhone applications. On the Mac the WebView object is many orders of magnitude more powerful than UIWebView on the iPhone and allows full bridging from JavaScript to Objective-C and vice versa.

You can browse to "javascript:my_method()" and intercept the loading...
Look at UIWebViewDelegate delegate.
Documentation at http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/Reference/Reference.html
Look at the method shouldStartLoadWithRequest
((BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType)
Addtionally, you may want to look at https://dev.mobileread.com/trac/webkitbrowser/browser/trunk/WebKit-r30377/WebKit/qt/Api/qwebframe.cpp#L167

The way I've done this in the past is to implement my own URL scheme. In your HTML pages, you'd create links like myapp://dosomefunction?aParameter=aValue. Mac OS X will open your application (if it's not already running) and pass it the URL when you click these links. It's slightly more convenient than trapping requests, and it would work with any web view anywhere on the system.
See the accepted answer to this question for details on how to set up the handler. Unfortunately, this is a one-way operation. You won't be able to get any return values back from the application.

Related

How to work with voice over in objective-C?

Its the first time when I work with voice over on objective c. i'm trying to make some simple app for blind community.
tell me necessary classes and methods to work with voice?
what should my application to except play and pause voice?
please show me main protocols and methods with examples.
Full tutorial will be appreciated :-)
video will be perfect
You just need to make the elements in your app accessible in the accessibility tree. By default they are all set to YES, so all the elements are ready by voice over. You don't have to write any code for that.
However you need to write code to post accessibility notifications, and to make some elements not read by voice over.
You can change the voice over settings in the device accessibility settings.
Please read the Apple's Documentation regarding the UIAccessibility.

Library or API for file browser view?

I would like to implement a file browser view in my application so users can open files using a side panel similar to the browsers in XCode, Text Wrangler and some other programs.
Before I go off implementing another one of those browsers from scratch, does anyone know if there are existing libraries or APIs that already does this?
Google doesn't turn up with much and most of the searches point me to NSOpenPanel which I believe doesn't do what I want.
Thanks in advance.
The Cocoa class that is used to display hierarchical lists is called NSOutlineView.
Outline views provide several configuration options to adjust the appearance.
The content can be provided by implementing a data source protocol or via Cocoa bindings.
Apple has some sample code online that should get you started (it's a file browser - so maybe you can use larger parts of that sample):
https://developer.apple.com/library/mac/#samplecode/SourceView/Introduction/Intro.html

Apple Mail and its Webview Component

Does anyone know what Apple Mail is written in?
I'm trying to determine what component it uses to render HTML, is it using the Webview Class?
Are there any other options to render HTML when building OS X applications?
It's an Objective-C/Cocoa app and it's using WebView.
I know secondhand (from a developer who was tracking down bugs in his app and comparing behavior to Mail) it takes advantage of some undocumented calls to accomplish certain things. But for the most part it's the same WebView that you've got access to.
If you'd rather render HTML a different way, you could check out Gecko, the engine/library that Firefox and Camino are based on.

How to simulate a click in a webpage in iOS with objective-C

I'm looking for an equivalent of Mechanize (Ruby/python and more) for iOS.
I need to simulate a click in a webpage (form submission) and get the response back. I tried to construct a POST-request using ASIHTTPRequest without succes. I was able to create a solution in Ruby (with Mechanize) but I want to be able to do the same in objective-c for iphone development. Any suggestions ?
As described in the duplicated posts, the API to programmatically simulate touches is private, so not appropriate for a shipping app. Follow the links if you want to know how to do it anyway.

Cocoa automated WebView

I looking into making a kind of robot testing browser. Like Selenium, but one that we can use to make full integration tests of our site. I'm wondering if it's possible to create a Cocoa app that loads up a web page in a WebView and they programmatically sends click events. I realize you could use:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
To send js click evenets, but it would be better if you could send click events to the DOMElements themselves. That way you could test file uploads and other elements that can't be accessed via javascript like flash. Does anyone know if this is possible?
You can obtain DOMNode* objects corresponding exactly to JavaScript Node objects by using a WebView's -windowScriptObject method (that returns the WebScriptingObject* that corresponds to the JavaScript window object) or any frame's -DOMDocument method to return that frame's JavaScript document method.
Example:
DOMDocument* d = [[webView mainFrame] DOMDocument];
[[[d getElementsByTagName:#"a"] item:0] click];
Fake sounds like exactly what you want. It's WebKit based, automated, has tab support, and a huge library full of useful things like evaluating JavaScript, assertions, variables, events, and loops. Highly recommended.