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!