LoginSignup
10
9

More than 5 years have passed since last update.

Deployment Target 7.0 での Swift 開発

Last updated at Posted at 2015-06-23

(追記)

コメントにも書いてある通り、よくよく調べてみるとビルドはできるが、申請は無理らしい

In the meantime, I will point out that it is only supported to use an embedded framework on iOS 8. The only question is whether you could use dlopen() to conditionally load the embedded framework on iOS 8, and not load the embedded framework on earlier versions of iOS.

前置き

iPhone ユーザーは OS のアップデートに対して積極的に取り組んでくれるが、まだまだ レガシー OS に対して対応しなければならないことがあると思う。

しかし、Deployment Target 7.0 での Swift の開発は非常に厄介で、CocoaPods による Swift ライブラリの導入ができない。そこでライブラリのソースファイルをプロジェクトにドラッグアンドドロップで入れるというお粗末な導入がしばしば見受けられる。これによる弊害として

  • 名前空間が使えない
  • バージョン管理下にライブラリのソースコードが入る(submoduleで解決するが...)

特に名前空間を使えないのは、非常に困る。Swift の名前空間は C++ のような明示的なものではなく Embedded Framework 単位で名前空間 1 が分かれている。そのため、同じターゲットに対してドラッグアンドドロップで Swift のライブラリを追加していくと、まれにクラス名が被ってコンパイルできないことがある。Carthage 2 を使うなど対応方法があるが、多くのライブラリが CocoaPods を使用しているという状況。

そこで、自分の環境では Embedded Frameworkgit submodule を利用して名前空間を利用しながら Swift で iOS の開発をしているのでその紹介をする。

手法

構成

CocoaPods (Obj-Cのライブラリのみ使用)を利用した環境を想定する。
ここに./Vendor./Vendor/Checkoutsというディレクトリを作成しておく。

➜ tree -L 1
.
├── Deploy7Sample
├── Deploy7Sample.xcodeproj
├── Deploy7Sample.xcworkspace
├── Deploy7SampleTests
├── Podfile
├── Podfile.lock
├── Pods
└── Vendor
    └── Checkouts

6 directories, 2 files

手順

Alamofire を導入してみる。まずは submodule で ./Vendor/Checkouts に checkout

➜ git submodule add git@github.com:Alamofire/Alamofire.git Vendor/Checkouts/Alamofire
Cloning into 'Vendor/Checkouts/Alamofire'...
remote: Counting objects: 1741, done.
remote: Compressing objects: 100% (52/52), done.
remote: Total 1741 (delta 23), reused 0 (delta 0), pack-reused 1689
Receiving objects: 100% (1741/1741), 930.78 KiB | 744.00 KiB/s, done.
Resolving deltas: 100% (982/982), done.
Checking connectivity... done.

次にプロジェクトを開く

➜ open Deploy7Sample.xcworkspace

まずは File -> New -> Target から Cocoa Touch Framework を選択

スクリーンショット 2015-06-23 15.57.55.png

スクリーンショット 2015-06-23 16.17.17.png

Framework の名前は Alamofire にしておく

スクリーンショット 2015-06-23 15.59.24.png

すると AlamofireAlamofireTests というターゲットとグループができるので Unit Test の AlamofireTests どっちも使わないので消しておく。

スクリーンショット_2015-06-23_15_59_42.png

./Alamofireディレクトリは./Vendorの中に移動し、

mv Alamofire Vendor/Alamofire

グループVendorとその子供にグループAlamofireを追加する。

スクリーンショット 2015-06-23 16.01.44.png

このままだとグループとディレクトリ構成が合わないので、グループVendorAlamofireのロケーションを変更しておく。

  • グループVendor -> ./Vendor
  • グループVendor/Alamofire -> ./Vendor/Alamofire

にロケーションを変更

スクリーンショット_2015-06-23_16_01_54.png

スクリーンショット 2015-06-23 16.03.53.png

グループAlamofireを移動させるとinfo.plistが行方不明になるので、info.plistをダイアログから選択しておく。

スクリーンショット 2015-06-23 16.04.24.png

Alamofireのソースファイルをプロジェクトに追加する。
この時にCopy items if neededのチェックを外しておくこととAdd to targetsのチェックをAlamofireに変更しておくことを忘れずに

スクリーンショット 2015-06-23 16.07.58.png

スクリーンショット 2015-06-23 16.08.17.png

Deployment Target を 7.0 に設定する

Untitled.png

あとはViewControllerに次のサンプルコードを書いて終了

import UIKit
import Alamofire

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        Alamofire.request(.GET, "http://httpbin.org/get")
            .response { (request, response, data, error) in
                println(request)
                println(response)
                println(error)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

スクリーンショット 2015-06-23 16.10.32.png

参考URL

  1. Swiftの名前空間とは

  2. https://github.com/Carthage/Carthage

10
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9