您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 技巧大全

iOS 開發常用第三方框架大盤點

編輯:iOS 技巧大全

寫 iOS 專案幾乎不可能完全不碰第三方庫。善用成熟框架能把大量重複勞動外包出去,讓你把精力放在產品本身的邏輯上。GitHub 上有大量優質專案,授權條款對商業使用也相當友善。以下依功能分類整理。

網路與資料序列化

AFNetworking

Objective-C 生態系中最廣泛使用的網路請求庫。

  • GitHub:https://github.com/AFNetworking/AFNetworking
  • CocoaPod:pod 'AFNetworking', '~> 2.5.0'

Mantle

專門簡化 Model 層。App 發請求拿回的多半是 JSON,過去得手寫大量序列化程式碼把 JSON 轉成 Model,繁瑣又易錯。Mantle 把這件事抽象得很乾淨。

  • GitHub:https://github.com/Mantle/Mantle
  • CocoaPod:pod 'Mantle', '~> 1.5.4'

SBJson

老牌 JSON 編解碼工具。

  • 官網:https://sbjson.org/
  • GitHub:https://github.com/stig/json-framework

Reachability

監聽網路連線狀態,幾乎是每個 App 的標配。

  • GitHub:https://github.com/tonymillion/Reachability
  • CocoaPod:pod 'Reachability', '~> 3.2'

版面與 Auto Layout

Masonry

螢幕尺寸越來越碎片化,用程式碼寫絕對定位早已不合時宜。Masonry 用鏈式語法包裝 Auto Layout,把蘋果原本晦澀難懂的 API 寫得像自然語言一樣清楚。

  • GitHub:https://github.com/Masonry/Masonry
  • CocoaPod:pod 'Masonry', '~> 0.6.1'

FlatUIKit

扁平化風格 UI 元件,外觀接近 WP 或 iOS 7 的設計語言。

  • GitHub:https://github.com/Grouper/FlatUIKit

PrettyKit / MGBox2 / Nimbus

三者都提供成套自訂元件。PrettyKit 涵蓋 UITableViewCell、UINavigationBar、UITabBar、UIToolBar 等;MGBox2 主打快速搭建表格與網格版面,包含 MGBox、MGTableBox、MGScrollView、MGButton、MGEvents、MGEasyFrame、MGLine 等,其中 MGBox 還支援 screenshot 截圖;Nimbus 則是了一套非常完整的 UI 元件庫,能明顯提升開發效率。

  • https://github.com/vicpenap/PrettyKit
  • https://github.com/sobri909/MGBox2
  • https://github.com/jverkoey/nimbus

提示與進度指示

MBProgressHUD

最經典的等待與提示元件,如果設計師沒有另外提供 loading 樣式,直接用它準沒錯。

  • GitHub:https://github.com/jdg/MBProgressHUD
  • CocoaPod:pod 'MBProgressHUD', '~> 0.9'

SVProgressHUD

效果與 MBProgressHUD 相近,但不需要遵循協定、也不用宣告實例,全部透過類方法呼叫,接入成本更低。

  • GitHub:https://github.com/samvermette/SVProgressHUD
// 顯示狀態
+ (void)show;
+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType;
+ (void)showWithStatus:(NSString *)string;
+ (void)showWithStatus:(NSString *)string maskType:(SVProgressHUDMaskType)maskType;

// 明確進度
+ (void)showProgress:(CGFloat)progress;
+ (void)showProgress:(CGFloat)progress status:(NSString *)status;
+ (void)showProgress:(CGFloat)progress status:(NSString *)status
            maskType:(SVProgressHUDMaskType)maskType;

// 隱藏
+ (void)dismiss;

// 成功、失敗狀態(圖示來自 Glyphish)
+ (void)showSuccessWithStatus:(NSString *)string;
+ (void)showErrorWithStatus:(NSString *)string;
+ (void)showImage:(UIImage *)image status:(NSString *)string;

ZAActivityBar

API 比 SVProgressHUD 更精簡,動畫取自 ZKBounceAnimation,狀態圖示來自 Pictos。

  • GitHub:https://github.com/zacaltman/ZAActivityBar
[ZAActivityBar showWithStatus:@"加載中..."];
[ZAActivityBar showSuccessWithStatus:@"成功!"];
[ZAActivityBar showErrorWithStatus:@"失敗!"];
[ZAActivityBar dismiss];

CMPopTipView

在指定視圖或導覽列按鈕旁彈出提示框。

  • GitHub:https://github.com/chrismiles/CMPopTipView
