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 裡查看

2013年10月29日 星期二

[iOS] iOS7 使用 UIAlertView 顯示輸入框小記

在 iOS6 以前,會使用以下方法來顯示輸入框:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Enter Password" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setSecureTextEntry:YES];  //輸入內容為密碼
[alert addSubview:myTextField];
[myTextField release];


但在 iOS7 以後,addSubview 函式不支援了,要改統一使用以下方法:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Enter Password" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;  //輸入內容為密碼
UITextField *myTextField = [alertView textFieldAtIndex:0];
[alertView show];

2013年10月16日 星期三

[iOS] 測試 InApp 時,出現無法連接 iTunes Store 的錯誤時處理小記

每次測試 InApp 總讓我有新的注意事項,發現取得付費資訊正常。
但按下確認要輸入帳號資訊時,一直出現"無法連接 iTunes Store"的錯誤訊息。
結果才發現,要先把設定裡的 apple id 登出,才可使用。
或者直接登入在 iTunes Connect 裡建立好的 tester 帳號亦可。

2013年10月10日 星期四

[iOS] 如何設定 SearchBar 背景色

在 iOS6 以前,加入以下方法即可。

for(UIView *subview in searchBar.subviews)
{
    if([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
    {
        [subview removeFromSuperview];
        break;
    }
}

但在 iOS7 中,要再加下以下方式才可以。

[searchBar setBarTintColor:[UIColor clearColor]];

2013年9月23日 星期一

[iOS] iOS7 狀態欄無法消失?

//在 root ViewController 中,viewDidload 函式裡增加
if([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
{
    // iOS 7
    [self prefersStatusBarHidden];
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}
else
{
    // iOS 6
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}


// 增加此函式
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

如此即可相容 iOS6 & iOS7 的情況。

2013年9月11日 星期三

[iOS] NSMutableArray 排序小記

一般在陣列排序都使用以下方式:
NSSortDescriptor *countSorter = [NSSortDescriptor sortDescriptorWithKey:@"count" ascending:YES];

[self.listData sortUsingDescriptors:[NSArray arrayWithObject:countSorter]];
[countSorter release];


但,發現如果 key 值是整數的話,就要加上 comparator 來判斷才可。
NSSortDescriptor *countSorter = [NSSortDescriptor sortDescriptorWithKey:@"count" ascending:YES comparator:^(id obj1, id obj2)
{
    if([obj1 integerValue] > [obj2 integerValue])
    {
        return (NSComparisonResult)NSOrderedDescending;
    }
    if([obj1 integerValue] < [obj2 integerValue])
    {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

[self.listData sortUsingDescriptors:[NSArray arrayWithObject:countSorter]];
[countSorter release];


如果是判斷內容是 NSDate 的話,使用以下方式:
NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
self.listData = [[self.listData sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];