Come faccio a codificare “e” in un URL in un attributo HTML valore?
-
06-09-2019 - |
Domanda
Mi piacerebbe fare un URL clic grado in app e-mail. Il problema è che un URL con parametri rompe questo a causa della "&" nell'URL. La variabile corpo sotto la linea problema. Entrambe le versioni di "corpo" non sono corretti. Una volta che l'applicazione e-mail si apre, il testo si ferma a "... link:". Ciò che è necessario per codificare la e commerciale?
NSString *subject = @"This is a test";
NSString *encodedSubject =
[subject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000¶m=0'>click me</a>"; //original
NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&param=0'>click me</a>"; //have also tried &
NSString *encodedBody =
[body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *formattedURL = [NSString stringWithFormat: @"mailto:myname@somedomain.com?subject=%@&body=%@", encodedSubject, encodedBody];
NSURL *url = [[NSURL alloc] initWithString:formattedURL];
[[UIApplication sharedApplication] openURL:url];
Soluzione
la e commerciale sarebbe% 26 per HEX in standard di codifica URL
Altri suggerimenti
ho usato -[NSString gtm_stringByEscapingForURLArgument]
, che viene fornito in Google Toolbox per Mac , in particolare in GTMNSString + URLArguments.h e GTMNSString + URLArguments.m .
È possibile utilizzare una rappresentazione esadecimale del personaggio, in questo caso% 26.
si può semplicemente utilizzare CFURLCreateStringByAddingPercentEscapes
con CFBridgingRelease
per ARC sostegno
NSString *subject = @"This is a test";
// Encode all the reserved characters, per RFC 3986
// (<http://www.ietf.org/rfc/rfc3986.txt>)
NSString *encodedSubject =
(NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)subject,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
Si utilizza stringByAddingPercentEscapesUsingEncoding
, esattamente come si sta facendo.
Il problema è che non si utilizza abbastanza. Il formato in cui si sta inserendo il corpo codificato anche ha una e commerciale, che non avete codificato. Tack la stringa codificata su di esso invece, e codificarli (utilizzando stringByAddingPercentEscapesUsingEncoding
) insieme.
<a href='http://somewhere.com/two.woa/wa?id=000&param=0'>click me</a>
Is correct, although ‘&’ is more commonly used than ‘&’ or ‘,’.
If the ‘stringByAddingPercentEscapesUsingEncoding’ method does what it says on the tin, it should work(*), but the NSString documentation looks a bit unclear on which characters exactly are escaped. Check what you are ending up with, the URL should be something like:
mailto:bob@example.com?subject=test&body=Link%3A%3Ca%20href%3D%22http%3A//example.com/script%3Fp1%3Da%26amp%3Bp2%3Db%22%3Elink%3C/a%3E
(*: modulo the usual disclaimer that mailto: link parameters like ‘subject’ and ‘body’ are non-standard, will fail in many situations, and should generally be avoided.)
Once the email app opens, text stops at "...link:".
If ‘stringByAddingPercentEscapesUsingEncoding’ is not escaping ‘<’ to ‘%3C’, that could be the problem. Otherwise, it might not be anything to do with escapes, but a deliberate mailer-level restriction to disallow ‘<’. As previously mentioned, ?body=... is not a reliable feature.
In any case, you shouldn't expect the mailer to recognise the HTML and try to send an HTML mail; very few will do that.
Example of use of %26 instead of & without this attributes arrived in PHP as an array!
var urlb='/tools/lister.php?type=101%26ID='+ID; // %26 instead of &
window.location.href=urlb;