2014年8月28日 星期四

[iOS] Switching from http to https. Invalid certificate

常用一些第三方套件提供網路通訊時,如果 server 端為 https 需要做一些設定來排除如標題所提到的問題,主要是設定允許 certificate 存取:

@interface NSURLRequest(Private)
+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost;
@end

使用如下指令:
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[inURL host]];

如果是使用 AFNetworking 套件時再加入以下定義:(加在 .pch 檔內)
#ifdef DEBUG
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
#endif

參考來源:http://stackoverflow.com/questions/12447318/switching-from-http-to-https-invalid-certificate

[iOS] NSTimer 機制小記

常常在用 NSTimer 來設計一些延遲或重覆執行某個 func call,但如果有用到 touch 事件,如:圖片點擊,UIScrollView 滑動等,就會把該 fund call 暫停,直到 touch 結束才繼續執行。

這樣一來就有時間差的問題存在,問了 google 大大後得知有人分享解決方式,就是將 NSTimer 加到別的 RunLoopMode 即可。

在 NSTimer 初始化後加入以下指令:
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

參考來源:http://stackoverflow.com/questions/6684016/why-timer-stops-when-scrolling-in-uiwebview-iphone

2014年8月19日 星期二

[iOS] 畫個虛線

- (void)makeLineInImageView:(UIImageView *)imageView
{
    UIGraphicsBeginImageContext(imageView.frame.size);
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //設定線條終點形狀
   
    float lengths[] = {2, 2}; //第一個參數設定每個虛線寬度,第二個參數設定虛線的間隔
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
   
    CGContextSetLineDash(line, 0, lengths, 2);
    CGContextMoveToPoint(line, 0.0f, 0.0f);    //開始畫線
    CGContextAddLineToPoint(line, imageView.frame.size.width, 0.0f);
    CGContextStrokePath(line);
   
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
}

2014年7月30日 星期三

[iOS] How To Disable AFNetworking Cache

最近使用 AFNetworking 來下載圖片,發現在上傳圖後至 back-end 後,再更新會有 cache 的問題,在此註記一下如何忽略 cache 問題。主要是將 cachePolicy 設為 NSURLRequestReloadIgnoringLocalAndRemoteCacheData 即可。

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
 cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                          timeoutInterval:20];

2014年7月19日 星期六

[iOS] Custom UIAlertView for iOS 7

自從 iOS 7 開始,很多人說無法加入 custom view。

若要帶輸入框(UITextField)的話,雖然本身有提供好幾種模式,如:UIAlertViewStyleLoginAndPasswordInput (帳號&密碼)、UIAlertViewStylePlainTextInput (文字)等。

結果發現要做個重設密碼,需要2個 UITextField 且為 Secure 模式就無法度了,因為 UIAlertViewStyleLoginAndPasswordInput 其中有1個不是 Secure 模式。

拜讀 google 大神後找到好心人提供方法,去設定 UIAlertView 裡的 accessoryView 參數即可。
[alertView setValue:customContentView forKey:@"accessoryView"];

參考來源:http://stackoverflow.com/questions/18886048/ios-multiple-text-fields-on-a-uialertview-in-ios-7

2014年7月18日 星期五

[iOS] Reaching the bottom of the UIScrollView

光陰似劍,研替3年快結束了!要轉職回小老百姓了說。

話說之前在使用 UIScrollView 來判斷是否 scroll 到底都用 scrollViewDidScroll 來判斷,今天發現另一個方法來實作,好處是可以減少一直呼叫丫!

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (bottomEdge >= scrollView.contentSize.height)
    {
        // we are at the end
    }
}

參考來源:http://stackoverflow.com/questions/6217900/uiscrollview-reaching-the-bottom-of-the-scroll-view

2014年4月21日 星期一

[iOS] 使用 ASIHTTPRequest 函式庫遇到 502 error 處理小記

目前在使用與伺服器溝通的第3方函式庫中,多數常用 ASIHTTPRequest 或 AFNetworking,而 ASIHTTPRequest 停止更新後,都改用 AFNetworking 比較多。最近在處理舊專案是使用 ASIHTTPRequest 時,突然出現了 Server Error
502 - Web server received an invalid response while acting as a gateway or proxy server 的錯誤訊息。

由於伺服器環境是使用微軟的 Azure WebSites,發生的原因是 Azure 無法處理 user-agent 的問題導致,解決方法是設定一下 user-agent 即可,如:[ASIHTTPRequest setDefaultUserAgentString:@"MyApp 1.0"]。