Très court extrait: obtenir la sortie d'une URL en Objective-C

Peut-être que je devrais écrire "Très très très, courts extraits», cependant, est une excellente et pratique "truc" pour montrer ce que je suis. L'exécution de la "deux" lignes de code ci-dessous, vous pouvez obtenir la sortie d'une URL et de le manipuler.

1
2
3
4
5
url = [ NSString stringWithFormat : @ "http://www.appleside.it/" ] ; NSString * url = [ NSString stringWithFormat: @ "http://www.appleside.it/"];
result = [ NSString stringWithContentsOfURL : [ NSURL URLWithString : url ] NSString * result = [ NSString stringWithContentsOfURL: [ NSURL URLWithString: url]
encodage: NSASCIIStringEncoding
] ; erreur: nil];
"Indirizzo: %@ = %@" , url, result ) ; NSLog (@ "Adresse:% @ =% @", url, résultat);

Par exemple nous pourrions profiter de nombreux services pour résoudre divers problèmes API REST. Juste pour vous donner un exemple concret, imaginons volre "compresser" (trim) le http://www.undolog.com url, comme de nombreux services comme Twitter, FriendFeed ou Facebook. Eh bien nous pourrions utiliser ce code simple:

1
2
3
4
5
6
url = @ "http://www.undolog.com" ; NSString * url = @ "http://www.undolog.com";
apiRest = [ NSString stringWithFormat : @ "http://api.tr.im/v1/trim_simple?url=%@" ,url ] ; NSString * apiRest = [ NSString stringWithFormat: @ "% @ http://api.tr.im/v1/trim_simple?url =" url];
result = [ NSString stringWithContentsOfURL : [ NSURL URLWithString : apiRest ] NSString * result = [ NSString stringWithContentsOfURL: [ NSURL URLWithString: apiRest]
encodage: NSASCIIStringEncoding
] ; erreur: nil];
"URL Originale: %@ - URL Compresso: %@" ,url, result ) ; NSLog (@ "URL d'origine: @% - URL comprimé:% @", url, résultat);

Tout le crédit, bien sûr, est la méthode stringWithContentsOfURL : car il vous permet de charger le contenu dans une chaîne (de sortie) d'une URL.

Il n'y a aucun commentaire pour cet article

Laisser un commentaire

XHTML TAG PERMIS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> CODE D'INSERTION:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL