7

On both Mac and iOS platforms, it is possible to do two-way interchange with the native runtime through custom URI schemes / a NSURLProtocol. For example.. to request an NSImage from a native Objective-C method, you can register your custom handler (a simple string, here i used "mycustomprotocol") with Webkit / your WebView NSView, and call it from JS like…

var theURL = 'mycustomprotocol:///' + (textField.value);
img.innerHTML = '<img src="'+theURL+'" id="string"/>';

I would LIKE to be able to use jQuery to do make requests, as at this point, it is more familiar than 90's-style JS.. but as far as I can find on the web, $.get and $.ajax only do http(s).

Would something like

javascript:document.location = 'mycustomprotocol://'

override jquery's URL handling? I'm a dumbdumb when it comes to JavaScript, I'm sure this is easily done.. I do believe this is how the entire jQuery mobile framework is implemented (via private URI's).. So, why is there nothing on google or SO about it, huh? Can i get some help from my sister friends?

Alex Gray
  • 16,007
  • 9
  • 96
  • 118
  • Usually the protocol is very meaningful. Like you cannot use the browsers AJAX framework to access and ftp resource. Its not just a matter of replacing the string in the url. If you have custom protocol do you will also need a js "plugin" that can open sockets with that protocol. Once that is available you can add your own jquery plugin to behave like `$.ajax` and `$.get`. – d_inevitable Apr 15 '12 at 23:46

4 Answers4

3

The basic ajax and get methods use the browser's regular http requests. For security reasons, ajax type calls will never work with a custom protocol. If you try to develop a website that isn't on a server and use .ajax, you'll notice it will do nothing. You'll have to start from the ground up and make a custom request handler altogether, not just alter something within jQuery.

runfaj
  • 393
  • 3
  • 17
  • This is sort of true, but you can get around it by adding HTTP headers in your NSURLProtocol response: https://stackoverflow.com/questions/15224071/how-to-mock-ajax-call-with-nsurlprotocol/15234787#15234787 – Jared Updike Oct 28 '16 at 18:49
2

Here is what I did for a callto: protocol, which works in Firefox 24 and IE 10 (I haven't tested any other browsers):

First - your protocol must already be registered or your computer won't recognize it. Second create a hidden link in your markup with your protocol as the href. When I click on a button (#calltobutton), then it sets the value of the hidden link (#clicker) and then and then clicks it, causing the browser to perform the action.

Then do this:

$('#calltobutton').click(function () {
    initCallTo(1234567890);
});


function initCallTo(callto) {
    $('#clicker').attr('href', "callto:" + callto);
    $('#clicker')[0].click();
}

Now, the method of clicking on the callto link is a but strange. You can read more about that in this here thread.

Community
  • 1
  • 1
bgmCoder
  • 6,205
  • 8
  • 58
  • 105
2

This comes a little late, but you can use https://github.com/ded/reqwest to do this:

reqwest('mycustomprotocol://myaction', function(result) {
  // ...
});
Manuel Ebert
  • 8,429
  • 4
  • 40
  • 61
  • tried this from mobile safari against a custom URL scheme and it failed. Typing the url scheme directly or redirecting the browser itself worked however. – stonedauwg Jul 07 '15 at 18:42
1

Only way I could get this to work was to do a browser window redirect using the DOM in JS. Calling from JQuery or even the reqwestJS library mentioned on this page didnt work for me with mobile safari and an app that listens for custom URI schemes.

stonedauwg
  • 1,328
  • 1
  • 14
  • 35