Convert a persistent link to a short link (iOS version)

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.