分享

IOS 基本常用设置

 LoveSeasonLee 2017-06-27
1、禁止手机睡眠

[UIApplication sharedApplication].idleTimerDisabled = YES;

2、动画强制退出程序

   [UIView beginAnimations:@"exitApplication" context:nil];


    [UIView setAnimationDuration:0.5];


    [UIView setAnimationDelegate:self];

    

    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:[AppDelegate AppDelegate].window cache:NO];

    

    [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];


    [AppDelegate AppDelegate].window.bounds = CGRectMake(0, 0, 0, 0);

    

    [UIView commitAnimations];

///退出程序方法

- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

    if ([animationID compare:@"exitApplication"] == 0) {

        exit(0);

    }

}

3、去除数组中重复的对象

NSArray *newArr = [oldArr valueForKeyPath:@"@distinctUnionOfObjects.self"];


4、跳进app权限设置

// 跳进app设置

            if (UIApplicationOpenSettingsURLString != NULL) {

                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

                [[UIApplication sharedApplication] openURL:url];

            }

        }


5、给一个view截图

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

- (UIImage*)snapshotScreen  {

    UIView * view = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];

    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}


6、设置navigationBar上的title颜色和大小

    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor youColor], NSFontAttributeName : [UIFont systemFontOfSize:15]}]

7、view设置圆角

#define ViewBorderRadius(View, Radius, Width, Color)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES];\

[View.layer setBorderWidth:(Width)];\

[View.layer setBorderColor:[Color CGColor]] // view圆角


8、弱/强引用

#define WeakSelf(type)  __weak typeof(type) weak##type = type; // weak

#define StrongSelf(type)  __strong typeof(type) type = weak##type; // strong

9、长按复制功能

- (void)viewDidLoad

{

    [self.view addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pasteBoard:)]];

}

- (void)pasteBoard:(UILongPressGestureRecognizer *)longPress {

    if (longPress.state == UIGestureRecognizerStateBegan) {

        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

        pasteboard.string = @"需要复制的文本";

    }

}


10、判断图片类型

//通过图片Data数据第一个字节 来获取图片扩展名

- (NSString *)contentTypeForImageData:(NSData *)data{

    uint8_t c;

    [data getBytes:&c length:1];

    switch (c) {

        case 0xFF:

            return @"jpeg";

        case 0x89:

            return @"png";


        case 0x47:

            return @"gif";

        case 0x49:

        case 0x4D:

            return @"tiff";

        case 0x52:

        if ([data length] < 12) {

            return nil;

        }

        NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];

        if ([testString hasPrefix:@"RIFF"]

            && [testString hasSuffix:@"WEBP"]) {

            return @"webp";

        }

        return nil;

    }

    return nil;

}

11、获取手机和app信息

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  

 CFShow(infoDictionary);  

// app名称  

 NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];  

 // app版本  

 NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];  

 // app build版本  

 NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];  




    //手机序列号  

    NSString* identifierNumber = [[UIDevice currentDevice] uniqueIdentifier];  

    NSLog(@"手机序列号: %@",identifierNumber);  

    //手机别名: 用户定义的名称  

    NSString* userPhoneName = [[UIDevice currentDevice] name];  

    NSLog(@"手机别名: %@", userPhoneName);  

    //设备名称  

    NSString* deviceName = [[UIDevice currentDevice] systemName];  

    NSLog(@"设备名称: %@",deviceName );  

    //手机系统版本  

    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];  

    NSLog(@"手机系统版本: %@", phoneVersion);  

    //手机型号  

    NSString* phoneModel = [[UIDevice currentDevice] model];  

    NSLog(@"手机型号: %@",phoneModel );  

    //地方型号  (国际化区域名称)  

    NSString* localPhoneModel = [[UIDevice currentDevice] localizedModel];  

    NSLog(@"国际化区域名称: %@",localPhoneModel );  


    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  

    // 当前应用名称  

    NSString *appCurName = [infoDictionary objectForKey:@"CFBundleDisplayName"];  

    NSLog(@"当前应用名称:%@",appCurName);  

    // 当前应用软件版本  比如:1.0.1  

    NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];  

    NSLog(@"当前应用软件版本:%@",appCurVersion);  

    // 当前应用版本号码   int类型  

    NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"];  

    NSLog(@"当前应用版本号码:%@",appCurVersionNum);

12、拿到当前正在显示的控制器,不管是push进去的,还是present进去的都能拿到


- (UIViewController *)getVisibleViewControllerFrom:(UIViewController*)vc {

    if ([vc isKindOfClass:[UINavigationController class]]) {

        return [self getVisibleViewControllerFrom:[((UINavigationController*) vc) visibleViewController]];

    }else if ([vc isKindOfClass:[UITabBarController class]]){

        return [self getVisibleViewControllerFrom:[((UITabBarController*) vc) selectedViewController]];

    } else {

        if (vc.presentedViewController) {

            return [self getVisibleViewControllerFrom:vc.presentedViewController];

        } else {

            return vc;

        }

    }

}














    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约