2016年3月23日水曜日

SwiftなCocoa Touch FrameworkでCommonCryptoを使う for iOS9.3

SwiftでCommonCryptoを使用する際、libcommonCryptoライブラリのリンクエラーが出たので対応方法を。

手順は次のようになります。

  1. CommonCryptoというディレクトリを作成
  2. module.mapというファイルを作成
  3. プロジェクト設定のBuild Settings -> Swift Compiler - Search Paths -> Import Pathsに、上記のCommonCryptoディレクトリを指定



次はサンプルです。 module.mapにSDK内のヘッダファイルのPathを記述します。

module CommonCrypto [system] {
    header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/CommonCrypto/CommonCrypto.h"
-    link "CommonCrypto"
    export *
}

link "CommonCrypto"を削除します。

2016年3月19日土曜日

[Swift]TableViewのSection HeaderをカスタムViewに置き換える

TableViewのSection HeaderをカスタムViewに置き換える方法です。



UITableViewHeaderFooterViewのサブクラスを作成

UITableViewHeaderFooterViewを継承したクラスを作成します。

import UIKit

class CustomTableViewHeaderFooterView: UITableViewHeaderFooterView {

    @IBOutlet weak var headerView: UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
}


Nibファイルを作成

Header用のViewを作成します。



TableViewにセット

Cellと同様にtableView.registerNibでセットしておきます。tableView:viewForHeaderInSectionでロードして戻り値とします。
注意点としては、ViewのサイズがAutolayoutになっていないので、frame.sizeでサイズ指定します。

class ViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let nib:UINib = UINib(nibName: "CustomTableViewHeaderFooterView", bundle: nil)
        tableView.registerNib(nib, forHeaderFooterViewReuseIdentifier: "CustomTableViewHeaderFooterView")
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    // header height
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    
    // header view
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header :CustomTableViewHeaderFooterView = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier("CustomTableViewHeaderFooterView") as! CustomTableViewHeaderFooterView
        header.headerView.frame.size = CGRectMake(0, 0, tableView.frame.size.width, 44).size
        
        return header
    }
    
}

[Swift]iOSでGoogle Places API for iOSを使う

Google Places APIをiOSで使う方法です。
公式サイトはこちらです。

Google Places API for iOS
https://developers.google.com/places/ios-api/?hl=ja


SDKの追加

SDKはCocoaPodで公開されています。Podfileに次の項目を追加します。

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.1'
pod 'GoogleMaps'
Podfileに追加の上、installを実行するだけで完了です。

pod install


API Keyの取得

Google Developer Consoleにて、APIの設定が必要です。次はAPI Keyを取得するまでの手順です。

  • プロジェクトを作成
  • Google Places API for iOS と Google Maps SDK for iOSを有効にする
  • 認証情報からiOSキーを作成(BundleIDの登録が必要です)
  • API Keyを取得する



API Keyのセット

AppDelegate.swiftで、GMSServicesクラスにAPI Keyのセットします。
import GoogleMapsが必要です。

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        
        GMSServices.provideAPIKey("YOUR_API_KEY")
        return true
    }


Place Autocompleteの使い方

ViewControllerからGMSAutocompleteViewControllerを起動します。
オートコンプレートによるPlaceの選択結果は、GMSAutocompleteViewControllerDelegateで受け取ることができます。

extension ViewController: GMSAutocompleteViewControllerDelegate {
    func startGooglePlacesAutocomplete(){
        let autocompleteController = GMSAutocompleteViewController()
        autocompleteController.delegate = self
        self.presentViewController(autocompleteController, animated: true, completion: nil)
    }
    
    // Handle the user's selection.
    func viewController(viewController: GMSAutocompleteViewController, didAutocompleteWithPlace place: GMSPlace) {
        print("Place name: ", place.name)
        print("Place address: ", place.formattedAddress)
        print("Place attributions: ", place.attributions)
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    func viewController(viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: NSError) {
        // TODO: handle the error.
        print("Error: ", error.description)
    }
    
    // User canceled the operation.
    func wasCancelled(viewController: GMSAutocompleteViewController) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    // Turn the network activity indicator on and off again.
    func didRequestAutocompletePredictions(viewController: GMSAutocompleteViewController) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    }
    
    func didUpdateAutocompletePredictions(viewController: GMSAutocompleteViewController) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    }
    
}


