Patel Manthan
Patel Manthan

Reputation: 687

Shorten url using bit.ly

I have tried to Shorten urls using bit.ly. When i try to pass a static link it gives me a shorten url but when i try to pass a variable link it doesn't. here is my code....

Bitlyzer *bitlyzer = [[Bitlyzer alloc] initWithDelegate:self];

[bitlyzer shortURL:string];


[bitlyzer shortURL:@"http://www.google.com"];

When i pass this url it gives me a Shorten url but when i pass a variable string as shown above it doesn't give me shorten url.

Please give me your suggetions...

Upvotes: 2

Views: 278

Answers (1)

Paras Joshi
Paras Joshi

Reputation: 20551

Some time in our string some space is remain and so bitly not convert it and return null value so first remove the null or space from string and then try to convert it..

Add my these two methods in your .m file and then use with your variable.. see the example also how to use it...

-(NSString*) trimString:(NSString *)theString {

     NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
     return theStringTrimmed;
}

-(NSString *) removeNull:(NSString *) string {    

     NSRange range = [string rangeOfString:@"null"];
    //NSLog(@"in removeNull : %d  >>>> %@",range.length, string);
    if (range.length > 0 || string == nil) {
           string = @"";
    }
    string = [self trimString:string];
   return string;
}

And use this like bellow...

    string = [self removeNull:string];
    [string retain];

    Bitlyzer *bitlyzer = [[Bitlyzer alloc] initWithDelegate:self];
    [bitlyzer shortURL:string];

Upvotes: 3

Related Questions