iOS 中的一些常用函数

1.获取 本地语言

+ (NSString *)getLocalLanguage

{

    NSString *language = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];

    return language;

}

2.获取mac 地址

// returns the local MAC address.

+ (NSString*) macAddress:(NSString*)interfaceNameOrNil

{

    // uses en0 as the default interface name

    NSString* interfaceName = interfaceNameOrNil;

    if (interfaceName == nil)

    {

        interfaceName = @"en0";

    }

    

    int                 mib[6];

    size_t              len;

    char                *buf;

    unsigned char       *ptr;

    struct if_msghdr    *ifm;

    struct sockaddr_dl  *sdl;

    

    mib[0] = CTL_NET;

    mib[1] = AF_ROUTE;

    mib[2] = 0;

    mib[3] = AF_LINK;

    mib[4] = NET_RT_IFLIST;

    

    if ((mib[5] = if_nametoindex([interfaceName UTF8String])) == 0)

    {

        printf("Error: if_nametoindex error\n");

        return NULL;

    }

    

    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)

    {

        printf("Error: sysctl, take 1\n");

        return NULL;

    }

    

    if ((buf = malloc(len)) == NULL)

    {

        printf("Could not allocate memory. error!\n");

        return NULL;

    }

    

    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)

    {

        printf("Error: sysctl, take 2");

        free(buf);

        return NULL;

    }

    

    ifm = (struct if_msghdr*) buf;

    sdl = (struct sockaddr_dl*) (ifm + 1);

    ptr = (unsigned char*) LLADDR(sdl);

    NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",

                           *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];

    free(buf);

    

    return outstring;

}

3.判断是否 越狱

static const char * __jb_app = NULL;



+ (BOOL)isJailBroken

{

    static const char * __jb_apps[] =

    {

        "/Application/Cydia.app", 

        "/Application/limera1n.app", 

        "/Application/greenpois0n.app", 

        "/Application/blackra1n.app",

        "/Application/blacksn0w.app",

        "/Application/redsn0w.app",

        NULL

    };



    __jb_app = NULL;



    // method 1

    for ( int i = 0; __jb_apps[i]; ++i )

    {

        if ( [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:__jb_apps[i]]] )

        {

            __jb_app = __jb_apps[i];

            return YES;

        }

    }

    

    // method 2

    if ( [[NSFileManager defaultManager] fileExistsAtPath:@"/private/var/lib/apt/"] )

    {

        return YES;

    }

    

    // method 3

    if ( 0 == system("ls") )

    {

        return YES;

    }

    

    return NO;    

}



+ (NSString *)jailBreaker

{

    if ( __jb_app )

    {

        return [NSString stringWithUTF8String:__jb_app];

    }

    else

    {

        return @"";

    }

}

 

你可能感兴趣的:(常用函数)