GMSAutocompleteViewControllerの起動が成功すると、次のような画面が表示されます。Placesの確定時にdidAutocompleteWithPlaceがコールされるので、引数:placeで値を受け取ります。

2016年3月5日土曜日

BottomSheetの実装方法 via Android Design Support Library 23.2

Android Design Support Library 23.2でBottomSheetが追加されました。
GoogleMapなどに用いられている画面が作成可能になりました。
実装はCoordinatorLayoutの子Viewに対して、Behaviorを指定するだけとシンプルです。

AOSPのソースコード : BottomSheetBehavior.java

Layoutのサンプル

BottomSheetはBehaviorを指定することで実装します。
CoordinatorLayoutの子ViewとしてLayoutを追加して、BottomSheet化したいLayoutに対してapp:layout_behavior="@string/bottom_sheet_behavior"を指定します。

<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/bottom_sheet_behavior"
            app:behavior_peekHeight="240dp"
            app:behavior_hideable="false"
            android:background="@android:color/white">
        <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="BottomSheet Sample"/>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

次のパラメータの指定が可能です。

  • app:behavior_peekHeight
    • BottomSheetの最小表示サイズ
  • app:behavior_hideable
    • 下スクロールで完全に非表示にするかどうか。非表示後に上スクロールで表示可能


2016年3月2日水曜日

[Swift]NavigationControllerのNavigationBarの表示 / 非表示方法

NavigationControllerを使っている際、画面上部のBarを表示したくないことがあります。
viewDidLoad()で表示/非表示するのではなく、viewWillAppear()とviewWillDisappear()で表示切り替えを行い、
画面遷移後のControllerに処理させないようにしました。

    override func viewWillAppear(animated: Bool) {
        self.navigationController?.navigationBarHidden = true
    }
    
    override func viewWillDisappear(animated: Bool) {
        self.navigationController?.navigationBarHidden = false
    }

2016年2月13日土曜日

Android M Fingerprint APIについて調べてみた

関西モバイル研究会で発表した内容です。

Android M FingerPrint(public)
http://www.slideshare.net/baroqueworksdev/android-m-finger-printpublic

簡易クラス図

アプリからHAL層までの簡易クラス図です。



アプリケーションから使用する場合

  • Context#getSystemService()でFragmentprintManagerにアクセス 
  • コールバックで結果を受け取る

FingerprintManagerクラス

  • アプリケーションから要求を受け取る
  • Android Frameworksのサービス群にある、FingerprintServiceに連携
  • 認証結果をFingerprintServiceから受け取り、アプリケーションに通知する

FingerprintServiceクラス

  • SystemServiceクラスを継承
  • Android Systemとして指紋認証機能を実行
  • Native(JNI->HAL)につなぐ
  • 認証要求時にFingerprintDeamonクラスをとおして、Native層に通知 アプリケーションの突然死の際、unbindする(IBinder.DeathRecipient)

JNI / HAL層

  • HAL層(ライブラリ)をとおして、Kernelに通知、ハードウェアの制御を行う
  • 認証結果をKeystoreServiceに通知

2015年10月22日木曜日

[Swift][GCM]iOSでGoogle Cloud Messageを使う

iOSでGoogle Cloud Message 3.0を使ったので、はまりどころのメモです。
基本的なことは本家サイトを参照ください。
https://developers.google.com/cloud-messaging/ios/client?ver=swift

APNs用にデータを追加する

データ構造はGCM 2.0に以下のAPNsでも使用できるパラメータを追加すればデバイスに届きます。
  "content_available" : "true"
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark"
    }

もし、デバイスに届かない場合はcontent_availableの値を確認しましょう。"true"というStringではなく、true / falseのBoolが正しいです。

Push受信しても鳴動しない

notificationにsoundパラメータを追加しましょう。
https://developers.google.com/cloud-messaging/http-server-ref
https://developer.apple.com/jp/documentation/RemoteNotificationsPG.pdf

アプリ起動していない状態でバックグラウンド受信しない

priorityの追加
priorityパラメータを追加して、10を指定しましょう。
https://developers.google.com/cloud-messaging/http-server-ref
https://developer.apple.com/jp/documentation/RemoteNotificationsPG.pdf
iOSアプリ側の設定
Background fetch modeをONにしましょう。
AppDelegateにて、MinimumBackgroundFetchIntervalをセットします。DefaultはNeverになっているため、Background fetchしません。
  application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)