2011-04-19 2 views
0

iPhone에서 UR1을 TinyURL로 프로그래밍 방식으로 변환하려고합니다. 이 작업을 수행하는 방법?URL을 iPhone의 TinyURL로 변환

+0

에서 작동합니다. 이 아이디어를 C#에서 볼 수있는 힌트를 줄 수 있습니다. [See] (http://stackoverflow.com/questions/1116860/whats-the-best-way-to-create-a-short-hash-similiar-to-what- tiny-url-does) –

답변

5

작은 URL 그것은

그냥 그것은 당신의 연결

을 가진 작은 URL을 반환됩니다 URL

http://tinyurl.com/api-create.php?url=http://yourURL.com/

을 가진이 요청을 보내 매우 간단합니다, 당신이 사용할 수있는 간단한 API를 가지고

편집 : 여기에 실제 예가 나와 있습니다. 동기 요청이므로 너무 오래 걸리는 경우 앱이 응답하지 않을 수 있습니다.

NSString *origUrl = @"http://stackoverflow.com"; 
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@", origUrl]]; 
NSURLRequest *request = [ NSURLRequest requestWithURL:url 
             cachePolicy:NSURLRequestReloadIgnoringCacheData 
            timeoutInterval:10.0 ]; 
NSError *error; 
NSURLResponse *response; 
NSData *myUrlData = [ NSURLConnection sendSynchronousRequest:request 
            returningResponse:&response 
               error:&error]; 
NSString *myTinyUrl = [[NSString alloc] initWithData:myUrlData encoding:NSUTF8StringEncoding]; 
//do stuff with url 
[myTinyUrl release]; 
+0

나는 당신의 대답을 좋아하지 만, 서버가 다운되었거나 너무 바빠서 그 시간에 요청을하면 어떻게 될까? 그가 완전히 tinyUrl API에 의존하면 @Warrior 응용 프로그램이 작동하지 않습니다. –

+0

@AhmadTK : 당신처럼, 처음에는 헥터의 링크를 시도하는 데 아주 오래 기다렸지 만 지금은 매우 빨리 반응하고있는 것 같아 일시적인 결함 일 가능성이 큽니다. – Tommy

+0

글쎄, 내 전화에서 게시 할 때 특정 코드 예제를 제공하지 않았다. 타임 아웃이나 다른 오류가 발생했을 때 http 요청에 대한 폴백이 있다고 가정합니다. – Hector204

0

방법을 쉽게하고 문자열이 작게 일부 해시 함수를 필요 iOS7에

NSError *error 
NSString *tinyURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@", YOUR-URL]] 
                encoding:NSASCIIStringEncoding error:&error]; 

// error handling here..