简体   繁体   中英

iOS deep linking: how to embed URL in another URL?

I need to deep link to my app with another URL, say if my custom scheme is:

myapp://

then the deep linking URL looks like:

myapp://?type=url&url=[another URL]

The problem is that the "another URL" may contain the separators like ?, /, & etc, I thought about using quotation mark:

myapp://?type=url&url="http://another.url"

However the another URL may also have quotation mark in it, so is there a way to do it?

Thanks

You need to properly escape all of the special characters in the 2nd URL. I use a utility method I wrote to help:

+ (NSString *)encodeString:(NSString *)string {
    NSString *result = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR("% '\"?=&+<>;:-#\\/~`!"), kCFStringEncodingUTF8);

    return result;
}

With that in place (in some helper class or added to some category), you can do the following:

NSString *link = @"http://another.url";
NSString *escapedLink = [SomeUtility encodeString:link];

NSString *myURL = [NSString stringWithFormat:@"myapp://?type=url&url=%@", escapedLink];

Note that this encodeString: method should be used to properly escape any URL parameter value you may ever use to build the query string of a URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM