【2018年版】iOSアプリ新規で作る時にやること①
これ年に10回くらいはやるんだけど、どうにかならんのかなぁ。シェルスクリプト書けばいけそうだけど、Xcodeについてくのだるいし、労力と比較して迷う。
Carthage / RxSwift / Compass / XCGLogger / Reachability / Alamofire / R.swift / SwiftLint / Generamba / VIPER / fastlaneあたりが技術キーワードでしょうか。箇条書きでいきまっせ。
ぼくはもうCarthage対応してないやつは使わないので、Cocoapodsは出てきません。
①プロジェクト作成~設定・各種ライブラリ導入編です。
Apple Developerアカウントもらう
誰かにもらいましょう
App ID決める
誰かに決めてもらいましょう
PROJECT作る
作りましょう。
.gitignore投入
### https://raw.github.com/github/gitignore/160d27e2bebf784c4f4a1e070df057f3868b62bc/Objective-C.gitignore # Xcode # # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore ## Build generated build/ DerivedData/ ## Various settings *.pbxuser !default.pbxuser *.mode1v3 !default.mode1v3 *.mode2v3 !default.mode2v3 *.perspectivev3 !default.perspectivev3 xcuserdata/ ## Other *.moved-aside *.xcuserstate ## Obj-C/Swift specific *.hmap *.ipa *.dSYM.zip *.dSYM # CocoaPods # # We recommend against adding the Pods directory to your .gitignore. However # you should judge for yourself, the pros and cons are mentioned at: # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control # # Pods/ # Carthage # # Add this line if you want to avoid checking in source code from Carthage dependencies. Carthage/Checkouts Carthage/Build # fastlane # # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the # screenshots whenever they are needed. # For more information about the recommended setup visit: # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md fastlane/report.xml fastlane/Preview.html fastlane/screenshots fastlane/test_output # Code Injection # # After new code Injection tools there's a generated folder /iOSInjectionProject # https://github.com/johnno1962/injectionforxcode iOSInjectionProject/ *.generated.swift vendor/bundler
PROJECT→Info
iOS Deployment Target
誰かに決めてもらいましょう
Configurations
Debug→ProductionDebug
Release→ProductionRelease
に変更して
それぞれをDuplicateして
StagingDebugとStagingReleaseを作る。
Developが必要ならDevelopDebugとDevelopReleaseも。
でもDevelopはあんま作りたくない。なんでiOSエンジニアがローカルにバックエンドの開発環境作らなきゃいけないのかうんぬんかんぬん。
Productionも正直用意するか迷う。何人かで開発するときはいらないかも。
Schema
デフォルトでできてるやつにsuffix:_Productionをつける
Duplicateして_Stagingを作る
両方Sharedにする
_Stagingのほうを編集。Build ConfigurationをStagingDebugとStagingReleaseに変更。
PROJECT→Build Settings
Versioning
Current Project Version:1
Versioning System:Apple Generic
に変更。
Swift Compiler - Custom Flags→Other Swift Flags
ProductionDebug:-D DEBUG
StagingDebug: -D DEBUG -D STAGING
追加。
TARGETS→General
Identity→Version
Signing
Deployment Info
をいじる
Deployment Info→Main Interfaceはクリアして、Main.storyboardを削除。
TARGETS→Capabilities
必要なのをオン
TARGETS→Info
Bundle Name:$(BundleNamePrefix)アプリ名
TARGETS→Build Settings
User-DefinedにBundleNamePrefixとConfigurationNameを追加。Stagingだけ設定をお好みで追加。
Packaging→Product Bundle Identifier:[お好みで]$(ConfigurationName)
SwiftLint導入
インストール。グローバルにいれるのすきじゃないんだけど、Build Phaseで使いたいのでまぁしゃーない。もう、homebrewのインストールはいいっしょ。。このブログ内検索すればあるはず。
brew install swiftlint
TARGETS→Build PhaseでNew Run Script Phase
#!/bin/sh if which swiftlint >/dev/null; then swiftlint autocorrect swiftlint else echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint" fi
で、プロジェクトルートに.swiftlint.ymlを用意。
included: - [Project Module Nameを] excluded: - Pods/ - Carthage/ - fastlane - [Project Module Nameを]/Application/R.generated.swift disabled_rules: - force_cast - force_try - force_unwrapping file_length: warning: 1000 error: 1500 type_body_length: warning: 500 error: 750 function_body_length: warning: 200 error: 300 line_length: warning: 200 error: 300 variable_name: min_length: 1
で、ビルドエラーになったら修正。
Carthage導入
インストール。
brew install carthage
プロジェクトルートにCartfileを作る。基本は以下。
バージョンは2018/06現在の最新。
# General github "ReactiveX/RxSwift" "4.2.0" github "hyperoslo/Compass" "6.0.0" # Resources github "mac-cain13/R.swift.Library" "v4.0.0" # Debug github "DaveWoodCom/XCGLogger" "6.0.4" # API github "ashleymills/Reachability.swift" "v4.1.0" github "Alamofire/Alamofire" "4.7.2"
作れたら
carthage update --cache-builds --platform iOS
Cartfileを変更したら毎回これやりましょう。初回はものすごい時間がかかりまっせ。
TARGETS→General→Linked Frameworks and Librariesで+→Add Other...で
Carthage/Build/iOSの下にできた.frameworkたちを追加。
TARGETS→Build PhaseでNew Run Script Phase
/usr/local/bin/carthage copy-frameworks
Input Filesにさっき入れたFrameworkを全て追加。。。
・7/1追記
github.com
公式の説明の通り、ドラッグ&ドロップだとなんかうまくいかなかった。
たぶんOutput Filesもちゃんと書いたほうがよい気がする
R.swift導入
github.com
ここから任意のバージョンのzipをダウンロードして解凍。
rswiftをプロジェクト内のお好みの場所に。
TARGETS→Build PhaseでNew Run Script Phase
これはComple Sourcesより前にやったほうがよい(上から順番に実行されるから上のほうにおくとよい)と思う。
"$PROJECT_DIR/rswiftのパス" generate "$PROJECT_DIR/R.generated.swiftを置きたいディレクトリのパス(.swiftlint.yml参照)"
ビルドするとR.generated.swiftができるので、プロジェクトに追加しておきましょう。