xcode iPhone私有API编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21970650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone private API compiling
提问by Max Ka
I'm searching through the whole internet since a few hours now, and I just can't find the informations I'm looking for. I would like to mess around with the private apis, see whats possible, etc., but I can't even compile something.
几个小时以来,我一直在整个互联网上搜索,但找不到我要找的信息。我想弄乱私有 api,看看有什么可能,等等,但我什至无法编译一些东西。
So I've got a few pretty basic questions:
所以我有几个非常基本的问题:
- Do I have to dump the headers? Because I downloaded a sample, where the API is loaded with
- 我必须转储标题吗?因为我下载了一个示例,其中加载了 API
char *framework = "/System/Library/PrivateFrameworks/...";
dlopen(...);
I would like to use the objc-syntax (if possible) rather than using C (as mentioned above), if there are any opportunities.
如果有任何机会,我想使用 objc 语法(如果可能)而不是使用 C(如上所述)。
- How do I make Xcode compile, if I import the private APIs? Do I have to add other link flags? (because I read about two different opinions) I added a private framework and created a new Folder "Headers" and put all the headers files in there, so the framework shows up correctly in Xcode. Do I have to import the whole .framework, or only the headers from the framework I would like to use? After I imported the framework, I get 20+ errors, unknown type names, and many more.
- 如果导入私有 API,如何编译 Xcode?我是否必须添加其他链接标志?(因为我读到了两种不同的意见)我添加了一个私有框架并创建了一个新文件夹“Headers”并将所有头文件放在那里,因此该框架在 Xcode 中正确显示。我必须导入整个 .framework,还是只导入我想使用的框架中的标头?导入框架后,我收到 20 多个错误、未知类型名称等等。
And, finally, I've read about entitlements (which seem to be new in iOS 7). How do I use these entitlements and when do I use them?
最后,我已经阅读了有关权利的内容(这似乎是 iOS 7 中的新内容)。我如何使用这些权利以及何时使用它们?
Could someone please just type a few lines as an example?
有人可以输入几行作为示例吗?
回答by Nate
Background
背景
In order to use methods in anyframework, you can choose to either reference those frameworks statically or dynamically. I haven't seen anything in your question that suggests you need to use dynamiclinking, so I'm going to avoid that (it's slightly more complicated for a beginner). (‡)
为了在任何框架中使用方法,您可以选择静态或动态引用这些框架。我在您的问题中没有看到任何表明您需要使用动态链接的内容,所以我将避免这种情况(对于初学者来说稍微复杂一些)。(‡)
To statically reference APIs in a framework, you would import the relevant headers, and then configure your Xcode project to link to the framework. These two steps only change slightlyfor PrivateAPIs.
要在框架中静态引用 API,您需要导入相关的头文件,然后配置您的 Xcode 项目以链接到框架。对于私有API,这两个步骤仅略有变化。
Private APIs usually don't provide you with the headers (*.h) that describe the APIs. I say "usually", because sometimes, an API that's private on iOS is actually public on Mac OS X, so to use it, you simply copy the OS X version of the header into your project.
私有 API 通常不会为您提供描述 API 的标头 (*.h)。我说“通常”,因为有时,在 iOS 上私有的 API 在 Mac OS X 上实际上是公共的,因此要使用它,您只需将标头的 OS X 版本复制到您的项目中。
Generating Headers
生成标题
Probably more common, though, is that you have to generatethe header yourself. If you know which header you need, often you can find it posted online under someone's github account. If not, you need a tool like class-dump, or class-dump-z. Run the class dump tool on the private framework, by finding it on your Mac:
不过,可能更常见的是您必须自己生成标题。如果您知道需要哪个标题,通常可以在某人的 github 帐户下在线找到它。如果没有,您需要一个类似class-dump或class-dump-z 的工具。通过在 Mac 上找到它,在私有框架上运行类转储工具:
cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/System/Library/PrivateFrameworks/
class-dump -H -o ~/Headers/7.0/MusicLibrary/ MusicLibrary
Then, go into ~/Headers/7.0/MusicLibrary/
and find lots of dumped header files. Copy (only) the header(s) you need into your Xcode iOS project directory. Then, from inside Xcode, right click on a source folder in your Project Navigator view, select "Add files to <Project Name> ...". Pick the dumped header file you need to include in your project.
然后,进入~/Headers/7.0/MusicLibrary/
并找到大量转储的头文件。将(仅)您需要的标题复制到您的 Xcode iOS 项目目录中。然后,从 Xcode 内部,右键单击 Project Navigator 视图中的源文件夹,选择“Add files to <Project Name> ...”。选择需要包含在项目中的转储头文件。
Linking
链接
In order to successfully link against the API, you also need to add the framework to your Xcode Build Phases. From your project Targetsettings, select Build Phasesthen Link Binary with Libraries. You normally choose a public framework from the default list that the iOS SDK provides you. However, you can choose to browse your Mac for 3rd-party frameworks, or private frameworks, too. For private frameworks, you're just going to have to navigate to a folder location like this
为了成功链接 API,您还需要将框架添加到您的 Xcode 构建阶段。从您的项目目标设置中,选择Build Phases然后Link Binary with Libraries。您通常从 iOS SDK 为您提供的默认列表中选择一个公共框架。但是,您也可以选择在 Mac 上浏览 3rd 方框架或私有框架。对于私有框架,您只需要导航到这样的文件夹位置
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/System/Library/PrivateFrameworks/
and then pick the *.framework
directory.
然后选择*.framework
目录。
Then, simply use the APIs like you would use any public/private API. #import
the header file, call the APIs, instantiate the classes, etc.
然后,只需像使用任何公共/私有 API 一样使用 API。 #import
头文件、调用 API、实例化类等。
The use of this code:
这段代码的使用:
char *framework = "/System/Library/PrivateFrameworks/...";
dlopen(...);
is an attempt to dynamicallyopen a private framework. That's not necessary, if you know at compile time which framework you want to use, and have it present on your Mac to let Xcode link against.
是一种动态开放私有框架的尝试。如果您在编译时知道要使用哪个框架,并且将它存在于您的 Mac 上以让 Xcode 链接,那么这不是必需的。
Entitlements
权利
Entitlements are not new to iOS 7. They have existed for quite some time, and are one technique iOS uses to prevent unauthorized usage of some private APIs. iOS will check to see if your app has been granted a particular entitlement (by name), and if it does not have that entitlement, calling the protected API will fail (usually silently, although sometimes you'll see a message in the Console log).
权利对于 iOS 7 来说并不新鲜。它们已经存在了很长一段时间,是 iOS 用来防止未经授权使用某些私有 API 的一种技术。iOS 将检查您的应用程序是否已被授予特定权利(按名称),如果它没有该权利,调用受保护的 API 将失败(通常是静默的,尽管有时您会在控制台日志中看到一条消息)。
See here for an example of granting your (jailbreak) app an entitlement.
(‡) Update:iOS 9.3 has brought some changes with respect to Private APIs, and static vs dynamic linking. Please see this Stack Overflow question here for more.
(‡) 更新:iOS 9.3 在私有 API 以及静态与动态链接方面带来了一些变化。请在此处查看此堆栈溢出问题以了解更多信息。