Skip to content

Instantly share code, notes, and snippets.

View kitasuke's full-sized avatar

Yusuke Kita kitasuke

View GitHub Profile
@kitasuke
kitasuke / ObjectMapping
Created January 17, 2015 08:31
Object Mapping with RestKit not using URL connection
NSHTTPURLResponse *httpurlResponse = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"/hoge/"]
statusCode:200
HTTPVersion:nil
headerFields:@{}];
RKResponseMapperOperation *responseMapperOperation = [[RKResponseMapperOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"/hoge/"]]
response:httpurlResponse
data:[NSData new]
responseDescriptors:@[
[RKResponseDescriptor responseDescriptorWithMapping:responseMapping
@kitasuke
kitasuke / KIF.md
Last active December 28, 2015 00:52
KIF導入

KIF iOS Integration Testing Framework

特徴

導入方法

テスト用ターゲットの追加

テスト用のターゲットを作成する必要があります。 ユニットテストを有効にした場合は、MyApplication_Testsという名前のターゲットが既に存在しているかもしれません。 その場合に、もしそのターゲットをユニットテスト用に使用しないのであれば、そのターゲットをKIF用に使用しても構いません。 それ以外の場合は、以下の手順で新規にターゲットを作成してください。

  1. Xcode上でプロジェクトやターゲット選択欄から、ターゲットの追加を押す

概要

iOS9からApp Transport Security(ATS)が追加されました。 ATSとは安全なネットワーク接続を実現するための機能であり、Appleの要件を満たしていない場合は接続失敗という扱いになります。

対応方法

対象となるのは、自社内サーバ、WebView経由、サードパーティライブラリ(SNS、広告、分析など)だと思います。 ATS自体を無効にするのはあまり良いやり方ではないので、これらのドメインをATSの例外に設定するのが一般的なやり方だと思います。

設定方法

設定方法はInfo.plistに指定の情報をセットするだけです。 しかし、プロジェクトのTargetが多かったりConfigurationによって設定を分けたい場合には、Info.plistを個別に用意して管理する必要がありますが、無駄にInfo.plistファイルが増えるので良い方法ではないと思います。

Protocol BuffersをAPIKitとKituraで試してみる

この記事では、Protocol BuffersをSwiftでどう使うかサンプルアプリを使って説明します。Protocol Buffersの詳細はこちらの記事を先に読むと大体のイメージが掴めると思います。個人的には Protocol Buffersのメリットは型安全に書けて高速な通信が実現する ことだと思います。

サンプルアプリ

今回のサンプルアプリは、クライアント側はAPIKit、サーバ側はKituraで 通信処理を実装します。サンプルアプリはGitHubで公開しているので、こちらからダウンロードして試してみてください。

また、URLSessionで実装したサンプルアプリもこちらに用意しました。URLSessionだけでも楽に実装できるので、こちらも参考にしてみてください。

import UIKit
protocol StoryboardInstantiable {
static var storyboardName: String { get }
static var storyboardIdentifier: String { get }
}
extension StoryboardInstantiable where Self: UIViewController {
static var storyboardName: String { return String(describing: self) }
static var storyboardIdentifier: String { return String(describing: self) }
import Foundation
protocol UserDefaultsStorable {
associatedtype UserDefaultsKey: RawRepresentable
static func userDefaultsValue(forKey key: UserDefaultsKey) -> Bool?
static func userDefaultsValue(forKey key: UserDefaultsKey) -> Int?
static func userDefaultsValue(forKey key: UserDefaultsKey) -> Int64?
static func userDefaultsValue(forKey key: UserDefaultsKey) -> Double?
static func userDefaultsValue(forKey key: UserDefaultsKey) -> String?
import UIKit
extension UICollectionView {
func registerNib<T: UICollectionViewCell>(forCellType type: T.Type) {
let name = String(describing: type)
let nib = UINib(nibName: name, bundle: nil)
register(nib, forCellWithReuseIdentifier: name)
}
func registerNib<T: UIView>(forSupplementaryViewOfKind elementKind: String, withType type: T.Type) {
import Foundation
import APIKit
protocol ProtobufRequest: Request {}
extension ProtobufRequest {
var dataParser: DataParser {
return ProtobufDataParser()
}
}

Type-safe API call with Protocol Buffers in Swift

Apple recently open sourced swift-protobuf which is a plugin of Protocol Buffers for swift language. Protocol Buffers in Swift enables us to have type safety, make API faster and unify schema documentation of structured data. I had a chance to use swift-protobuf in my project and thought that there are many benefits for us, so I would like to share my thoughts.

I also created a repository which has sample server/client app with Protocol Buffers. Please take a look here if you're interested in what implementation looks like.

What's Protocol Buffers?

import UIKit
import RxSwift
import RxCocoa
extension Reactive where Base: UITableView {
func items<S: Sequence, Cell: UITableViewCell, O: ObservableType>
(cellType: Cell.Type)
-> (_ source: O)
-> (_ configureCell: @escaping (Int, S.Iterator.Element, Cell) -> Void)
-> Disposable