2017-02-07 108 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; 
} 
相关问题