self.tipView = [[CMPopTipView alloc] initWithMessage:@"提示消息"];
self.tipView.delegate = self;
[self.tipView presentPointingAtView:anyButton inView:self.view animated:YES];
[self.tipView presentPointingAtBarButtonItem:barButtonItem animated:YES];

- (void)popTipViewWasDismissedByUser:(CMPopTipView *)popTipView {
    self.tipView = nil;   // 清理資源
}

下拉刷新與分頁載入

SVPullToRefresh

由 SVPullToRefresh 與 SVInfiniteScrolling 兩部分組成,為 UITableView 提供下拉刷新與上拉載入更多。

  • GitHub:https://github.com/samvermette/SVPullToRefresh
- (void)viewDidLoad {
    [super viewDidLoad];
    __weak SVViewController *weakSelf = self;

    [self.tableView addPullToRefreshWithActionHandler:^{
        [weakSelf insertRowAtTop];
    }];

    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf insertRowAtBottom];
    }];
}

- (void)insertRowAtTop {
    // 取得最新資料...
    [self.tableView.pullToRefreshView stopAnimating];
}

- (void)insertRowAtBottom {
    // 載入下一頁...
    [self.tableView.infiniteScrollingView stopAnimating];
}

STableViewController

比 PullToRefresh 多了上拉載入更多,需繼承自 STableViewController 並覆寫若干方法。

  • GitHub:https://github.com/shiki/STableViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"STableViewController Demo";
    self.headerView = headerView;   // 自訂下拉刷新視圖
    self.footerView = footerView;   // 自訂上拉載入視圖
}

- (BOOL)refresh {
    if (![super refresh]) return NO;
    [self performSelector:@selector(addItemsOnTop) withObject:nil afterDelay:2.0];
    return YES;
}

- (void)addItemsOnTop {
    // 載入資料...
    [self.tableView reloadData];
    [self refreshCompleted];
}

- (BOOL)loadMore {
    if (![super loadMore]) return NO;
    [self performSelector:@selector(addItemsOnBottom) withObject:nil afterDelay:2.0];
    return YES;
}

- (void)loadMoreCompleted {
    [super loadMoreCompleted];
    if (!self.canLoadMore) {
        // 沒有更多資料時的處理
    }
}

- (void)addItemsOnBottom {
    // 載入更多...
    [self.tableView reloadData];
    [self loadMoreCompleted];
}

ODRefreshControl

還原 iOS 6 橡皮筋式的下拉刷新樣式,蝦米音樂與早期 QQ 客戶端都曾採用。

  • GitHub:https://github.com/Sephiroth87/ODRefreshControl
  • CocoaPod:pod 'ODRefreshControl', '~> 1.1.0'

圖片處理與快取

SDWebImage

iOS 上最主流的非同步圖片下載與快取方案,幾乎成了業界標準。

UIActivityIndicator-for-SDWebImage

為 SDWebImage 加上載入中的轉圈動畫。

  • GitHub:https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage
- (void)setImageWithURL:(NSURL *)url
usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
- (void)setImageWithURL:(NSURL *)url
      placeholderImage:(UIImage *)placeholder
usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;

UIImage+Resize

提供透明度、圓角、裁切、縮放等多種圖片處理方法。

  • GitHub:https://github.com/coryalder/UIImage_Resize
- (UIImage *)imageWithAlpha;
- (UIImage *)transparentBorderImage:(NSUInteger)borderSize;
- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize;
- (UIImage *)croppedImage:(CGRect)bounds;
- (UIImage *)resizedImage:(CGSize)newSize
   interpolationQuality:(CGInterpolationQuality)quality;

ImageCacheResize

整合 SDWebImage 與 UIImage+Resize,下載完成後可直接縮放或裁切再顯示。

  • GitHub:https://github.com/toptierlabs/ImageCacheResize
// 按比例縮放
[imageView setImageWithURL:url
                andResize:CGSizeMake(30, 30)
          withContentMode:UIViewContentModeScaleAspectFit];

// 裁切成 100x100
[imageView setImageWithURL:url andCropToBounds:CGRectMake(0, 0, 100, 100)];

ios-image-filters

提供多種圖片濾鏡效果。

  • GitHub:https://github.com/esilverberg/ios-image-filters

瀏覽與媒體展示

MWPhotoBrowser

