views:

30

answers:

1

I need to handle HTTP URLs that begin with a certain domain with my app, because it points to a REST source. The web service however is not under my control, so i can't easily introduce a custom url scheme to open the links with my app.

Is there a way to intercept and handle such URLs in a Cocoa app?

Example: http://static.domain.name/some/random/path

My app should handle all links pointing to http://static.domain.name

I'm afraid that the answer will be NO, but hey, everything is possible somehow :).

Update


This is the Safari extension code i used to make it work (content in <> replaced with your stuff)

var allLinks = document.links;
for (var i=0; i<allLinks.length; i++) {
    var link = allLinks[i];
    if (/.*<match>.*/.test(link.href)) {
        var rewrite = link.href.replace(/<match>/, "<customscheme>://");
        link.href = rewrite;
        console.log("Rewrote: " + rewrite);
    }
}
A: 

Just encode the whole url under your own scheme, e.g. myscheme://http%3A//static.domain.name/some/random/path

When your application handles the URL, just chop off the first part and unescape it:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    NSString * urlString = [url description];
    // Remove your scheme
    urlString = [urlString stringByReplacingOccurrencesOfString:@"myscheme://" withString: @""];
    // Unescape
    urlString = [urlString stringByReplacingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
    // Do something with urlString
    return YES;
}

But no. You can't be assigned to handle http://

Matt Williamson
But how should i accomplish that if the URL is located on a webpage? It does need to work right out of safari as well.
Erik Aigner
I guess i have to write a safari extension then :)
Erik Aigner
Sorry, I put iPhone specific code here. I understood wrong. Can you give a use-case? Is it the user visits some website and when they click on a link to your domain, it opens your cocoa app with the url?
Matt Williamson
No not my domain. Specifically, i need to open Last.fm URLs. For example when you go [here](http://www.last.fm/music/Noisettes/Wild+Young+Hearts) there's a `Play Noisettes Radio` button that links to `http://www.last.fm/listen ...` . I need to get those URLs to my app somehow
Erik Aigner
Sounds like you need a Safari plugin :(
Matt Williamson
You could make the plugin simple though. Just transform the interesting links to your own scheme.
Matt Williamson
Yes I've done that. Took me only an hour to figure it out. It was fairly easy (only very few lines of code) to rewrite all matching links on the page to my URL scheme with a Safari extension.
Erik Aigner