2017-02-07 107 views
13

我寫的SDK iOS和我想驗證是否StoreKit.framework被鏈接到使用我的SDK應用程序,所以我跑:如何檢查iOS應用程序是否使用'StoreKit.framework'?

if ([SKStoreProductViewController class]) { 
     SKStoreProductViewController *storeController = 
         [[ SKStoreProductViewController alloc ] init ]; 
     // ... 
    } 

enter image description here

然而即使StoreKit.framework不掛[SKStoreProductViewController class仍然返回true

如何解決這個問題?


編輯1

爲@ x4h1d指出我創建新的空項目並添加到默認的控制器:

BOOL isStoreKitAvailable = 
    (NSClassFromString(@"SKStoreProductViewController") != nil); 

// => YES (there is no linked frameworks at all, why I get YES?) 

編輯2

我的供應教授ILE已In-App Purchase啓用(而不是項目本身)

從iOS應用程序的ID

enter image description here

然而在Xcode:

enter image description here

也許這是一個原因,甚至是空的應用有內置 StoreKit?

+0

怎麼樣'NSClassFromString(@ 「SKStoreProductViewController」)'? – matt

+0

我想這是問題所在。導入與加載不同。 _您已導入StoreKit。因此,實際上沒有嘗試使用StoreKit並崩潰,您無法發現StoreKit是否也實際上是_loaded_。 – matt

+0

@matt'NSClassFromString(@「SKStoreProductViewController」)'返回true – snaggs

回答

0

在鏈接框架和庫中,使其爲Optional而不是Required。所以,如果應用程序開發人員想要實現它,他會在應用程序中將該框架標記爲Required。現在如果你使用[SKStoreProductViewController class]。它可能會崩潰使用NSStringFromClass(@"SKStoreProductViewController")來確定它是否安全使用它。

+0

我的問題是:我如何檢測主機應用程序是否連接'StoreKit.framework'而不是 – snaggs

+0

要做到這一點,您可以使用弱鏈接SDK中的必需框架並使用NSStringFromClass(@「SKStoreProductViewController」)。 https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html –

0
//SWIFT 
class func allFrameworks() -> [AnyObject] 

//OBJECTIVE-C 
+ (NSArray *)allFrameworks 

從一個NSBundle

+allFrameworks 
Returns an array of all of the application’s bundles that represent frameworks. 

返回值

所有應用程序捆綁表示框架的數組。只包含其中包含一個或多個Objective-C類的框架。

import語句

import Foundation 

可用性

Available in iOS 2.0 and later. 

如何使用

(NSBundle.allFrameworks() - >返回項目所有框架使用的陣列。

您可以使用for檢查循環適用包含storeKit.framework或不如下。

夫特:---

func isStoreKitAvailable() -> Bool { 
    for frameWorkName in Bundle.allFrameworks { 
     if ((frameWorkName.classNamed("SKStoreProductViewController")) != nil) { 
      return true; 
     } 
    } 
    return false; 
} 

目標C:---

- (BOOL)isStoreKitAvailable { 

    for (NSBundle *frameWorkName in NSBundle.allFrameworks) { 
     if ([frameWorkName classNamed:@"SKStoreProductViewController"]) { 
      return YES; 
     } 
    } 
    return NO; 
} 

查找here

0

在Xcode中多個參考,

當我們在Capabilities下啓用In-App purchase時,Xcode自動鏈接StoreKit.frameworkAdd the In-App purchase feature to our App ID。同樣,如果我們的應用程序ID已啓用應用程序內購買,則會發生相同情況

因此,通過簡單地做,

BOOL isStoreKitAvailable = (NSClassFromString(@"SKStoreProductViewController") != nil); 

我希望這可以幫助你。

2

您可以使用以下代碼檢查storekit的可用性。

func checkStoreKitAvailibility() -> Bool { 
    for bundle in Bundle.allFrameworks { 
     if ((bundle.classNamed("SKStoreProductViewController")) != nil) { 
      return true; 
     } 
    } 
    return false; 
} 

編輯: 對於Objective-C中,你可以使用:

- (BOOL)checkStoreKitAvailibility { 

    for (NSBundle *bundle in NSBundle.allFrameworks) { 
     if ([bundle classNamed:@"SKStoreProductViewController"]) { 
      return YES; 
     } 
    } 
    return NO; 
} 
相關問題