Thursday, March 6, 2014


I'm using AFNetworking to access a HTTPS website.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // We modify AFSecurityPolicy to allow invalid certificate only for beecloud.cn
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager GET:@"https://url" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
}];


But I got the following errors:

2014-03-06 00:46:13.311 xctest[61972:1b27] CFNetwork SSLHandshake failed (-9806)
2014-03-06 00:46:13.371 xctest[61972:1b27] CFNetwork SSLHandshake failed (-9806)
2014-03-06 00:46:13.456 xctest[61972:1b27] CFNetwork SSLHandshake failed (-9806)
2014-03-06 00:46:13.458 xctest[61972:1b27] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9806)

2014-03-06 00:46:13.461 xctest[61972:303] Error: Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x102a2aa50 {NSUnderlyingError=0x10c0dd710 "An SSL error has occurred and a secure connection to the server cannot be made.", NSErrorFailingURLStringKey=, NSErrorFailingURLKey=, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.}

It turns out that at our server side, we are generating a self-signed certificate using the keys generated using the default DSA algorithm. Somehow iOS has trouble dealing with DSA keys for SSL. After changing it to RSA using the following command, the problem is gone: 

keytool -genkey -keyalg RSA -alias server -keystore real_serverKeys 

Problem is gone!

Saturday, March 1, 2014

写代码的建议

几个写代码的建议,尽量避免重新发明车轮,一般比较好的做法是写一些底层的比较灵活的API,然后对这些底层API做各种crazy的unit test,然后其他的高层的API都调用这些底层的API,这样能够减少bug,代码design也更好。

我在Google受到的training有很多是关于代码格式,我刚来Google的时候,写的第一段代码写了改改了写差不多10多个来回,被人拍的体无完肤。以后要把这个习惯延续。
1 所有的函数都需要写注释,写清楚这个函数干嘛的,@param 参数是什么, @return 返回值是什么。
2 Unit Test极其重要,写的每一个函数最好都能有unit test来测试各种分支情况,尤其是对于逻辑复杂的函数,必须有Unit Test,各种不合法的输入等等。(UI可能不好unit test,但是对于写的一些具有复杂的logic代码也要有unit test)。
3 代码里面要多些注释,尤其是一些不那么常见的设计,或者是自己图方便图简单的设计,不那么显而易见的逻辑都需要写注释。

这些看是浪费时间,实则磨刀不误砍柴工,我深有体会,以前读PHD时候也是懒得这么做,后来上班了读别人的代码才知道这样做的重要性。

做后端,就是要能静下心,做技术的就是要有对技术追求精益求精的强迫症。所以你们在做设计,写代码的时候,一定要做到有“洁癖”,有“强迫症”,一定要找到最elegant的方案。

另外Google永远是好伙伴,找不到解决方案一定要上Google搜索,中文或者英文的关键词都试试,很多问题都迎刃而解。

共勉+自勉

Monday, January 30, 2012

Can I make MATLAB code run silently?

Can I make MATLAB code run silently, without producing any intermediate results or showing any values of matrix or variables?

Yes, the answer is amazingly simple.

Add a semicolon ";" to the end of every line of code

That's it:)

Thursday, March 10, 2011

How to efficiently write academic paper on Mac OS?

I wanna write something on this because some of my friends using Mac complain about the inconvenience to use Latex-related software to write paper.

I have to admit that WinEdt is pretty awesome (I used it years ago in my undergrad). But

Can we do all WinEdt can do under Mac?

I think the answer should be positive.

My current solution is

VIM + TexShop

The special feature I love about TexShop is the "sync" which leads you to the correct tex file and correct line and even correct word which corresponds to the PDF selection (WinEdt can do this of course). Another good thing about TexShop is that compiling LaTex is really fast (compared with my weak Makefile), and also the preview windows loads the file where you were rather than from the beginning. I will talk later how annoying Adobe Reader and Preview are which always loads your new PDF file from the beginning.

VIM's magic lies in ~/.vimrc and I plan to write more about it.
Today I found a good post writing about How to Use Vim's Spellchecker and How to use Vim’s textwidth like a pro. Some of these features are not included in my previous post of a good .vimrc cheat sheet.

For now, I'm still trying to make VIM + TexShop to be as good as WinEdt (currently still lagging behind). For example, I want to make double clicking a word redirect me to the editing location rather than right-click and then click "Sync". Another example is the search function of PDF preview in TexShop does not work, you have to search in the drawer's window, which sucks!

Adobe Reader's search is way better, but after compilation, it's hard to go to the old editing location in Adobe Reader because it considers the file to be different before and after compilation (so even if you enable "Restore last view settings when reopening documents", after "make clean" and "make all" of your paper, opening the paper will start from the beginning and you have to scroll pages before you find where you were ..., Preview app in Mac has similar problems).

I feel I need to future change some shortcuts to make VIM+TexShop more efficient and eventually, it should be comparable to WinEdt if not more.

Mac should not be in any case worse than Windows except for hosting viruses!


Sunday, February 27, 2011

iPhone compilation problem

this post saved my life!