2013-06-06 3 views
0

Je suis en train d'avoir des tuyaux verticaux dans l'URLURL Objectif C avec tubes/barres verticales

Chaîne d'entrée: http://testURL.com/Control?command=dispatch|HOME|ABC:User Nom

-(NSString *)getURLEncodedString:(NSString *)stringvalue{ 

NSMutableString *output = [NSMutableString string]; 
const unsigned char *source = (const unsigned char *)[stringvalue UTF8String]; 
int sourceLen = strlen((const char *)source); 
for (int i = 0; i < sourceLen; ++i) { 
    const unsigned char thisChar = source[i]; 
    if (thisChar == ':' || thisChar == '/' || thisChar == '?' || thisChar == '=' || thisChar == '|' || thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || 
       (thisChar >= 'a' && thisChar <= 'z') || 
       (thisChar >= 'A' && thisChar <= 'Z') || 
       (thisChar >= '0' && thisChar <= '9')) { 
     [output appendFormat:@"%c", thisChar]; 
    } else { 
     [output appendFormat:@"%%%02X", thisChar]; 
    } 
} 
return output; 
} 

Chaîne de sortie après avoir appelé méthode ci-dessus: http://testURL.com/Control?command=dispatch|HOME|ABC:User%20Name

maintenant , si je passe la chaîne de codage ci-dessus à [[NSURL URLWithString: encodedString];

Je reçois Domain = NSURLErrorDomain Code = -1000 "mauvaise URL" UserInfo = {0xae9d760 NSUnderlyingError = 0xaec8ed0 "mauvaise URL", NSLocalizedDescription = mauvaise URL}

Toute entrée sur ce gars-là? Je veux que l'URL ressemble à encodedString.

Merci!

+1

Qu'allez-vous en faire? '|' n'est pas un caractère valide dans une URL, c'est pourquoi il est rejeté en tant que mauvaise URL. – gaige

+0

duplication possible de [symbole de conduite verticale non détecté dans NSURL] (http://stackoverflow.com/questions/6167331/vertical-pipe-symbol-not-being-detected-in-nsurl) – Mobilewits

Répondre

2

Je ne vois vraiment pas une raison pour encoder/échapper à votre chaîne manuellement ... De toute façon, cela fonctionne très bien:

NSString *urlString = @"http://testURL.com/Control?command=dispatch|HOME|ABC:User Name"; 
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

qui délivre en sortie:

http://testURL.com/Control?command=dispatch%7CHOME%7CABC:User%20Name 

Il semble que NSURL n'aime pas les barres verticales après tout, que vous n'avez pas encodées dans votre méthode et obtenant ainsi un code bad URL.

+0

J'ai besoin de barres verticales dans le URL :). Pas% 7C. Désolé, aurait dû le mentionner clairement. – Mobilewits

+0

Ensuite, vous devrez trouver une autre classe pour gérer les URLs parce que NSURL ne peut pas ... – Alladinian

+0

J'ai utilisé le cryptage ISOLatin. Changé à NSUTF8StringEncoding. Ma question est maintenant un doublon. – Mobilewits