支援本地與遠端圖片的瀏覽器,只要實作 MWPhotoBrowserDelegate 協定即可。

  • GitHub:https://github.com/mwaterfall/MWPhotoBrowser
@interface TestViewController () <MWPhotoBrowserDelegate>
{
    NSArray *_photos;
}
@end

@implementation TestViewController

- (void)doAction {
    NSMutableArray *photos = [NSMutableArray array];
    for (/* 每張圖片 */) {
        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:url]];
        photo.caption = description;
        [photos addObject:photo];
    }
    _photos = photos;

    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.displayActionButton = YES;
    UINavigationController *nc =
        [[UINavigationController alloc] initWithRootViewController:browser];
    nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:nc animated:YES completion:nil];
}

#pragma mark - MWPhotoBrowserDelegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}

- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser
            photoAtIndex:(NSUInteger)index {
    if (index < _photos.count) return [_photos objectAtIndex:index];
    return nil;
}

@end

MUKMediaGallery 與 PTShowcaseViewController

兩者都是媒體庫展示效果。前者支援圖片、視頻與音訊;後者支援格式更廣,還包含 PDF。

  • https://github.com/muccy/MUKMediaGallery
  • https://github.com/exalted/PTShowcaseViewController

PDF Reader Core for iOS

成熟的 PDF 閱讀核心。

  • GitHub:https://github.com/vfr/Reader

Blocks 與 KVO 增強

BlocksKit

為 Objective-C 常用類別加上 Block 語法支援,寫起來舒適很多。

  • GitHub:https://github.com/zwaldowski/BlocksKit
  • CocoaPod:pod 'BlocksKit', '~> 2.2.5'

KVOController

Facebook 開源的 KVO 增強庫,解決了原生 KVO 移除觀察者容易出錯的痛點。特性包括:支援 Blocks、自訂 Action 或 NSKeyValueObserving 回調;觀察者移除時不會拋出異常;控制器 dealloc 時自動移除觀察;改善 NSKeyValueObservingInitial 的效能;並具備執行緒安全與額外保護。

  • GitHub:https://github.com/facebook/KVOController
  • CocoaPod:pod 'KVOController', '~> 1.0.3'

pop

同樣出自 Facebook,做動畫的首選方案。

  • GitHub:https://github.com/facebook/pop
  • CocoaPod:pod 'pop', '~> 1.0.7'

富文本與加密

  • DTCoreText(Cocoanetics):支援 HTML 等富文本渲染 — https://github.com/Cocoanetics/DTCoreText
  • FTCoreText:富文本視圖 — https://github.com/FuerteInternational/FTCoreText
  • CoreTextWrapper:多欄文本視圖 — https://github.com/akosma/CoreTextWrapper
  • Base64(nicklockwood):字串 Base64 編碼 — https://github.com/nicklockwood/Base64
  • RNCryptor:AES 加解密 — https://github.com/rnapier/RNCryptor

其他值得收藏的經典庫

  • JSONKit / json-framework:JSON 編解碼
  • GTMBase64:Base64 編解碼
  • TouchXML:XML 解析 — https://github.com/schwa/TouchXML
  • SFHFKeychainUtils:將使用者密碼安全存入 Keychain
  • ASIHTTPRequest:HTTP 等協議封裝(較早期方案,新專案建議改用 AFNetworking)
  • EGOTableViewPullRefresh:下拉刷新 — https://github.com/enormego/EGOTableViewPullRefresh
  • AsyncImageView:非同步圖片載入與快取
  • Flurry:使用者行為統計
  • CorePlot:2D 圖表繪製
  • GData client:Google 服務相關類庫
  • RegexKitLite:正則表達式支援
  • CocoaAsyncSocket:TCP/IP 通訊 — https://code.google.com/p/cocoaasyncsocket/
  • ZBar:條碼掃描 — https://zbar.sourceforge.net/iphone/
  • PanoramaGL:720 全景展示
  • Three20:Facebook 的大型 UI 框架 — https://github.com/facebook/three20

App 上架後的營運工具

  • appannie:下載量與排名追蹤
  • Flurry:使用者行為分析
  • MajicRank:各國排行榜查詢
  • AppViz:銷售統計
  • PodViz:使用者評論檢視
  • appfigures:報表統計分析

最後提醒:引入第三方庫前務必確認其授權條款與維護狀態。上面部分專案已停止更新多年(例如 ASIHTTPRequest、Three20),新專案建議優先選擇仍在活躍維護的替代品,並鎖定版本號以免上游改動造成相容性問題。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved