Extensions: Sharing Code with an Embedded FM

iOS 8 Extensions: Sharing Code with an Embedded Framework

Note: Due to the NDA, this tutorial won’t include any screenshots from Xcode 6. When iOS 8 is released, I will update the tutorial with screenshots.

Extensions were one of the more dramatic new things introduced at WWDC 2014. They allow all kinds of convenient ways to extend the power of your app beyond the app itself.

This tutorial will walk you through making a today extension for iOS 8 (commonly called a widget). Widgets display in notification center, and allow people to quickly access to bits of information or perform simple actions without having to launch your app.

Although this tutorial specifically focuses on today extensions, much of what we will cover  applies to other extensions too, including sharing code & data.

Before you begin, download the source code to super simple sample app Word of the Day here. Word of the Day as you would expect, displays the word of the day from Wordsmith.org. It wouldn’t get many downloads in the App Store, but it does the job.

This is a finished project, with a widget (today extension), an embedded framework we are using to share code, and data sharing with a shared container.

Sharing Code With an Embedded Framework

One of the most important things you need to do when adding a widget to your project is to share code and files from your main project with your extension. Since extensions are separate binaries, the easiest way to do this is by creating an embedded framework, a new feature available in iOS8.

Step 1: Add a New Target

In your project, select File > New > Target. Then, on the sidebar, select Framework & Library under the iOS header, then select Cocoa Touch Framework and click Next.

Give your framework a name and click Finish.

Step 2: Add & Remove Necessary Files

Make sure to move all the necessary code and resources (including images, etc) that you will need to access from both your extension as well as your app into the new framework.

The easiest way to do this is to select your Framework target, and go to the Build Phases tab. Add all .m files to the Compile Sources phase, all .h files to the Headers phase (more details about header files below), and resources go to the Copy Bundle Resources phase.

All of these files should also be removed from the corresponding locations in the original target (your app). Make sure the files you need to access from both your app & widget are all added to your framework, and removed from your main app.

Step 3: Import Headers & Set Header Visibility

When you create your dynamic framework, it’s going to come with an umbrella header file that you can use to import other header files in the framework. For example, in our sample project, our WordOfTheDayFramework has a handy WordOfTheDayFramework.h file.

Any files that needs to be visible to the app or extension, need to be imported into this file. For example, we need both TFWordOfTheDay.h and TFWordOfTheDayFetcher.h, so we add these two lines to WordOfTheDayFramework.h: 

#import "TFWordOfTheDayFetcher.h"
#import "TFWordOfTheDay.h"

Note that you’ll get a build error after doing this if any of the headers you import into this master header are not public.

To change this, check your Framework’s Build Phases tab, and make sure any headers you want to access in your app or extension are in the Public visibility category. Any headers that only need to be visible to the framework should be in the Project visibility category. You can drag & drop headers from one section to another to fix this.

Note that when you create a NEW file in your framework, by default the header visibility will be set to Project. You’ll want to change that to Public if necessary.

Step 4: Link Your Framework to Your Extension

When you created your Framework, it should have automatically been embedded in your app and linked. Double check by going to your app target, under Build Phases, check that your Framework is in the Link Binary With Libraries section and the Embed Frameworks section.

Select your extension target, and add the framework to the Link Binary With Libraries section to link your framework to the widget.

Important Note: After performing this step and building your project, you’ll likely get an error like this: warning: linking against dylib not safe for use in application extensions. 

To make this error go away, choose the Framework target, and under the General tab, check the Allow app extension API only box (some APIs are not available in extensions, and this will tell Xcode to warn you if you try to use any of those APIs).

Step 5: Update Framework Imports

Now that all your shared code is moved to the embedded Framework, you need to update any imports by referring to the framework. You can do this by just importing the umbrella header if you want, or import a specific file. In our sample project to import everything we could use:

@import WordOfTheDayFramework;

Or, to import a specific header:

