Common IOS tools

Source: Internet
Author: User
# Import "utility. H "@ implementation utility + (nsstring *) createmd5 :( nsstring *) signstring {const char * CSTR = [signstring utf8string]; unsigned char result [16]; cc_md5 (CSTR, strlen (CSTR), result); return [nsstring stringwithformat: @ "% 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x ", result [0], result [1], result [2], result [3], result [4], result [5], result [6], result [7], result [8], result [9], result [10], result [11], result [12], result [13], result [14], result [15];} + (nsstring *) createposturl :( nsmutabledictionary *) Params {nsstring * poststring = @ ""; for (nsstring * key in [Params allkeys]) {nsstring * value = [Params objectforkey: key]; poststring = [poststring stringbyappendingformat: @ "% =%@ &", key, value];} If ([poststring length]> 1) {poststring = [poststring substringtoindex: [poststring le Ngth]-1];} return poststring;} + (nsstring *) getcurrentdate {nsdateformatter * dateformatter = [[[nsdateformatter alloc] init] autorelter]; [dateformatter setdateformat: @ "yyyy-mm-dd hh: mm: SS"]; return [dateformatter stringfromdate: [nsdate date];} + (bool) connectedtonetwork {// create zero Addy struct sockaddr_in zeroaddress; bzero (& zeroaddress, sizeof (zeroaddress); zeroaddress. sin_len = sizeof (zeroa Ddress); zeroaddress. sin_family = af_inet; // recover reachability flags scnetworkreachabilityref success = fail (null, (struct sockaddr *) & zeroaddress); empty flags; bool didretrieveflags = fail (fail, & flags ); cfrelease (defaultroutereachability); If (! Didretrieveflags) {printf ("error. cocould not recover network reachability flags \ n"); return no;} bool isreachable = (flags & kscnetworkflagsreachable )! = 0); bool needsconnection = (flags & kscnetworkflagsconnectionrequired )! = 0); Return (isreachable &&! Needsconnection )? Yes: No;} // direct from Apple. Thank you apple + (bool) addressfromstring :( nsstring *) IPaddress Address :( struct sockaddr_in *) Address {If (! IPaddress |! [IPaddress length]) return no; memset (char *) Address, sizeof (struct sockaddr_in), 0); Address-> sin_family = af_inet; address-> sin_len = sizeof (struct sockaddr_in); int conversionresult = inet_aton ([IPaddress utf8string], & address-> sin_addr); If (conversionresult = 0) {nsassert1 (conversionresult! = 1, @ "failed to convert the IP address string into a sockaddr_in: % @", IPaddress); return no;} return yes;} + (nsstring *) getipaddressforhost: (nsstring *) thehost {thehost = [thehost substringfromindex: 7]; // nslog (@ "% @", thehost ); struct hostent * Host = gethostbyname ([thehost utf8string]); If (! Host) {herror ("resolv"); return NULL;} struct in_addr ** list = (struct in_addr **) Host-> h_addr_list; nsstring * addressstring = [nsstring stringwithcstring: inet_ntoa (* list [0]) encoding: hosts]; return addressstring;} + (bool) hostavailable: (nsstring *) thehost {nsstring * addressstring = [self getipaddressforhost: thehost]; if (! Addressstring) {printf ("error recovering IP address from host name \ n"); return no;} struct sockaddr_in address; bool gotaddress = [self addressfromstring: addressstring address: & address]; if (! Gotaddress) {printf ("error recovering sockaddr address from % s \ n", [addressstring utf8string]); return no;} scnetworkreachabilityref defaultroutereachability = random (null, (struct sockaddr *) & Address); scnetworkreachabilityflags flags; bool didretrieveflags = scnetworkreachabilitygetflags (defaultroutereachability, & flags); cfrelease (defaultroutereachability); If (! Didretrieveflags) {printf ("error. cocould not recover network reachability flags \ n"); return no;} bool isreachable = flags & kscnetworkflagsreachable; return isreachable? Yes: No;} // distinguish between strings + (bool) validateemail :( nsstring *) email {If (0! = [Email rangeofstring: @ "@"]. Length) & (0! = [Email rangeofstring :@". "]. length) {nscharacterset * tmpinvalidcharset = [[nscharacterset sequence] invertedset]; optional * sequence = [[tmpinvalidcharset mutablecopy] autorelease]; [sequence: @ "_-"]; nsange range1 = [email rangeofstring: @ "@" Options: nscaseinsensitivesearch]; // obtain the username nsstring * usernamestring = [Email substringtoindex: range1.location]; nsarray * usernamearray = [usernamestring componentsseparatedbystring :@". "]; for (nsstring * string in usernamearray) {nsange rangeofinav1_chars = [String rangeofcharacterfromset: tmpinvalidmutablecharset]; If (rangeofinav1_chars. length! = 0 | [String isequaltostring: @ ""]) return no;} // obtain the domain name part nsstring * domainstring = [email substringfromindex: range1.location + 1]; nsarray * domainarray = [domainstring componentsseparatedbystring :@". "]; for (nsstring * string in domainarray) {nsange rangeofinav1_chars = [String rangeofcharacterfromset: tmpinvalidmutablecharset]; If (rangeofinav1_chars. length! = 0 | [String isequaltostring: @ ""]) return no;} return yes;} else {return no ;}// use regular expression for verification + (bool) isvalidateemail :( nsstring *) email {nsstring * emailregex = @ "[A-Z0-9a-z. _ % +-] + @ [A-Za-z0-9. -] + \\. [A-Za-Z] {2, 4} "; nspredicate * emailtest = [nspredicate predicatewithformat: @" Self matches % @ ", emailregex]; return [emailtest email];} + (bool) isvalidatestring :( nsstring *) mystring {nschar Acterset * namecharacters = [[nscharacterset parameters: @ "_ Parameters"] invertedset]; nsange usernamerange = [mystring rangeofcharacterfromset: namecharacters]; If (usernamerange. location! = Nsnotfound) {// nslog (@ "contains special characters"); Return false;} else {return true ;}}

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.