简体   繁体   English

如何防止 function 被其他捆绑包 id 调用

[英]How to prevent function to being called by other bundle ids

can I restrict function call by bundle id我可以通过捆绑 ID 限制 function 调用吗

For example I have created extension for string例如,我为字符串创建了扩展名

 extension String {
        func trim() -> String {
        if let bundle = Bundle.main.bundleIdentifier {
            if bundle.hasPrefix("xyz") {
                return self.trimmingCharacters(in: .whitespacesAndNewlines)
            }
            else {
                print("Framework Restricted")
                return  ""
            }
        }
    }
}

It gives error at runtime but i want prevent user to access the trim() funtions at compile time remember source is not visible to user it's static framework它在运行时出错,但我想阻止用户在编译时访问 trim() 函数记住源对用户不可见,它是 static 框架

Issue: I have too many function so i have to put condition in all of the istead i want to code something to make it easy.问题:我有太多的 function 所以我必须在所有的地方都加上条件,我想编写一些代码让它变得容易。

Since frameworks are precompiled, you can't have a compile time error.由于框架是预编译的,因此不会出现编译时错误。 You should have a runtime error.您应该有一个运行时错误。

Make a global function and call it where ever you need:制作一个全局 function 并在您需要的任何地方调用它:

func crashIfStolen() {
    if Bundle.main.bundleIdentifier != myBundle {
        fatalError("Unauthorized use of the framework")
    }
}

NSObject has a method called load that automatically gets called on load of the framework (just by being in the app). NSObject有一个名为load的方法,它会在框架加载时自动调用(只是在应用程序中)。

class X: NSObject {
    override class func load() {
        crashIfStolen()
    }
}

But

Method 'load()' defines Objective-C class method 'load', which is not permitted by Swift.方法“load()”定义了 Objective-C class 方法“load”,这是 Swift 不允许的。

So you can have an Objective-C class in your framework with this function to make it dynamically restricted.因此,您可以使用此 function 在您的框架中使用 Objective-C class 以使其动态受限。 (Swift is static) (斯威夫特是静态的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM