iOS系统API笔记


实时搜索发送请求

防止输入文字过快频繁请求服务器,也可以用RAC 的throttle节流器控制

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(requestSearch:) object:nil];
    [self performSelector:@selector(requestSearch:) withObject:searchText afterDelay:0.3];
    
}

实时监听textField输入中文内容的时候拼音,拼音不跟着响应

- (void)textFieldDidChange:(UITextField *)textField{
    if (textField.markedTextRange == nil) {
        NSLog(@"text:%@", textField.text);
    }
}

运行时加载类名

// 获取所有加载的Objective-C框架和动态库的名称
const char ** objc_copyImageNames ( unsigned int *outCount );

// 获取指定类所在动态库
const char * class_getImageName ( Class cls );

// 获取指定库或框架中所有类的类名
const char ** objc_copyClassNamesForImage ( const char *image, unsigned int *outCount );

NSLog(@"获取指定类所在动态库");

NSLog(@"UIView's Framework: %s", class_getImageName(NSClassFromString(@"UIView")));

NSLog(@"获取指定库或框架中所有类的类名");
const char ** classes = objc_copyClassNamesForImage(class_getImageName(NSClassFromString(@"UIView")), &outCount);
for (int i = 0; i < outCount; i++) {
    NSLog(@"class name: %s", classes[i]);
}

unsigned int count;
const char **classes;
Dl_info info;

查找本工程里所有vc

dladdr(&_mh_execute_header, &info);
classes = objc_copyClassNamesForImage(info.dli_fname, &count);

NSMutableArray *vcs = [NSMutableArray new];
for (int i = 0; i < count; i++) {
    NSLog(@"Class name: %s", classes[i]);
    NSString *className = [NSString stringWithCString:classes[i] encoding:NSUTF8StringEncoding];
    Class class = NSClassFromString (className);
    if ([class isSubclassOfClass:[UIViewController class]]) {
        [vcs addObject:className];
    }
}

注册 RunLoop监听事件

注册 RunLoopObserver 可以观测当前 RunLoop 的运行状态,并在状态机切换时收到通知:

  1. RunLoop开始
  2. RunLoop即将处理Timer
  3. RunLoop即将处理Source
  4. RunLoop即将进入休眠状态
  5. RunLoop即将从休眠状态被事件唤醒
  6. RunLoop退出

当这一次 RunLoop 迭代处理完成了所有事件,马上要休眠时
使用 CF 的带 block 版本的注册函数可以让代码更简洁
在其中的 TODO 位置,就可以开始任务的收集和分发了,当然,不能忘记适时的移除这个 observer

CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFStringRef runLoopMode = kCFRunLoopDefaultMode;
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler
(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity _) {
    // TODO here
});
CFRunLoopAddObserver(runLoop, observer, runLoopMode);

利用CFRunLoopMode的特性,可以将图片的加载放到NSDefaultRunLoopMode的mode里,这样在滚动UITrackingRunLoopMode这个mode时不会被加载而影响到。


如何判断网络请求是否开启了代理,防charles抓包

- (BOOL)getProxyStatus {
    NSDictionary *proxySettings =  (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
    NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:@"http://www.baidu.com"]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));
    NSDictionary *settings = [proxies objectAtIndex:0];
    
    NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);
    NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);
    NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);
    
    if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"]){
        //没有设置代理
        return NO;
    }else{
        //设置代理了
        return YES;
    }
}

读取文件属性

当试图获取磁盘中一个文件的属性信息时,使用 [NSFileManager attributesOfItemAtPath:error:] 会浪费大量时间读取可能根本不需要的附加属性。这时可以使用 stat 代替 NSFileManager,直接获取文件属性:

#import 
struct stat statbuf;
const char *cpath = [filePath fileSystemRepresentation];
if (cpath && stat(cpath, &statbuf) == 0) {
    NSNumber *fileSize = [NSNumber numberWithUnsignedLongLong:statbuf.st_size];
    NSDate *modificationDate = [NSDate dateWithTimeIntervalSince1970:statbuf.st_mtime];
    NSDate *creationDate = [NSDate dateWithTimeIntervalSince1970:statbuf.st_ctime];
    // etc
}

class_isMetaClass的使用场景

