user187676
user187676

Reputation:

Cocoa app handling a standard HTTP url scheme

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);
    }
}

Upvotes: 0

Views: 1882

Answers (1)

Matt Williamson
Matt Williamson

Reputation: 40193

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://

Upvotes: 1

Related Questions