LoginSignup
4
4

More than 5 years have passed since last update.

Bundle Seed ID を取得する方法

Posted at

仕事でBundle Seed IDだけを取得する必要が出て、いろいろ調べたのですが、ネット上にあまり情報がありませんでした。
かなりニッチな情報っぽいですが、せっかくなのでその方法をQiita初投稿のエントリとして共有することにします。

前提

  • Xcode 6.1.1
  • Swift 1.1

ポイント

Bundle Seed IDだけを取得するAPIは用意されていないようです。
そのため、Keychainに適当なデータを保存し、その登録結果(NSDictionary)に含まれるKeychainAccessgroupの値からBundle Seed IDを抜き出すということをしています。

実装

UIApplicationのextensionとして実装しました。

UIApplicationExtensions.swift
import UIKit
import Security

extension UIApplication {

    class var bundleSeedId: String {
        struct Static {
            static var token : dispatch_once_t = 0
            static var identifier: String!
        }

        dispatch_once(&Static.token) {

            let query: NSDictionary = NSDictionary(objects: [kSecClassGenericPassword, "Bundle Seed ID", kCFBooleanTrue], forKeys: [kSecClass, kSecAttrAccount, kSecReturnAttributes])

            var result: Unmanaged<AnyObject>?
            var status: OSStatus = SecItemCopyMatching(query as CFDictionaryRef, &result)

            if status == errSecItemNotFound {
                status = SecItemAdd(query as CFDictionaryRef, &result);
            }

            if status == errSecSuccess {
                if let op = result?.toOpaque() {
                    let resultDict: NSDictionary = Unmanaged<NSDictionary>.fromOpaque(op).takeUnretainedValue()

                    if let accessGroup = resultDict[kSecAttrAccessGroup as NSString] as? NSString {
                        let components = accessGroup.componentsSeparatedByString(".")
                        Static.identifier = components[0] as String
                    }
                }
            }
        }

        return Static.identifier
    }
}

サンプルコード

使い方はこんな感じです。

let bundleSeedId = UIApplication.bundleSeedId
NSLog("Budle Seed ID: \(bundleSeedId)")

P.S.
社内SDKを作っているような人間しかKeychainを使って複数のアプリでデータを共有なんてことはしないのかもしれませんね。

4
4
0

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
4
4