IOS --- Summary of common methods

Source: Internet
Author: User
Tags sha1 sha1 encryption

IOS --- Summary of common methods

In the last two months, I have encountered some amazing problems in developing a static library of my own company, but I have finally solved them. I would like to share with you my solutions.

1. sha1 encryption:

 

+ (NSString *) sha1 :( NSString *) input {

If (! Input | [inputisEqualToString: @]) {

Return @;

}

 

// Const char * cstr = [input cStringUsingEncoding: NSUTF8StringEncoding];

// NSData * data = [NSData dataWithBytes: cstr length: input. length];

// Replace the above two lines of code with the following line of code because the above lines will lose data when encrypting Chinese characters.

NSData * data = [inputdataUsingEncoding: NSUTF8StringEncoding];

 

Uint8_t digest [CC_SHA1_DIGEST_LENGTH];

CC_SHA1 (data. bytes, (unsignedint) data. length, digest );

NSMutableString * output = [NSMutableStringstringWithCapacity: CC_SHA1_DIGEST_LENGTH * 2];

For (int I = 0; I

[Output appendFormat: @ % 02x, digest [I];

}

Return output;

}


2. parse json strings

 

+ (Id) getJsonObjectWithJsonStr :( NSString *) jsonStr {

If (jsonStr = nil ){

Returnnil;

}

 

// The following two lines of code are used to prevent the json string from containing special characters of url encode.

JsonStr = [(NSString *) jsonStrstringByReplacingOccurrencesOfString: @ + withString: @];

 

JsonStr = [jsonStr stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];


NSData * jsonData = [jsonStrdataUsingEncoding: NSUTF8StringEncoding];

NSError * error;

Id object = [NSJSONSerializationJSONObjectWithData: jsonData options: NSJSONReadingMutableContainerserror: & error];

If (error ){

LGLog (@ json parsing failed: % @, error );

Returnnil;

}

Return object;

}


3. Determine whether the string contains Chinese Characters

 

+ (BOOL) IsChinese :( NSString *) str {

For (int I = 0; I <[strlength]; I ++ ){

Int a = [strcharacterAtIndex: I];

If (a> 0x4e00 & a <0x9fff ){

ReturnYES;

}

} ReturnNO;

}


4. Identify the jailbreaking machine because cydia is installed on all jailbreaking devices.

 

+ (BOOL) isJailBreak

{

If ([[UIApplicationsharedApplication] canOpenURL: [NSURLURLWithString: @ cydia: //]) {

NSLog (@ The device is jail broken !);

ReturnYES;

}

NSLog (@ The device is NOT jail broken !);

ReturnNO;

}


5. dynamically calculate the label height based on the text content

After specifying the width of the label, call the [label sizeToFit] method. There are no problems in iPhone 5S, iPhone 6, and iPhone 6 Plus, and there are no other devices available for testing!

 

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.