2014年10月24日 星期五

[iOS] How to detect iPhone 6 Plus screen size

簡單列出一下 iPhone 6 & iPhone 6 Plus 解析度:
        iPhone 6(4.7"):   750w x 1334h @2x (in points: 375w x 667h)
iPhone 6 Plus(5.5"): 1242w x 2208h @3x (in points: 414w x 736h)

本來天真的以為要判斷是否為 iPhone 6 Plus 只要查看 w or h 即可,但發現用模擬器執行 NSLog 印出 [UIScreen mainScreen].bounds 發現竟然跟 iPhone 5(4") 的解析度相同為 640w x 1136h,代誌不是憨人所想的那樣,查了一下發現可以用 nativeScale 來判斷,程式碼如下所示:

- (BOOL)IsiPhone6Plus
{
    if([[UIScreen mainScreen] respondsToSelector:@selector(nativeScale)])
    {
        // Nativescale is always 3 for iPhone 6 Plus
        if([UIScreen mainScreen].nativeScale > 2.1)
            return YES;
    }

    return NO;
}

Reference: http://stackoverflow.com/questions/25756087/detecting-iphone-6-6-screen-sizes-in-point-values

2014年10月20日 星期一

[iOS] iOS 8 UIAlertController scrolling 小記

最近發現在 UIAlertController 的 message 如果太長會導致無法 scroll ,解決方法為設定 frame 即可。如下:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                         message:messageString
                                                                  preferredStyle:UIAlertControllerStyleAlert];

alertController.view.frame = [[UIScreen mainScreen] applicationFrame];

Reference : http://stackoverflow.com/questions/24671833/uialertcontroller-uialertview-scrolling-on-ios-8

2014年10月5日 星期日

[iOS] iOS 6/7/8 UIActivity subclass for Line App

GitHub 開張啦,獻上一個 support iOS 6/7/8 UIActivity subclass for Line App,連結如下:

LineActivity

[iOS] iOS 8 non-public API usage 解決方法 (持續更新...)

一直以來都知道 apple 會把使用 non-public API 的 app 給 reject,沒想到最近真的遇到了!

一開始 apple 很貼心給個指令供查詢,最近更新到使用 Application Loader 時就會提示 (佛心),免除到進入 in review 才被 reject 的痛苦!

這邊要記錄一下有 "不小心" 用到的 non-public API 列表:
NSURLRequest
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost;
用途:允許任何 cer 通過 https 的連線方式,可以改用 (AFNetworking) allowsInvalidSSLCertificate 設為 YES 即可。

2014年9月27日 星期六

[iOS] iOS 8 Push Notification Service

iOS 8 為 Notification Center 帶來新功能,所以取得 token 的 api 也修改了,如下所示:

if(IS_IOS8)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

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

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"]。

2014年3月29日 星期六

[iOS] UITableViewCell 在 iOS 7 的分割線左邊少 20 個 pixel 補滿的方法小記

if([_tableView respondsToSelector:@selector(setSeparatorInset:)])
{
    [_tableView setSeparatorInset:UIEdgeInsetsZero];
}

2014年3月18日 星期二

[iOS] NSGenericException 錯誤小記

在使用 NSMutableArray 時,經常會對陣列中的元素做增減的動作,而在做元素刪除時,若有使用 foreach 來遍歷各個元素比對後刪減,就會發生 NSGenericException 的錯誤。

原因是只要在 foreach 裡做刪減時會將陣列的值全部失效,若剛好是最後一個值就沒問題,若不是即報錯誤。

解決方式提供2種,1種是一樣使用 foreach 做刪減,但完成後即 return 跳出。另1種是改用 for 迴圈即可。

2014年3月8日 星期六

[iOS] 如何設定系統相機、相簿的語系

每次在使用 iOS 的預設拍照時,都會遇到語系是英文,即拍照畫面上的文字為英文語系。
如:取消=cancel,重新拍攝=retake 等等。

如果要支援其他語系(依使用者裝置預設的語系),設定很簡單,只要把專案的 Localizations 裡新增想要的語系即可。


另一個是在 info.plist 裡增加 Localized resources can be mixed 為 YES

2014年3月3日 星期一

[iOS] 使用 AFNetworking 遇到 "Error Domain=AFNetworkingErrorDomain Code=-1016" 處理小記

常常使用 AFNetworking 在處理與伺服端的 api 溝通,而回傳值是用 Json 格式。
最近遇到伺服端回傳 text/html 的 content-types,而 AFJSONRequestOperation 預設支援的 content-types 為 "text/json", "application/json" or "text/javascript" 這三種。

解決方法為將 text/html 加入支援的 content-types 即可

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

2014年1月21日 星期二

[iOS] iOS7 在提示使用者給予 app 評分&評論小記

最近許多 app 會在內部新增一個按鈕或視窗提示使用者給予 app 好的評分 or 評論,來提升 app 的排名等等。記錄一下連結在 iOS7 的改變:

itms-apps://itunes.apple.com/app/idAPP_ID?at=10l6dK

iOS 6以下可用原本的連結:
itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APP_ID?at=10l6dK

APP_ID:可在 itunes connect 裡查看