如何为脚本禁用 App Transport Security?

How to disable App Transport Security for a script?

我正在编写一个应用程序,它是一个 python 脚本,它使用 pyobjc 绑定来使用 mac 上的 objc/cocoa 运行时。 该应用程序(Présentation.app,mac 的 pdf 演示工具)使用 WebView 显示任意网页。

自 Mac OS X 10.11 起,App Transport Security 阻止加载任何非 https 网站。 可以通过在 Info.plist 中设置 NSAppTransportSecurity 字典来禁用此功能(以重新启用以前的行为,即任意网页加载),如 here.

所述

但是,我的应用程序是一个脚本,可以从命令行使用而无需捆绑到 .app 中。 在这种情况下,它没有 Info.plist.

我读到我可以 add an Info.plist to a binary command line tool using the linker,但我需要 python 脚本的解决方案…

关于如何将 Info.plist 嵌入到脚本中有什么建议吗? 或任何通过 API 而不是 Info.plist 文件配置 App Transport Security 的解决方案?

好的,我找到了一种从脚本本身禁用 App Transport Security 的方法。 事实证明,您可以在启动后从脚本中编辑信息字典,并且会考虑更改。 所以在脚本的开头,我添加了:

from objc import YES
from AppKit import NSBundle
bundle = NSBundle.mainBundle()
info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
info['NSAppTransportSecurity'] = {'NSAllowsArbitraryLoads': YES}

就是这样。