NSMutableArray *arr = [NSMutableArray new];

[arr addObject:[NSObject class]];
[arr addObject:[NSValue class]];
[arr addObject:[NSNumber class]];
[arr addObject:[NSPredicate class]];
[arr addObject:@"not a class object"];

for (int i; i<[arr count]; i++) {
    id obj = [arr objectAtIndex:i];
    
    if(class_isMetaClass(object_getClass(obj)))
    {
        //do sth
        NSLog(@"Class: %@", obj);
    }
    else
    {
        NSLog(@"Instance: %@", obj);
    }
}

过滤字符串前面和后面的空白和换行

//常用于聊天信息发送时字符串处理
[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]

利用arc4random_uniform()产生随机数

OC 中有个arc4random()函数用来生成随机数且不需要种子,但是这个函数生成的随机数范围比较大,需要用取模的算法对随机值进行限制,有点麻烦。其实Objective-C有个更方便的随机数函数arc4random_uniform(x),可以用来产生0~(x-1)范围内的随机数,不需要再进行取模运算。如果要生成1~x的随机数,可以这么写:arc4random_uniform(x)+1


屏蔽编译警告

对于关闭某个警告,如果需要全局关闭的话,直接在Other C Flags里写 -Wno-...
就行了,比如 -Wextra -Wno-sign-compare
就是一个常见的组合。如果相对某几个文件开启或禁用警告,在Build Phases的Compile Source相应的文件中加入对应的编译标识即可。如果只是想在某几行关闭某个警告的话,可以通过临时改变诊断编译标记来抑制指定类型的警告,具体如下:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[transaction.target performSelector:transaction.selector];
#pragma clang diagnostic pop


多线程group用法

//1.创建队列组
dispatch_group_t group = dispatch_group_create();
//2.创建队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//3.多次使用队列组的方法执行任务, 只有异步方法
//3.1.执行3次循环
dispatch_group_async(group, queue, ^{
    for (NSInteger i = 0; i < 3; i++) {
        NSLog(@"group-01 - %@", [NSThread currentThread]);
    }
});
//3.2.主队列执行8次循环
dispatch_group_async(group, dispatch_get_main_queue(), ^{
    for (NSInteger i = 0; i < 8; i++) {
        NSLog(@"group-02 - %@", [NSThread currentThread]);
    }
});
//3.3.执行5次循环
dispatch_group_async(group, queue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"group-03 - %@", [NSThread currentThread]);
    }
});
//4.都完成后会自动通知
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    NSLog(@"完成 - %@", [NSThread currentThread]);
});

创建屏幕截图系统API

@interface UIScreen (UISnapshotting)
// Please see snapshotViewAfterScreenUpdates: in UIView.h for some important details on the behavior of this method when called from layoutSubviews.
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);
@end

iOS-NSDateFormatter 格式说明

转载

格式化参数如下:
G: 公元,例如AD公元
yy: 年的后2位
yyyy: 完整年
MM: 月,显示为1-12
MMM: 月,显示为月份简写,如 Jan
MMMM: 月,显示为英文月份全称,如 Janualy
dd: 日,2位数表示,如02
d: 日,1-2位显示,如 2
EEE: 简写星期几,如Sun
EEEE: 全写星期几,如Sunday
aa: 上下午,AM/PM
H: 时,24小时制,0-23
K:时,12小时制,0-11
m: 分,1-2位
mm: 分,2位
s: 秒,1-2位
ss: 秒,2位
S: 毫秒

常用日期结构:
yyyy-MM-dd
HH:mm:ss.SSS
yyyy-MM-dd
HH:mm:ss
yyyy-MM-dd
MM dd yyyy


根据图片填充相应颜色

原图Mask

填充颜色后效果
- (UIImage *)jsq_imageMaskedWithColor:(UIColor *)maskColor
{
    NSParameterAssert(maskColor != nil);
    
    CGRect imageRect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
    UIImage *newImage = nil;
    
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, self.scale);
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextScaleCTM(context, 1.0f, -1.0f);
        CGContextTranslateCTM(context, 0.0f, -(imageRect.size.height));
        
        CGContextClipToMask(context, imageRect, self.CGImage);
        CGContextSetFillColorWithColor(context, maskColor.CGColor);
        CGContextFillRect(context, imageRect);
        
        newImage = UIGraphicsGetImageFromCurrentImageContext();
    }
    UIGraphicsEndImageContext();
    
    return newImage;
}

