Extensions:sharing Code with an Embedded FM

Source: Internet
Author: User
Tags notification center

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'll 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 would 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 action s without has to launch your app.

Although this tutorial specifically focuses on today extensions, much of the what we'll cover applies to other extensions T OO, 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 is 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 was a finished project, with a widget (today extension), an embedded framework we have using to share code, and data sh Aring with a shared container.

Sharing Code with an Embedded Framework

One of the most important things your need to does when adding a widgets to your project are to share code and files from your Main project with your extension. Since extensions is separate binaries, the easiest-on-the-do-is-by-creating framework, a new embedded AVA Ilable 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 and then select Coc OA 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) so you'll need to a Ccess from both your extension as well as your apps into the new framework.

the easiest-to-do the-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 is removed from the corresponding locations in the original target (your app). Make sure the files need to access from both your apps & Widgets is all added to your framework, and R Emoved 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 so can use to imp Ort other header files in the framework. For example, with our sample project, our  wordofthedayframework  have a handy  wordofthedayframework.h  file.

Any files which needs to is visible to the app or extension, need to is imported into this file. For example, we need both TFWordOfTheDay.h and TFWordOfTheDayFetcher.h, so we add these and 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 your import into this master header is not publi C.

to change this, check the your Framework ' s Build phases tab, and make sure any headers you want to access in y Our app or extension is in the  public  visibility category. Any headers that-need to is visible to the framework should is in the  project  Visibil ity category. You can drag & drop headers from one sections to another to fix this.

Note that if you create a NEW file in the your framework, by default the header visibility would be set to Project. You'll want to the change of that to public if necessary.

Step 4:link Your Framework to Your Extension

When you created your Framework, it should has automatically been embedded in your app and linked. Double check by going to your app target, under Build phases, Check that your Framework are in the Li NK Binary with Libraries sections and the Embed frameworks section.

Select your extension target, and add the framework to the link Binary with Libraries sections to link your Framew Ork to the widget.

Important Note: After the performing this step and the 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 Exten Sion API Only box (some APIs is not available in extensions, and the this would tell the Xcode to warn if you try to use Any of those APIs).

Step 5:update Framework Imports

Now-all your shared code are moved to the embedded Framework, and you need to update any of the imports by referring to the frame Work. 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 is it! You should is all set to share code between your app and your extension. If I missed anything important, or if you run to 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.


Reference Links:

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/

Extensions:sharing Code with an Embedded FM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.