iOS 開發小技巧-持續更新~

1.修改xib創建視圖的圓角
layer.cornerRadius
layer.vorderWidth
這裏寫圖片描述
2.cell分割線不能到達屏幕左邊距

- (void)viewDidLoad {
    [super viewDidLoad];
    //解決不能cell分割線不能到達左邊距
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
  [self.tableView setSeparatorInset:UIEdgeInsetsZero];
   }
  if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
   [self.tableView setLayoutMargins:UIEdgeInsetsZero];
   }
   }
   //在willDisplayCell中,添加代碼
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

3阻止文件被iTunes和iCloud同步原文

#import “sys/xattr.h”
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = “com.apple.MobileBackup”;
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
}

- (void)addSkipBackupAttributeToPath:(NSString*)path {
u_int8_t b = 1;
setxattr([path fileSystemRepresentation], “com.apple.MobileBackup”, &b, 1, 0, 0);
}
兩種方法任選其一

4.刪除視圖上的所有子視圖

[XXXX.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

5.在代碼塊中添加“<##>”,使自己想要輸入的地方,變成可輸入狀態

 [MobClick event:@"XXXXX"];
 //將@“xxxxx”用<##>代替變成
  [MobClick event: ];

6.APP申請加急審覈這裏寫鏈接內容https://developer.apple.com/contact/app-store/?topic=expedite
7.使用Reveal連接模擬器調試界面
給出瞭如何不用修改Xcode工程就可以加載使用Reveal的方法。

在當前用戶目錄新建一個文件.lldbinit,位於~/.lldbinit,LLDB每次啓動的時候都會加載這個文件。

在.lldbinit中輸入如下內容:

command alias reveal_load_sim expr (void*)dlopen("/Applications/Reveal.app/Contents/SharedSupport/iOS-Libraries/libReveal.dylib", 0x2);

command alias reveal_load_dev expr (void*)dlopen([(NSString*)[(NSBundle*)[NSBundle mainBundle] pathForResource:@"libReveal" ofType:@"dylib"] cStringUsingEncoding:0x4], 0x2);

command alias reveal_start expr (void)[(NSNotificationCenter*)[NSNotificationCenter defaultCenter] postNotificationName:@"IBARevealRequestStart" object:nil];

command alias reveal_stop expr (void)[(NSNotificationCenter*)[NSNotificationCenter defaultCenter] postNotificationName:@"IBARevealRequestStop" object:nil];

上述文件創建了4個命令:
reveal_load_sim,reveal_load_dev, reveal_start 和 reveal_stop
reveal_load_sim 這個只在iOS模擬器上有效。它從Reveal的應用程序bundle中找到並加載libReveal.dylib(請確保你把Reveal安裝到了系統的Application文件夾,如果你換地方了,你修改上述的文件)。
reveal_load_dev 這個命令在iOS設備和模擬器上都有效。不過,它需要你在Build Phase中的的Copy Bundle Resources中加上libReveal.dylib,請確保沒有放到Link Binary With Libraries這個地方。
reveal_start 這個命令發出一個通知啓動Reveal Server。
reveal_stop 這個命令發出一個通知停止Reveal Server。
請注意:只有在iOS應用發出了UIApplicationDidFinishLaunchingNotification通知之後,比如應用的delegate已經處理過application::didFinishLaunchingWithOptions:之後才能調用上面的reveal_load_*命令,然後再調用reveal_start

在設備起來之後,你就可以斷下應用,
這裏寫圖片描述
7.啓動界面加載廣告,需要SDWebImage,

#import "AppDelegate.h"
#import "UIImageView+WebCache.h"
@interface AppDelegate ()
@property (strong, nonatomic) UIView *lunchView;
@end

@implementation AppDelegate
@synthesize lunchView;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [self.window makeKeyAndVisible];

    lunchView = [[NSBundle mainBundle ]loadNibNamed:@"LaunchScreen" owner:nil options:nil][0];
    lunchView.frame = CGRectMake(0, 0, self.window.screen.bounds.size.width, self.window.screen.bounds.size.height);
    [self.window addSubview:lunchView];

    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320, 300)];
    NSString *str = @"http://www.jerehedu.com/images/temp/logo.gif";
    [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"default1.jpg"]];
    [lunchView addSubview:imageV];

    [self.window bringSubviewToFront:lunchView];

    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(removeLun) userInfo:nil repeats:NO];

    return YES;
}

