2014年9月26日 星期五

[iOS] iOS 8 用 UIAlertController 來取代原本的 UIAlertView & UIActionSheet

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@“title" message:@“message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@“cancel"
                                                                      style:UIAlertActionStyleCancel
                                                                    handler:^(UIAlertAction * action)
{
    [alert dismissViewControllerAnimated:YES completion:nil];
}];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@“ok"
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action)
{
    [alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancelAction];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];

以下連結有更詳細的介紹:
http://useyourloaf.com/blog/2014/09/05/uialertcontroller-changes-in-ios-8.html

[iOS] iOS 8 進入 Settings 復活了

原本在 iOS 5.0 支援的終於在 iOS 8 又回來了,但目前只能進入 "Settings" 而己...

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

[iOS] iOS 7&8 UISearchBar 去除背景的方法

UISearchBar 在 iOS 的不同版本中,把 subviews 列出來看會發現 view 層次結構不同,而要去除背景主要是將 UISearchBarBackground 給 remove 掉,因此就要針對各個 iOS 的版本做點調整,如下所示:

for (UIView *view in self.searchBar.subviews)
{
    iOS 7 以前:
    if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
    {
        [view removeFromSuperview];
        break;
    }

    iOS 7 含以後:
    if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0)
    {
        for (UIView *subView in view.subviews)
        {
            if ([subView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                [subView removeFromSuperview];
                break;
            }
        }
    }
}

2014年8月28日 星期四

2014.8.2 退伍

2014.8.2 凌晨起退伍啦!
看來 blogger 的標題也要想個新名字!

[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();
}