怎么改变uitextfield placeholder的颜色和位置

//继承uitextfield,重写这个方法
- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}

把navigationbar弄成透明的而不是带模糊的效果

[self.navigationBar setBackgroundImage:[UIImage new]
                         forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

拦截网页图片 并修改图片大小

[aWebView stringByEvaluatingJavaScriptFromString:
 @"var script = document.createElement('script');"
 "script.type = 'text/javascript';"
 "script.text = \"function ResizeImages() { "
 "var myimg,oldwidth;"
 "var maxwidth=320;" //缩放系数
 "var images = document.images;"
 "alert(images.length);"
 "for(i=0;i  maxwidth){"
 "oldwidth = myimg.width;"
 "myimg.style.width = maxwidth;"
 "myimg.style.height = myimg.height * (maxwidth/oldwidth);"
 "}"
 "}"
 "}\";"
 "document.getElementsByTagName('head')[0].appendChild(script);"];

[aWebView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];

最简单的获取网络get的数据

NSError *error = nil;
NSString *environmentResult = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://211.144.118.118/v1.0.0/house/job/get_url"] encoding:NSUTF8StringEncoding error:&error];

获取某个类的成员变量、属性、方法

unsignedint numIvars; //成员变量个数
Ivar *vars = class_copyIvarList(NSClassFromString(@"BMKLocationService"), &numIvars);

NSString *key=nil;
for(int i = 0; i < numIvars; i++) {
    
    Ivar thisIvar = vars[i];
    key = [NSStringstringWithUTF8String:ivar_getName(thisIvar)];  //获取成员变量的名字
    NSLog(@"variable name :%@", key);
    key = [NSStringstringWithUTF8String:ivar_getTypeEncoding(thisIvar)]; //获取成员变量的数据类型
    NSLog(@"variable type :%@", key);
}
free(vars);


Method*meth =class_copyMethodList(NSClassFromString(@"UIView"), &numIvars);

for(inti =0; i < numIvars; i++) {
    MethodthisIvar = meth[i];
    
    SELsel =method_getName(thisIvar);
    constchar*name =sel_getName(sel);
    
    NSLog(@"zp method :%s", name);
}
free(meth);

returnYES;

过滤掉除了数字之外的字符

NSString* simplePhoneNumber =
[[phoneNumber componentsSeparatedByCharactersInSet:
  [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
 componentsJoinedByString:@""];

OC、C与C++ 字符串转换

//char * /const char * 转NSString
NSString * strPath = [NSString stringWithUTF8String:filename];

//NSString转char * /const char *
const char * filePathChar = [filePath UTF8String];

//转化char 到NSString
char myChar ='a';
NSString* string =[NSString stringWithFormat:@"%c", myChar];

//提取NSString的某个字段到char
- (unichar)characterAtIndex:(NSUInteger)index;

// c++ 和 oc的本身是不能直接对接的。要通过c的api做连接的。
// string 转 NSString
string str = [aNSString UTF8String];

//NSString 转 string
string str("testStr");
NSString * aString = [NSString stringWithUTF8String:str.c_str()];

方法 可变参数

- (id)initWithTitle:(NSString*)title delegate:(id)delegate cancelButtonTitle:(NSString*)cancelTitle destructiveButtonTitle:(NSString*)destructiveTitle otherButtonTitles:(NSString*)otherTitles, ... {
    
    self = [self init];
    self.delegate= delegate;
    self.backgroundColor= [UIColorclearColor];
    
    NSMutableArray*titles = [[NSMutableArrayalloc]init];
    
    if(otherTitles) {
        va_listargs;
        va_start(args, otherTitles);
        for(NSString*arg = otherTitles; arg !=nil; arg =va_arg(args,NSString* ))
        {
            [titlesaddObject:arg];
        }
        va_end(args);
    }
}

强制继承重写方法

- (BOOL)isFinished
{
    [self doesNotRecognizeSelector:_cmd];
    return NO;
    
}

(未完待续)

你可能感兴趣的:(iOS系统API笔记)