如何检查iOS应用程序是否使用了'StoreKit.framework'?

14

我正在为iOS编写SDK,我想要验证使用我的SDK的应用程序是否链接了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

我的配置文件启用了应用内购买(不是项目本身)

来自iOS App ID:

enter image description here

然而,来自Xcode:

enter image description here

也许这就是为什么即使空应用程序也有内置的StoreKit的原因?

NSClassFromString(@"SKStoreProductViewController") 怎么样? - matt
@matt NSClassFromString(@"SKStoreProductViewController") 返回 true。 - snaggs
@matt,我需要验证应用程序是否使用了StoreKit - 如果是则打开内部AppStore,否则打开浏览器。但是我仍然没有找到解决方案。 - snaggs
@x4h1d,请查看我的编辑,它仍然返回“YES”。 - snaggs
你能否使用终端执行“xcodebuild”命令,分别测试不链接storekit.framework和链接“storekit.framework”的情况?请在完成后将输出发送给我。谢谢。 - manishg
显示剩余7条评论
4个回答

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;
}

0
在链接的框架和库中,将其设置为可选而不是必需。这样,如果应用程序开发人员想要实现它,他将在应用程序中标记框架为必需。现在,如果您使用[SKStoreProductViewController class],它可能会崩溃,请使用NSStringFromClass(@“SKStoreProductViewController”)来确定是否可以安全地使用它。

我的问题是:如何检测主机应用程序是否链接了 StoreKit.framework - snaggs
要实现它,需要在SDK中弱链接所需的框架,并使用可用的NSStringFromClass(@“SKStoreProductViewController”)。https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html - Sachin Vas

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 Foundation

可用性

Available in iOS 2.0 and later.

如何使用

(NSBundle.allFrameworks() -> 返回项目中使用的所有框架的数组。

您可以使用for循环检查应用程序是否包含storeKit.framework,如下所示。

Swift :---

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

Objective C:

- (BOOL)isStoreKitAvailable {

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

这里查找更多参考资料。


0
在Xcode中,
当我们在“Capabilities”下启用“应用内购买”时,Xcode会自动链接“StoreKit.framework”并将“应用内购买功能添加到我们的App ID”中。同样,如果我们的App ID已经启用了应用内购买,则会发生相同的情况。
因此,只需这样简单地操作即可,
BOOL isStoreKitAvailable = (NSClassFromString(@"SKStoreProductViewController") != nil);

希望这能对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接