文档章节

Extensions: Sharing Code with an Embedded FM

小春0618
 小春0618
发布于 2015/08/01 18:57
字数 1001
阅读 80
收藏 0

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/

本文转载自:http://atinyfish.com/2014/06/27/ios-8-extensions-sharing-code-with-an-embedded-framework/

小春0618
粉丝 8
博文 40
码字总数 10552
作品 0
海淀
程序员
私信 提问
加载中

评论(0)

Hacking With File Upload Vulnerabilities

Allowing an end user to upload files to your website is like opening another door for a malicious user to compromise your server. However, uploading files is a necessity for any......

Bluven
2014/08/28
508
0
Azure Cloud Shell - your own bash shell and contai

Azure Cloud Shell - your own bash shell and contai Scott Hanselman's Blog2017-12-031 阅读 shellbashcloud Visual Studio Code has a HUGE extension library . There's also almost t......

Scott Hanselman's Blog
2017/12/03
0
0
Spring Boot Configure DataSource Using JNDI with Example

转载自 https://www.java4s.com/spring-boot-tutorials/spring-boot-configure-datasource-using-jndi-with-example/ ===================================================================......

wells杨
2019/07/12
713
0
Riot 3.6.1 发布,JavaScript 的 MVP 框架

Riot 3.6.1 发布了,Riot.js 是一个客户端模型-视图-呈现(MVP)框架并且它非常轻量级甚至小于 1kb。尽管他的大小令人难以置信,所有它能构建的有如下:一个模板引擎,路由,甚至是库和一个严格...

局长
2017/06/26
570
0
胡户主/go_spider

go_spider A crawler of vertical communities achieved by GOLANG. Latest stable Release: Version 1.2 (Sep 23, 2014). QQ群号:337344607 Features Concurrent Fit for vertical commun......

胡户主
2014/11/30
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Mysql 查询天、周,月,季度、年的数据

今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1 近7天 SELECT * FROM 表名 where DA......

sp_lucky
45分钟前
23
0
java函数式编程Lambda基础 几Stream

java函数式编程Lambda基础 LambdaJava 8引入了Lambda语法,用以简化单抽象方法接口的实现 Lambda语法实现了类型自动推断 只定义了单个(只有一个抽象方法)抽象方法接口被称为函数式接口,标注...

ossea
47分钟前
39
0
异步爬虫+asyncio+python3.7+(async + await )

import asyncio import json import time import traceback import aiohttp import logging from aiohttp import ContentTypeError from motor.motor_asyncio import AsyncIOMotorClient log......

丁典
50分钟前
17
0
项目讲解4

分组取topN的方法: 数据的预处理为使用mapreduce 每日新访客: 回头/单次访客统计: 漏斗模型: 使用python产生数据 1.统计每个步骤的总访问人数 create table tmp_page_views like ods_web...

七宝1
今天
88
0
笔记:pyecharts可视化

# 加载数据import pandas as pddf = pd.read_excel(r"D:\我的文档\数据源\采购商品出库综合查询 - Python.xlsx",header = 2)df # 计算出结果df1 = df.groupby(["部门"])["总销售额"......

tengyulong
今天
120
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部