safari 扩展_为什么构建Safari应用扩展程序是2020年的噩梦

safari 扩展

Safari App Extensions is the new way of Apple to develop Safari Extensions along with Cocoa Mac apps. Integrated into the App Extensions environment, it was a logical step to harmonize the ways to extend your Mac apps’ functionalities.

Safari App Extensions是Apple与Cocoa Mac应用程序一起开发Safari Extensions的新方法。 集成到App Extensions环境中,这是协调扩展Mac应用程序功能的方法的逻辑步骤。

It came with a promising and innovative tech stack for a browser extension: a Swift/Javascript combination rather than a full Javascript experience.

它带有用于浏览器扩展的有前途和创新技术堆栈:Swift / Javascript组合而不是完整的Javascript体验。

With this in mind, I was pretty excited. I mean: develop for the web with Swift ? This is the best news since the release of Vapor !… Right ?

考虑到这一点,我感到非常兴奋。 我的意思是: 使用Swift为网络开发 ? 这是自Vapor发布以来最好的消息!

Or so I thought. I decided to give it a shot, and I discovered how unstable the development experience was.

还是我想。 我决定试一试,发现开发经验是多么不稳定

Why though ? I’ll explain.

为什么呢? 我会解释。

调试器突然杀死了您的进程 (The Debugger kills your process abruptly)

Yes, you heard me. While you run the “hello world” example that Xcode provides, after 1 or 3 minutes, Safari closes itself, and so does your extension.

是的,你听到了我的声音。 在运行Xcode提供的“ hello world”示例时,在1或3分钟后,Safari会自行关闭,您的扩展也会关闭。

You might think that’s a Safari crash, but that’s in fact a part of the dev process. From what I have seen, the Xcode debugger kills the browser if no interaction/messaging is found during a certain lapse of time.

您可能会认为这是Safari崩溃,但这实际上是开发过程的一部分。 据我所知,如果在一定的时间间隔内未发现任何交互/消息,则Xcode调试器终止浏览器

So to avoid this, what you have to do is to write that code inside your injected script :

因此,为避免这种情况,您要做的就是在注入的脚本中编写该代码:

setInterval(() => safari.extension.dispatchMessage("ping", {}), 1000);

This will send a ping to the app every second, just like a keep-alive, begging the debugger not to kill your process. However, this should be done only while debugging and not for production code.

就像保持活动状态一样,这将每秒向应用程序发送一次ping操作,恳求调试器不要杀死您的进程。 但是 ,只能在调试时执行此操作,而不应在生产代码中执行此操作

Also, under release configuration, this does not apply, and the Safari App Extension runs without killing the browser.

此外,在发行版配置下,这并不适用,并且Safari App Extension可以运行而不会终止浏览器。

In addition, the docs do not mention this debugger behavior at any time. Well almost, as this archive paper proves that an App Extension can be killed anytime by the system if it takes long to load.

此外,文档在任何时候都不会提及此调试器行为。 差不多了 ,因为这份档案文件证明,只要加载很长时间,应用扩展程序就可以在任何时候被系统杀死。

该扩展有时无法安装到Safari (The extension sometimes fails to install to Safari)

When you’re developing Safari App Extensions, you might run into that error in Xcode’s debug console :

开发Safari应用程序扩展时,您可能会在Xcode的调试控制台中遇到该错误:

Computing the code signing dictionary failed for extension with identifier com.developer.myapp

Computing the code signing dictionary failed for extension with identifier com.developer.myapp

This might happen more often when your extension’s certificate is self-signed.

当扩展的证书是自签名的时,这种情况可能会更经常发生。

To avoid this, you have to:

为避免这种情况,您必须:

  1. Close and quit all instances of Safari

    关闭并退出Safari的所有实例

  2. Click Product → Clean build folder

    单击产品→ 清理构建文件夹

  3. Run your Cocoa Mac App first by changing the scheme in Xcode’s toolbar.

    首先通过更改Xcode工具栏中的方案来运行您的Cocoa Mac App

  4. Run the Safari Extension by changing the scheme in Xcode’s toolbar.

    通过更改Xcode工具栏中的方案来运行Safari扩展

My theory is this happens because self-signed app certificates might expire sooner than Developer Program Certificates. And that’s why we have to rebuild the app almost every time we run the project: to renew the local certificate.

我的理论是发生这种情况,因为自签名应用程序证书可能比开发人员计划证书更早过期。 这就是我们几乎在每次运行项目时都必须重建应用程序的原因:更新本地证书。

推荐文章 (Recommended articles)

Being totally honest, those frustrations were a bit legitimate.

老实说,这些挫折是合理的

I believe it is a common developer behavior to start digging into the code without really understanding how everything works.

我认为,在不真正了解所有工作原理的情况下开始研究代码是一种常见的开发人员行为。

Great programming guides from Apple explain the interactions between a Safari App Extension and the Containing App. Reading them toned down my anger and made me understand the overall functioning.

苹果公司的出色编程指南介绍了Safari应用扩展程序和包含应用程序之间的交互。 读它们可以减轻我的愤怒,并使我理解整体功能。

So, I highly recommend to read:

因此,我强烈建议阅读:

翻译自: https://medium.com/swlh/why-building-safari-app-extensions-is-a-nightmare-in-2020-48cb1a79c8db

safari 扩展

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值