2010-03-28 6 views
18

Je formaté ai le code suivant pour ouvrir google maps:UIApplication openURL ne fonctionne pas avec NSString

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@, Anchorage, AK",addressString]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; 

Mais ça ne marche pas et il n'y a pas d'erreur. Ça ne s'ouvre tout simplement pas.

Répondre

44

URLWithString requiert une chaîne à pourcentage d'échappement. Votre exemple d'URL contient des espaces qui entraînent la création d'un NSURL nul. De plus, addressString peut également contenir des caractères qui doivent être échappés. Essayez-pour cent d'échapper à la chaîne url première:

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@, Anchorage, AK",addressString]; 
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]]; 
5

besoin d'échapper à la urlString, autre [NSURL URLWithString: urlString] retournera Nill.

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@, Anchorage, AK",addressString]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ]];