{"id":2115,"date":"2010-02-10T20:57:07","date_gmt":"2010-02-10T19:57:07","guid":{"rendered":"http:\/\/www.devapp.it\/wordpress\/?p=2115"},"modified":"2014-02-04T13:23:43","modified_gmt":"2014-02-04T12:23:43","slug":"tips001-ridurre-la-dimensione-degli-url-sfruttando-tr-im","status":"publish","type":"post","link":"https:\/\/www.devapp.it\/wordpress\/tips001-ridurre-la-dimensione-degli-url-sfruttando-tr-im\/","title":{"rendered":"TIPS#001 &#8211; Ridurre la dimensione degli URL sfruttando tr.im"},"content":{"rendered":"<p><a href=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2010\/02\/logo_trim.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2010\/02\/logo_trim.png\" alt=\"Logo tr.im\" title=\"logo_trim\" width=\"150\" height=\"83\" class=\"alignleft size-full wp-image-2117\" \/><\/a> Se avete la necessit\u00e0 di ridurre via codice un URL, da utilizzare nella vostra applicazione, come capita spesso agli utenti del noto Social Network Twitter, che hanno a disposizione massimo 140 caratteri per un singolo tweet, in cui spesso inseriscono un link, non dovrete far altro che sfruttare il metodo <em>stringWithContentsOfURL<\/em> dell&#8217;oggetto NSString come mostrato da <strong>brandontreb<\/strong> di <a href=\"http:\/\/icodeblog.com\/2010\/02\/04\/iphone-coding-recipe-shortening-urls\/\" target=\"_blank\">iCodeBlog<\/a>, riportato qui in basso.<!--more--><\/p>\n<p>In questo Tips viene usato il servizio <em>trim_simple<\/em> offerto da <a href=\"http:\/\/tr.im\/\" target=\"_blank\">tr.im<\/a>, in quanto questo restituisce direttamente una stringa con l&#8217;URL accorciato. Avremmo potuto lavorare anche con altri servizi simili presenti in rete, tipo <a href=\"http:\/\/bit.ly\/\" target=\"_blank\">bit.ly<\/a>, ma quest&#8217;ultimo, ad esempio, restituisce una stringa JSON che avrebbe richiesto il parsing complicando sicuramente il tutto. <\/p>\n<p>Di seguito il codice:<\/p>\n<pre lang=\"objc\" escaped=\"true\">\r\nNSString *url    = @\"http:\/\/brandontreb.com\";\r\nNSString *apiEndpoint = [NSString stringWithFormat:@\"http:\/\/api.tr.im\/v1\/trim_simple?url=%@\",url];\r\nNSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]\r\nencoding:NSASCIIStringEncoding\r\nerror:nil];\r\nNSLog(@\"Long: %@ - Short: %@\",url,shortURL);\r\n\/\/ Outputs Long: http:\/\/brandontreb.com - Short: http:\/\/tr.im\/MRDd\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Se avete la necessit\u00e0 di ridurre via codice un URL, da utilizzare nella vostra applicazione, come capita&#8230;<\/p>\n","protected":false},"author":1,"featured_media":10898,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[105],"tags":[109,106,110,107,108],"class_list":["post-2115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-tricks","tag-stringwithcontentsofurl","tag-tips","tag-tr-im","tag-tricks","tag-url"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/posts\/2115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/comments?post=2115"}],"version-history":[{"count":6,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/posts\/2115\/revisions"}],"predecessor-version":[{"id":2123,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/posts\/2115\/revisions\/2123"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/media\/10898"}],"wp:attachment":[{"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/media?parent=2115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/categories?post=2115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/tags?post=2115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}