SoFunction
Updated on 2025-04-12

Conversion between long and short links in IOS

Conversion between long and short links in IOS

First, you need to encrypt the string using md5. The method of adding the md5 category of NSString is as follows

.h file

#import <CommonCrypto/>
 
@interface NSString (md5)
 
-(NSString *) md5HexDigest;
 
@end

.m file

#import "NSString+"
 
@implementation NSString (md5)
 
- (NSString *) md5HexDigest
{
  const char *original_str = [self UTF8String];
  unsignedchar result[CC_MD5_DIGEST_LENGTH];
  CC_MD5(original_str, strlen(original_str), result);
  NSMutableString *hash = [NSMutableStringstring];
  for (int i = 0; i < 16; i++)
    [hash appendFormat:@"%02X", result[i]];
  NSLog(@"lowercaseString = %@",[hash lowercaseString]);
  return [hash lowercaseString];
}
 
@end

Then there is the conversion algorithm, which is actually the same as the Java version, but it is implemented using OC.

- (NSString *)shortUrl:(NSString *)url
{
  NSArray *chars = [[NSArray alloc] initWithObjects:@"a" , @"b" , @"c" , @"d" , @"e" , @"f" , @"g" , @"h" ,
           @"i" , @"j" , @"k" , @"l" , @"m" , @"n" , @"o" , @"p" , @"q" , @"r" , @"s" , @"t" ,
           @"u" , @"v" , @"w" , @"x" , @"y" , @"z" , @"0" , @"1" , @"2" , @"3" , @"4" , @"5" ,
           @"6" , @"7" , @"8" , @"9" , @"A" , @"B" , @"C" , @"D" , @"E" , @"F" , @"G" , @"H" ,
           @"I" , @"J" , @"K" , @"L" , @"M" , @"N" , @"O" , @"P" , @"Q" , @"R" , @"S" , @"T" ,
           @"U" , @"V" , @"W" , @"X" , @"Y" , @"Z", nil];
  NSLog(@"chars = %d", [chars count]);
  NSString *key = @"xxxxxx";
  NSString *hex = [NSStringstringWithFormat:@"%@",[[key stringByAppendingFormat:@"%@",url] md5HexDigest]];
 
  NSLog(@"hex = %@", hex);
  NSMutableArray *resUrl = [[NSMutableArrayalloc] initWithCapacity:4];
  for (int i=0; i&lt;4; i++) {
    // Bit and perform bit and operation of encrypted characters in 8-bit sets of hexadecimal and 0x3FFFFFFF    NSString *sTempSubString = [hex substringWithRange:NSMakeRange(i*8, 8)];
    
    // Here you need to use the long type to convert, because Inteper can only handle 31 bits, and the first bit is the sign bit. If you don't use long, it will cross the boundary    long longOfTemp;
    sscanf([sTempSubString cStringUsingEncoding:NSASCIIStringEncoding], "%lx", &amp;longOfTemp);
    long lHexLong = 0x3FFFFFFF &amp; longOfTemp;
    NSString *outChars = @"";
    for (int j=0; j&lt;6; j++) {
      // Perform bit and calculation of the obtained value with 0x0000003D to obtain the character array chars index      long index = 0x0000003D &amp; lHexLong;
      // Add the obtained characters      outChars = [outChars stringByAppendingFormat:@"%@",[chars objectAtIndex:(int)index]];
      // Each cycle is bit-wise and shifts right by 5 bits      lHexLong = lHexLong &gt;&gt; 5;
    }
    //Save the string into the output array of the corresponding index    [resUrl insertObject:outChars atIndex:i];
  }
  return [resUrl objectAtIndex:0];// Here you can return any element as a short link (0, 1, 2, 3)}
 

Tip: The principle of converting a short link to a long link is very simple, which is equivalent to renaming. It is just that your own server defines its own rules and has its own key (that is, the key variable defined in the algorithm, which needs to be replaced with the key defined by your own supplies), and then determine which index element in resUrl is used as a recognized short link. If it is determined as the 0th, then the 0th one must be returned in the algorithm. If you want to use it yourself, you just need to change the key value.

There must be a server to define the key. If you click the link after the conversion is successful, the browser will not recognize it. In addition, the string returned here is just a string of characters and does not contain a domain name or something. You also need to determine a set of domain name rules yourself. For example, after the address conversion of Sina is successfully started with /, then you need to define a similar domain name. I won’t explain in detail how to define the domain name!

Thank you for reading, I hope it can help you. Thank you for your support for this site!