@import WordOfTheDayFramework.TFWordOfTheDayFetcher;

And that’s should be it! You should be all set to share code between your app and your extension. If I missed anything important, or if you run into any trouble, let me know in the comments.

Coming soon: In the next segment, we’ll cover sharing data & defaults between your app and widget, and after that, in the last segment, we’ll talk about debugging widgets.

参考链接:

http://code.tutsplus.com/tutorials/ios-8-creating-a-today-widget--cms-22379

http://zixun.github.io/blog/2014/10/22/%3Cyi-%3Eios-8-today-extension-tutorial/

时间: 2024-10-26 21:41:24

Extensions: Sharing Code with an Embedded FM的相关文章

WWDC2014 IOS8 APP Extensions

本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35558623  感谢撰文作者的分享 WWDC14 最令人兴奋的除了新语言switf外,无疑就是整个APPLE平台的开放了.说到开放,当然就是属于Extension了.Extension实现了各个应用程序间功能和资源的共享. 下面我们来看看Extension具体有哪些 Today -- 在 iOS 系统的“通知中心”里,支持第三方应用集成的“小工具.小部件”,比如 eBay 提供的价

JavaScript code modules

https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules Non-standardThis feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be la

VS Code 控制台不能输入

问题 使用 VS Code 编写 C++ 程序时出现控制台不能输入的情况,代码如下: #include<iostream> using namespace std; int main(){ int a; cin>>a; cout<<a<<endl; return 0; } 控制台提示 Cannot edit in read-only editor . 解决 我使用的是 Code Runner 扩展,设置里面 Run In Terminal 打勾就好了,步骤和

Go语言(golang)开源项目大全

转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析器控制台用户界面加密数据处理数据结构数据库和存储开发工具分布式/网格计算文档编辑器Encodings and Character SetsGamesGISGo ImplementationsGraphics and AudioGUIs and Widget ToolkitsHardwareLangu

GO语言的开源库

Indexes and search engines These sites provide indexes and search engines for Go packages: godoc.org gowalker gosearch Sourcegraph Contributing To edit this page you must be a contributor to the go-wiki project. To get contributor access, send mail t

[转]Go语言(golang)开源项目大全

内容目录 Astronomy 构建工具 缓存 云计算 命令行选项解析器 命令行工具 压缩 配置文件解析器 控制台用户界面 加密 数据处理 数据结构 数据库和存储 开发工具 分布式/网格计算 文档 编辑器 Encodings and Character Sets Games GIS Go Implementations Graphics and Audio GUIs and Widget Toolkits Hardware Language and Linguistics 日志 机器学习 Math

sublime text plugins

Sublime Text 插件,HTML+CSS+JAVASCRIPT+JSON快速格式化:  htmlpretty 快捷键:Ctrl+Shift+H Essential Sublime Text 2 Plugins and Extensionshttp://code.tutsplus.com/tutorials/essential-sublime-text-2-plugins-and-extensions–net-24173 Sublime Text 使用介绍.全套快捷键及插件推荐http:/

网上搜集第三方(二)

這是我收集了一些第三方的 Library,大部份都還沒用過Orz,只是這樣整理以後要用到的時候會比較好找到. UI UI Design alertView Menu(選單) Side ViewController UICollectionView Segment Control Others 動畫 影像特效 模糊.毛玻璃效果(Blur) 畫面轉換 各種統計圖表 Line Bar Pie Others Pull To ReFresh Notification Auto Layout Data St

WWDC2014之iOS使用动态库 framework【转】

from:http://www.cocoachina.com/industry/20140613/8810.html JUN 12TH, 2014 苹果的开放态度 WWDC2014上发布的Xcode6 beta版有了不少更新,其中令我惊讶的一个是苹果在iOS上开放了动态库,在Xcode6 Beta版的更新文档中是这样描述的: Frameworks for iOS. iOS developers can now create dynamic frameworks. Frameworks are a