priore

Convert URL to a Tiny URL

Apr 23rd, 2015
1,262
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (NSURL*)tinyURL:(NSURL*)originalURL
  2. {
  3.     NSString *origUrl = [originalURL absoluteString];
  4.     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@", [origUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
  5.     NSURLRequest *request = [ NSURLRequest requestWithURL:url
  6.                                               cachePolicy:NSURLRequestReloadIgnoringCacheData
  7.                                           timeoutInterval:10.0];
  8.     NSError *error;
  9.     NSURLResponse *response;
  10.     NSData *myUrlData = [ NSURLConnection sendSynchronousRequest:request
  11.                                                returningResponse:&response
  12.                                                            error:&error];
  13.     NSString *myTinyUrl = [[NSString alloc] initWithData:myUrlData encoding:NSUTF8StringEncoding];
  14.     return [NSURL URLWithString:myTinyUrl];
  15. }
RAW Paste Data