美文网首页程序员
Sandbox App使用第三方Binary文件

Sandbox App使用第三方Binary文件

作者: xiao_A | 来源:发表于2016-04-20 20:05 被阅读2408次

    所谓sandbox App是指那些为了发布到App Store的,受到权限控制的App。一般Sandbox App拥有如下权限

    Paste_Image.png

    一波三折 之一

    项目需要我们在符合Sandbox权限约束下的App中使用第三方的一个开源项目的Binary。并在之中调用这个Binary以进行一些功能上的实现。

    于是,我兴高采烈地直接拿到了local build版本的这个Binary,以下简称N文件。我通过包一个带UI的Sandbox App的方式,通过NSTask调用了这个Binary,一切功能运行正常。
    于是乎直接申请对应App ID,上传之,准备发布。
    然而,正以为一切都尽在掌控之际,陡生变故。在使用Xcode上传时,连第一步的Validate都未通过。并且错误显示我的这个N文件无Sandbox权限
    经过研究,原来,在Sandbox下的可独立执行的文件都必须符合Sandbox权限管控。可是,这是一个Command Line Tool的项目啊,这个如何加入Sandbox权限呢?

    一波三折 之二

    经过研究,发现可以通过

    codesign --entitlements ./entitlements.plist -s "copy & paste your certificate from keychain" ./commandlinetool

    来将我们手动创建的entitlements文件(记录了app的sandbox权限信息)sign进对应的commondlinetool
    请参考Stackoverflow原贴

    以为一切就如此顺利的搞定了。刚准备为自己的运气而庆幸,直接运行,出现

    Illegal instruction: 4

    这是因为我们为一个原本不受权限控制的可执行文件强行添加权限之后无法执行。
    再次研究,从Apple官方文档中找到

    Add the following arguments to your linker flags:
    -sectcreate __TEXT __info_plist Info.plist_path

    Info.plist 是记录一个SandboxApp 相关Bundle信息的文件,我一拍脑袋。我只顾为一个Binary进行权限分配,而没有为这个Binary包相关的Bundle信息,那么这个程序在运行时由于缺少Bundle信息,在Sandbox 化时系统无法为其生成相关的Container。根据文档,我修改了Makefile,添加了Linkflag

    一波三折 之三

    折腾了一天,终于可以下班了吧。在Terminal中完美运行了这个Sandbox化的N文件之后,我把它重新包进了之前带UI的Sandbox App。然而,运行后程序毫无反馈。调试后发现,既无报错也无Console。因为正常如果有任何异常都会有Console报错,如果能够运行,Console中会有运行中的Log信息。

    欲哭无泪,毫无头绪。

    经过多方求助终于在Apple的一个developer forum回帖中发现了一个相关回答
    其中,有价值的两点

    1.Sandbox inheritance (com.apple.security.inherit)
    2.converting your tool to some sort of script (a shell script, Python, whatever).

    先尝试了包一个shell,发现在Sandbox的app中调用bash去run 自己写的shell,再通过这个shell去run我们的binary是不可行的,报错:operation not permitted

    再看第二点,查Apple 文档和具体的这个com.apple.security.inherit权限文档
    深入研究后发现,

    To enable sandbox inheritance, a child must use exactly two App Sandbox entitlement keys: com.apple.security.app-sandbox and com.apple.security.inherit. If you specify any other App Sandbox entitlement, the system aborts the child process. You can, however, confer other capabilities to a child process by way of iCloud and notification entitlements.

    也就是说,我们的N文件现在是作为我们包的UI App的子项,于是我们需要让这个N文件继承UI App的Sandbox权限,而不是把它自己作为一个独立的Sandbox App。
    于是,

    1. 去掉Makefile中之前添加的linkflag
    2. 将entitlement文件改为

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.inherit</key>
    <true/>
    </dict>
    </plist>

    重新make,然后codesign,并把新N文件包进去

    黎明

    终于,这个程序正常了,并且N文件和外包的UI App公用同样的Bundle信息,同一个Container,使用同样的Sandbox权限。

    总结

    整个过程一波三折,每当有进展之后以为圆满解决了又来了更大的问题。同时,相关资料非常少,我全程查英文第一手资料,但是无奈Apple文档目录太深,Google也帮不了我。好在各种机缘和强大的同事,我们一步一步的猜想,实验,破解了谜团。
    App已经提交了,也许Apple不允许这样的通过N文件的方式进行包装,不过在这个过程中对Sandbox权限和项目构建有了更深的理解。

    相关的中文资料基本没有,希望能够给同样在坑中的人们带来一些参考。

    相关文章

      网友评论

        本文标题:Sandbox App使用第三方Binary文件

        本文链接:https://www.haomeiwen.com/subject/hettrttx.html