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];

2013年7月4日 星期四

[iOS] NSURLRequest 中 URL 的中文字處理小記

最近把 web link 用 uiwebview 來呈現,發現當 link 有中文字時,NSURLRequest 會無法順利載入,拜讀 google 大大後,找到解決方法,要透過 stringByAddingPercentEscapesUsingEncoding 來將中文字轉換成 percent escaped的字串,記錄如下:

NSString *urlStr = @"http://www.abc.com.tw/中文字.png";
NSString *imageUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: imageUrlStr]];

2013年5月6日 星期一

[iOS] pushViewController:animated & popViewControllerAnimated 使用 CATransition 小記

一般常見使用 push&pop ViewController 時,都是向右進入及向左退出。
若要使用其他效果的過場動畫時,可以搭配 CATransition 這個類別來使用。

需要的 framework:QuartzCore

使用方法:

CATransition *animation = [CATransition animation];
[animation setDuration:0.3];
[animation setType:@"oglFlip"];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

UIViewController *viewVC = [[UIViewController alloc] initWithNibName:@"UIViewController_iPhone" bundle:nil];
[self.navigationController pushViewController: viewVC animated:NO];
[self.navigationController.view.layer addAnimation:animation forKey:nil];
[viewVC release];




CATransition *animation = [CATransition animation];
[animation setDuration:0.3];
[animation setType:@"oglFlip"];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[self.navigationController.view.layer addAnimation:animation forKey:nil];
[self.navigationController popViewControllerAnimated:YES];

PS:要特別注意,push 與 pop ViewController 的順序有不同。

2013年4月18日 星期四

[iOS] InApp 在 iOS5 之後呼叫 paymentWithProductIdentifier 的問題小記

InApp Purchase 是 app developer 很大的收入來源,最近在使用發現原本使用的函式有些許改變,記錄一下:


+ (id)paymentWithProductIdentifier:(NSString *)identifier __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_3_0,__IPHONE_5_0);

在 iOS5.0 前使用的好好,在之後可改由下列方式實作:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
  //宣告 SKProduct *proUpgradeProduct 來儲存收到的付費資訊

  SKPayment *payment = [SKPayment paymentWithProduct:proUpgradeProduct];
  [[SKPaymentQueue defaultQueue] addPayment:payment];
}

PS:InApp 目前還無法在模擬器上完整跑完流程,還是乖乖在實機上測試為準。

2013年2月25日 星期一

[iOS] MFMailComposeViewController 當無任何信箱導致閃退小記


最近做個 會計app 在 ipad 上執行遇到呼叫寄送 email 的錯誤,導致閃退。
google 一下有人分享原因是當系統設定裡若尚未新增任何 email account 時造成的錯誤。
因此在呼叫寄送時先檢查一下,若尚未設定則可顯示一個提示視窗告知 user 先去新增即可。

if([MFMailComposeViewController canSendMail])
{
    // 顯示寄送 email 視窗
}
else
{
   // 顯示視窗提示尚未設定任何 email 信箱,請先至"設定"裡新增
}

2013年2月8日 星期五

[iOS] ZXing 在 iOS 6.1 的問題解決小記

最近把 iOS SDK 升到 6.1 之後,編譯 ZXing 就出現錯誤,訊息如下:

BinaryBitmap.h: Private field 'cached_y_' is not used


解決方法如下:
點選 ZXingWidget --> TARGETS 選擇 ZXingWidget --> 找到 Other Warning Flags 增加下列一項,再重編即可。

-Wno-unused-private-field