Disable App Nap in MacOS

from 10.9

1
2
3
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)]) {
[[NSProcessInfo processInfo] beginActivityWithOptions:0x00FFFFFF reason:@"receiving OSC messages"];
}

ios appstore 下载链接 评价链接

1
2
3
4
5
//http://itunes.apple.com/us/app/idxxxxxxx
NSString *str = [NSString stringWithFormat:
@"http://itunes.apple.com/us/app/id%d",appid];

[[UIApplication sharedApplication] openURL:[NSURL urlWithString:str]];
1
2
3
4
//跳转到评价页面
NSString *str = [NSString stringWithFormat:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id;=%d",appid];
[[UIApplication sharedApplication] openURL:[NSURL urlWithString:str]];

Core Graphics

画圆

1
2
3
var path = UIBezierPath(ovalInRect: rect)
fillColor.setFill()
path.fill()

创建path

1
var plusPath = UIBezierPath()

创建扇形

1
2
3
4
5
6
7
8
9
10
11
12
13
//2 - draw the outer arc
var outlinePath = UIBezierPath(arcCenter: center,
radius: bounds.width/2 - 2.5,
startAngle: startAngle,
endAngle: outlineEndAngle,
clockwise: true)
 
//3 - draw the inner arc
outlinePath.addArcWithCenter(center,
radius: bounds.width/2 - arcWidth + 2.5,
startAngle: outlineEndAngle,
endAngle: startAngle,
clockwise: false)

角度
顺时针 3点钟是0°角

ios UITextField

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>
@property (strong) IBOutlet UITextField *f;
@end

@implementation ViewController

-(void)viewDidLoad{
[super viewDidLoad];
[self.f addTarget:self
action:@selector(valueChangeCallback:)
forControlEvents:UIControlEventEditingChanged];
}

//文本变更后触发
-(void) valueChangeCallback:(UITextField *) sender{
NSLog(@"f.text %@",sender.text);
}

-(IBAction) changeByCode:(id)sender{
self.f.text = @"1234";
//不会触发下面如何方法
}

#pragma mark - delegate

//清除按钮的回调
-(BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"textFieldShouldClear");
return YES;
}

//Return 按键的回调 返回yes表示 开发人员自己来处理Return时间
//Return yes/no 跟关键键盘没有直接关系
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"textFieldShouldReturn");
[textField resignFirstResponder];
return NO;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

}

//停止编辑前调用 返回yes继续编辑 返回no取消"停止编辑"
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}


//停止编辑后调用
-(void)textFieldDidEndEditing:(UITextField *)textField{

}

//ios 10的方法 替换上面这个方法
-(void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason{
NSLog(@"textFieldDidEndEditing:reason %zd",reason);

}

//文本变更之前的方法
//range是影响的文本范围
//replacementString是range范围文本的替换内容
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"-----------------");
NSLog(@"current [%@]",textField.text);
NSLog(@"range %zd, %zd",range.location, range.length);
NSLog(@"replacementString [%@]",string);
NSLog(@"-----------------");
return YES;
}
#pragma mark end -
@end

UITextfield placeholder color

1
2
3
4
5
6
7
- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [self.placeholder drawInRect:rect 
withFont:self.font 
lineBreakMode:UILineBreakModeTailTruncation 
alignment:self.textAlignment];
}

fieldeditor

TextField 焦点从f0->f1 切换

1
2
3
4
5
FieldEditor1 resignFirstResponder
FieldEditor1 resignFirstResponder
TextField-f1 becomeFirstResponder
TextField-f1 resignFirstResponder
FieldEditor1 becomeFirstResponder

AXIsProcessTrustedWithOptions

1
2
3
NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};
BOOL accessibilityEnabled =
AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
1
2
3
let options:[NSString:NSNumber] = 
[String(kAXTrustedCheckOptionPrompt.takeRetainedValue()) as String:true]
let accessibilityEnabled = AXIsProcessTrustedWithOptions(options);

NSAlert

1
2
3
4
5
6
7
8
9
10
11
12
13
NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"Continue"];
    [alert addButtonWithTitle:@"Cancel"];
    [alert setMessageText:@"Alert"];
    [alert setInformativeText:@"content"];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
        if (returnCode == NSAlertFirstButtonReturn) {
            [[NSApplication sharedApplication] replyToApplicationShouldTerminate:YES];
        } else {
            [[NSApplication sharedApplication] replyToApplicationShouldTerminate:NO];
        }
    }];

ios lifecycle event

1.启动
application:didFinishLaunchingWithOptions
applicationDidBecomeActive

2.Home键退出/锁屏键退出/电话进来
applicationWillResignActive
applicationDidEnterBackground

3.后台启动
applicationWillEnterForeground
applicationDidBecomeActive

4.前台接收到通知
application:didReceiveLocalNotification 0
(UIApplicationStateActive)

5.前台接收到通知(不会触发回调事件)
此时在notification bar 不会看到记录

5.1点击(通知)
applicationWillEnterForeground
application:didReceiveLocalNotification 1
(UIApplicationStateInactive)
applicationDidBecomeActive

5.2未启动的时候点击通知
application:didFinishLaunchingWithOptions
(UIApplicationLaunchOptionsLocalNotificationKey)
applicationDidBecomeActive

6.前台状态下 下拉通知界面
applicationWillResignActive

Get image type from NSData

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
+ (NSString *)contentTypeForImageData:(NSData *)data {
uint8_t c;
[data getBytes:&c length:1];

switch (c) {
case 0xFF:
return @"image/jpeg";
case 0x89:
return @"image/png";
case 0x47:
return @"image/gif";
case 0x49:
case 0x4D:
return @"image/tiff";
}
return nil;
}