Posts Tagged ‘ NSURL

URLWithString retourne nil

En cherchant à créer un NSURL à partir d’un fichier local, j’ai utilisé la méthode URLWithString de NSURL comme ceci :

NSURL *baseURL = [NSURL URLWithString:url];

URLWithString retournait nil… Le problème venait du chemin local qui contenait un espace, ce chemin était du type :

/Library/Application Support/iPhone Simulator/4.0.2/Applications/516C..

L’espace entre iPhone et Simulator n’était pas apprécié par URLWithString. Pour contourner le problème, il suffit de supprimer du chemin tous les caractères non-ASCII :

NSURL *baseURL = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Source : http://stackoverflow.com/questions/1981390/urlwithstring-returns-nil