本文整理日常 iOS 開發中常見的實用技巧,涵蓋圖片處理、版本控制、介面調整、資料持久化、推送與上架相關問題。這些技巧多來自實際專案經驗,適合在開發過程中隨時查閱。
可以使用 Core Image 的高斯模糊濾鏡,讓圖片呈現毛玻璃質感。
let blurFilter = CIFilter(name: "CIGaussianBlur")
blurFilter?.setValue(ciImage, forKey: KCIInputImageKey)
blurFilter?.setValue(value, forKey: "inputRadius")
其中 inputRadius 代表模糊程度,數值越大,模糊越明顯。
若需要縮小圖片尺寸,可依原圖比例計算目標高度。
let targetHeight = targetWidth / width * height
UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight))
self.draw(in: CGRect(x: 0, y: 0, width: targetWidth, height: targetHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
若需要取得 UIColor 的 RGB 元件,可以透過 CGColor 取得 components。
UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
const CGFloat *components = CGColorGetComponents(color.CGColor);
SVN 常見流程是 CheckOut、修改、Commit、Update。Git 常見流程則是 Pull、Commit、Push。不論使用哪一種工具,每天下班前都應確保提交內容可以編譯,上班後也應先更新最新版本。
PCH 常用來放置全域標頭檔。建立後,需要在 Build Settings 中設定 Prefix Header 路徑。
如果遇到 No architectures to compile for 這類錯誤,可以檢查 Build Settings 中的 Architectures 與 Valid Architectures。
Version 是給使用者看的版本號,例如 1.0、2.0。Build 則是給開發者與 iTunes Connect 辨識的建置編號。若 Version 已提交過,通常可以透過調整 Build 號再次提交。
若需要清理舊的簽名檔,可以前往 Provisioning Profiles 目錄刪除。Xcode 中也可以在 Accounts 的 View Details 重新整理簽名資料。
當 iTunes Connect 要求接受最新協議時,可依畫面步驟完成更新。
若 rootViewController 被放入 UINavigationController,可以直接設定 title,讓 navigationItem 與 tabBarItem 一起顯示。
root.title = @"title";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
在 iOS 7 之後,建議在 view controller 中重寫:
- (BOOL)prefersStatusBarHidden {
return YES;
}
tableView.tableFooterView = [UIView new];
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
可以透過 CoreTelephony.framework 取得 SIM 狀態。
可以透過 SystemConfiguration 與 CaptiveNetwork 相關 API 取得網路介面資訊。
NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
advertisingIdentifier 用於廣告追蹤,identifierForVendor 則可辨識同一開發者的應用。
常見的資料保存方式包括:
@try {
[database beginTransaction];
// 執行多筆寫入
} @catch (NSException *exception) {
[database rollback];
} @finally {
[database commit];
}
若需要大量寫入,使用事務可以減少磁碟操作次數,提升效能。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age == %d", 15];
NSArray *result = [array filteredArrayUsingPredicate:predicate];
謂詞可以簡化集合篩選,避免撰寫大量 if 判斷。
應用程式第一次啟動時會詢問使用者是否允許通知。若同意,系統會向 APNS 註冊並取得 DeviceToken。開發者需要將 DeviceToken 送到自己的伺服器或推送服務平台,後續即可由伺服器發送推播。
可以使用 MPMediaQuery 查詢媒體庫中的音樂項目。
可以透過 MPVolumeView 內的 MPVolumeSlider 調整系統音量,並監聽音量變更通知。
若要判斷是否安裝特定應用,可以使用 canOpenURL。若要開啟網頁,可以使用 openURL。
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
同一台裝置能否安裝兩個相同名稱的應用,主要取決於 bundle identifier 是否相同。App Store 不允許同名應用,但企業簽名應用可自訂名稱與 bundle id。
若需要將某個 view 的座標系轉換到另一個 view,可以使用 convertRect 或 convertPoint。
在 macOS 命令列輸入:
uuidgen
可以透過 itunes.apple.com 的應用網址導向 App Store 頁面。
若線上版本有嚴重問題,可以透過 Apple 的 App Review Team 表單申請加急審核。
這些技巧看似零散,但在實際專案中往往能節省大量除錯時間。建議依專案需求整理成自己的開發備忘,遇到問題時就能快速定位。