First, you need to encrypt the string using md5. The method for adding the md5 category of NSString is as follows:
. H file
<CommonCrypto/CommonDigest. h>
NSString (md5)
-(*) Md5HexDigest;
@ End
. M file
"NSString + md5.h"
NSString (md5)
-(*) Md5HexDigest
{
* Original_str = [];
CC_MD5_DIGEST_LENGTH
(Original_str, (original_str), result );
NSMutableStringNSMutableString
(I =; I <; I ++)
[Hash:, result [I];
@ "LowercaseString = % @"
LowercaseString
}
@ End
Then there is the conversion algorithm. In fact, it is the same as the java version, but it is implemented using OC.
-(*) Signed url :( *) url
{
* Chars = [[]:,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,];
(, [Chars]);
* Key =;
StringWithFormatstringByAppendingFormat
(, Hex );
NSMutableArrayNSMutableArray
(I =; I <; I ++ ){
// 8 16 0x3FFFFFFF
* STempSubString = [hex :( I *,)];
// Long Inteper 31, long
LongOfTemp;
SscanfcStringUsingEncodingNSASCIIStringEncoding
LHexLong = & longOfTemp;
* OutChars =;
(J =; j <; j ++ ){
// 0x0000003D chars
Index = & lHexLong;
OutChars = [outChars:, [chars :() index];
// 5
LHexLong = lHexLong >>;
}
Store the string to the output array of the corresponding index
[ResUrl: outChars: I];
}
[ResUrl:];
}
Note: The principle of converting a long link to a short link is very simple. It is equivalent to renaming, but your server defines its own rules and has its own key (that is, the key variable defined in the algorithm, you need to replace it with the key defined by your product), and then determine which index element in the resUrl is a recognizable short link. If it is determined to be 0th, 0th will be returned in the algorithm. To use it for your own use, you only need to change the key value.
You must have a server-defined key. If you do not click the link converted successfully, the browser cannot recognize the key. In addition, the returned string is only a string of characters and does not contain domain names or anything, you also need to determine a set of domain name rules by yourself. For example, after the address conversion of Sina is completed at http://t.cn/, you can define a domain name for a specific domain name. For details about how to define this domain name, please be more advanced! 1