如何从Today View Widget引用AppDelegate? [英] How to reference the AppDelegate from a Today View Widget?

查看:140
本文介绍了如何从Today View Widget引用AppDelegate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个基于我的主应用程序构建的Today View小部件,我正在尝试访问一些存储的数据(通过CoreData)。但是当我创建lazy变量来处理我的一个实体时,它无法编译。我理解它抛出的错误,但我不确定如何处理/修复它。

So I have a Today View widget built off of my main application, and I'm trying to access some stored data (via CoreData). But when I am creating the lazy variable to handle one of my entities, it fails to compile. I understand the error that it throws, but I'm not sure how to handle/fix it.

lazy var managedObjectContext : NSManagedObjectContext? = {
    let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    if let managedObjectContext = appDelegate.managedObjectContext {
        return managedObjectContext
    }
    else {
        return nil
    }
    }()

错误被抛出第2行在...作为AppDelegate,它是未申请使用AppDelegate。我认为这是有道理的,因为AppDelegate位于基本应用程序文件夹中,而不是在小部件的文件夹中。但我不知道如何替换或修复它,以便代码编译和运行。有什么想法?

The error gets thrown at on line 2 at ..."as AppDelegate" that it is an "Undeclared use of AppDelegate." Which I guess makes sense because AppDelegate is in the base application folder and not in the widget's folder. But I am at a loss of how to replace or repair this so that the code will compile and function. Any ideas?

推荐答案

你做不到。扩展程序是您的主应用程序包中完全分离的二进制文件,这是您的应用程序委托所在的位置。您必须创建一个将由主应用程序包和扩展程序使用的共享库,或者执行大量的复制粘贴代码(以前的方法是首选)。

You can't. Extensions are entirely separated binaries from your main app bundle, which is where your app delegate resides in. You will have to either make a shared library that will be used by your main app bundle and extension, or do a lot of copy-pasting code (the former method is preferred).

来自开发人员指南


您可以创建一个嵌入式框架,以在您的app
扩展程序及其包含的应用程序之间共享代码。例如,如果您开发一个图像
过滤器以用于您的照片编辑扩展以及其含有
的应用程序,请将过滤器的代码放在框架中并将
框架嵌入到两个目标中。

You can create an embedded framework to share code between your app extension and its containing app. For example, if you develop an image filter for use in your Photo Editing extension as well as in its containing app, put the filter’s code in a framework and embed the framework in both targets.

如果你不想要,你不必创建一个正式的可链接库。只需确保您编写的库不引用不可用于扩展的API。

You don't have to create a formal linkable library if you don't want. Just make sure that the library you are writing doesn't reference APIs that are not available for extensions.


确保您的嵌入式框架不包含
应用扩展程序无法使用的API,如某些API不适用于应用
扩展

如果您有一个包含此类API的自定义框架,您可以从包含的应用程序安全地链接到它,但不能与应用程序包含的扩展名共享该代码
。 App Store拒绝链接到此类框架或以其他方式使用
不可用API的任何app
扩展名。

Make sure your embedded framework does not contain APIs unavailable to app extensions, as described in Some APIs Are Unavailable to App Extensions. If you have a custom framework that does contain such APIs, you can safely link to it from your containing app but cannot share that code with the app’s contained extensions. The App Store rejects any app extension that links to such frameworks or that otherwise uses unavailable APIs.

这篇关于如何从Today View Widget引用AppDelegate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