Passing url to Three20 TTURLMap

2020-07-27 05:35发布

问题:

I am trying to pass in a URL as a parameter to a TTURLMap like this:

[map from@"tt://person/(initWithURL:)" toViewController: [PersonViewController class]];

I then have a TTStyledTableItemCell with links that render like this:

<a href="tt://person/http://persons.url.com/blah">Person name</a>

but when I click on these links the link doesn't call the initWithURL: method in the PersonViewConroller class. I've verified things are wired up correctly by passing in a simple string. I believe this isn't working because the parser thinks the URL is part of the TTURLMap url mapping. Is there a way to escape the person's feed url (its a rest service that i need to use to pull info in)?

many thanks in advance.

回答1:

I think the only character that TTURLMap trips up on is the / character, so the following did the trick for me. Just call these as Mike suggests on the way in and out of your init function

+ (NSString *)encodeTtUrl:(NSString *)str {
    NSMutableString *temp = [NSMutableString stringWithString:str];
    [temp replaceOccurrencesOfString:@"/" withString:@"__" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
    return [NSString stringWithString:temp];
}

+ (NSString *)decodeTtUrl:(NSString *)str {
    NSMutableString *temp = [NSMutableString stringWithString:str];
    [temp replaceOccurrencesOfString:@"__" withString:@"/" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
    return [NSString stringWithString:temp];
}


回答2:

This is a common problem. I haven't found an elegant solution; the best I've been able to find is to manually escape the URL on the way in (e.g. by Base64-encoding it), and then manually unescape it inside your initWithURL: function. (Actually, you'd probably want to have a function called initWithBase64EncodedURL: or something like that, for clarity.)