문제

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);
    }
}
도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top