-(void)removeLun
{
    [lunchView removeFromSuperview];
}

8.使用SDWebImage,清除圖片緩存的

1.找到SDImageCache類
2.添加如下方法:

- (float)checkTmpSize {
    float totalSize = 0;
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:diskCachePath];
    for (NSString *fileName in fileEnumerator) {
       NSString *filePath = [diskCachePath stringByAppendingPathComponent:fileName];
       NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
       unsigned long long length = [attrs fileSize];
      totalSize += length / 1024.0 / 1024.0;
    } 
     return totalSize;
}
//在設置中使用
- (IBAction)cleanButton:(id)sender {
    NSLog(@"清除緩存");
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDWebImageManager sharedManager].imageCache clearMemory];
    [[SDWebImageManager sharedManager].imageCache clearDisk];
    [tableView reloadData];
    //計算清除後的緩存大小
    float tmpSize1 = [[SDImageCache sharedImageCache] checkTmpSize];
    self.cacheNamber.text = [NSString stringWithFormat:@"%.1fM",tmpSize1];
}

方法二,方法二比方法1好用
//清除緩存
- (void)sheZhi
{
    // 獲取Caches 目錄路徑
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    //浮點 獲取folderSizeAtPath 方法裏傳來的值
    CGFloat fileSize = [self folderSizeAtPath:cachPath];

    // GCD   線程管理   不多說,
    dispatch_async(dispatch_get_main_queue(), ^{
        sd =[NSString stringWithFormat:@"%.2fMB",fileSize];
        NSLog(@"%@",sd);
        self.cacheNamber.text = sd;
    });

}

- (CGFloat)folderSizeAtPath:(NSString *)folderPath
{
    //  獲取緩存大小,
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) {
        return 0;
    }

    // NSEnumerator (枚舉) NSEnumerator用來描述這種集合迭代運算的方式, 通過objectEnumerator向數組請求枚舉器,如果想從後向前瀏覽集合,可使用reverseObjectEnumerator方法。
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];

    NSString *fileName = nil;
    long long folderSize = 0;
    //在獲得枚舉器後,可以開始一個while循環,每次循環都向這個枚舉器請求它的下一個對象:nextObject。nextObject返回nil值時,循環結束。
    while ((fileName = [childFilesEnumerator nextObject]) != nil) {
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    // 因爲得到的數據時bate ,所以轉換成mb
    return folderSize/(1024.0*1024.0);
}

- (long long)fileSizeAtPath:(NSString *)filePath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;

}
//清除緩存
-(void)myClearCacheAction{
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       // GCD 獲取文件地址,文件裏的數據個數
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       NSLog(@"%@",cachPath);
                       NSLog(@"files :%lu",(unsigned long)[files count]);
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                           //                           NSString * cachePath =@"qwe";
                           CGFloat fileSize = [self folderSizeAtPath:cachPath];

                           dispatch_async(dispatch_get_main_queue(), ^{
                               NSString *sdd  = [NSString stringWithFormat:@"%.2fMB",fileSize];
                               NSLog(@"%@",sdd );
                               self.cacheNamber.text = sdd;
                           });
                       }
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
}


-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
}

9.cocoapods引入框架,頭文件不能聯想的問題
**選擇Target -> Build Settings 菜單,找到\”User Header Search Paths\”設置項
新增一個值”${SRCROOT}”,並且選擇\”Recursive\”,這樣xcode就會在項目目錄中遞歸搜索文件**
10.導航欄透明,iOS7以上,需要一張透明照片

    UIImage *image = [UIImage imageNamed:@"bj.jpg"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:image];

11.獲取當前的版本號

代碼實現獲得應用的Verison號:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
或
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

獲得build號:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章