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

2012年12月23日 星期日

iOS 6 畫面旋轉小技巧

目前為了要相容 iOS 5 和 6 的旋轉,在原本的旋轉方法下,還要加入以下方法。

viewController.mm
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//僅支援直式
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSUInteger)supportedInterfaceOrientations
{
//僅支援直式
return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
return YES;
}


以為解決了,結果在 AppDelegate.m 下用

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootviewcontroller = navigationController;

這下可好,完成不理之前的設定(僅支援直式),變成任意旋轉了@_@|||
查了查才發現 self.viewController 是 UIViewController,而被加入 UINavigationController ,旋轉設定並無法影響,解決方法有兩種:
1. 改 UIViewController 為 UINavigationController 的子類別
2. 直接在 AppDelegate.m 覆寫 supportedInterfaceOrientations 方法,如下所示:

@implementation UINavigationController (iOS6OrientationFix)

-(NSUInteger) supportedInterfaceOrientations
{
//被加入的 viewController 為最上層,故只取 topViewController
return [self.topViewController supportedInterfaceOrientations];
}

@end

參考來源:
http://stackoverflow.com/questions/12410031/ios-6-rotations-supportedinterfaceorientations-doesnt-work
http://stackoverflow.com/questions/12504464/ios-6-uitabbarcontroller-supported-orientation-with-current-uinavigation-control


好了,打完收工!