简体   繁体   English

在原始 iOS App 和 App Extension 之间共享代码

[英]Sharing code between original iOS App and App Extension

Sharing class between iOS app and extension app iOS 应用和扩展应用之间的共享类

I want to use a custom class in both my app and the extension app.我想在我的应用程序和扩展应用程序中使用自定义类。 If I just have the class in the main app folder, the extension doesn't pick up on it.如果我只是在主 app 文件夹中有该类,则扩展程序不会接受它。 I tried making a copy of it and putting it into the extension folder, but I of course get a invalid redeclaration of [my class] error.我尝试制作它的副本并将其放入扩展文件夹中,但我当然得到了invalid redeclaration of [my class]错误的invalid redeclaration of [my class] What's the best solution for this?什么是最好的解决方案? It's just one class, not multiple resources.它只是一个类,而不是多个资源。

Nothing should be copied.什么都不应该被复制。 Just add required files to compile sources for your extension:只需添加所需的文件即可为您的扩展编译源代码:

在此处输入图片说明

Just make sure that the files you want to share gets a tick in the section Target Membership .只需确保您要共享的文件在Target Membership部分中打勾。

Select your file, open the utility panel on the right and tick the appropriate target under Target Membership .选择您的文件,打开右侧的实用程序面板,然后在Target Membership下勾选相应的目标。

在此处输入图片说明

Embedded frameworks are a great way to reuse code between your application and your extension.嵌入式框架是在应用程序和扩展之间重用代码的好方法。 Having moved some of the code to embedded frameworks will provide us with a great opportunity to write some unit tests around it.将一些代码移到嵌入式框架将为我们提供一个很好的机会来围绕它编写一些单元测试。

See section Using an Embedded Framework to Share Code https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1请参阅使用嵌入式框架共享代码部分https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

App Extension(Widget, Today)应用扩展(小部件,今天)

App Extension - from iOS v8 - is a packaged bundle which uses different process than Containing App and can communicate with Host App . App Extension - 来自 iOS v8 - 是一个打packaged bundle ,它使用与Containing App不同的进程,并且可以与Host App通信。 It has .appex file extension and is located in PlugIns folder.它具有.appex文件扩展名,位于PlugIns文件夹中。 Something similar to CXTests [About]类似于 CXTests [关于]

Application packaged bundle应用打包捆绑

SpeedWidgetExtension.appex - Application Extension packaged bundle SpeedWidgetExtension.appex - 应用程序扩展打包包

  • Share data - use App Group [About]分享数据-使用App Group [关于]
  • Share code分享代码
    • Target Membership - the simplest way or for pre iOS v8 which allows you to add files into every target. Target Membership - 最简单的方法或适用于 iOS v8 之前的方法,它允许您将文件添加到每个目标中。 It increases a footprint of package because this file will be duplicated for every target它增加了包的占用空间,因为每个目标都会复制此文件

    • Framework - module for grouping/reusing code Framework - 用于分组/重用代码的模块

      • Embedded framework aka Dynamic framework - the most appropriate way to share common code base. Embedded framework又名Dynamic framework - 共享公共代码库的最合适方式。 Please note that for using Embedded framework you should embed it only in Containing App (and don't add this framework in App Extension . If you include framework and for App Extension it will have the same framework in App Extension bundle - framework will be duplicated).请注意,对于使用Embedded framework您应该仅将其嵌入到Containing App不要App Extension添加此框架。如果您包含框架和App Extension ,它将在App Extension bundle 中具有相同的框架 - 框架将被复制)。 It is possible because of @prath [About]这是可能的,因为@prath [关于]
       //App Extension LD_RUNPATH_SEARCH_PATHS = @executable_path/../../Frameworks //It means that App Extension can uses Dynamic framework from Containing app
      • Static Framework - link the code to executable binary. Static Framework - 将代码链接到可执行二进制文件。 You can link library only for Containing App您只能为Containing App链接库

[iOS extensions] [iOS 扩展]
[Vocabulary] [词汇]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS App和Todays Extension之间共享数据 - Sharing Data Between iOS App and Todays Extension 主应用程序和共享扩展名之间的iOS共享.h文件 - iOS Sharing .h file between main app and share extension 在ios主应用程序和扩展之间共享动态框架 - Sharing dynamic frameworks between ios main app and extension 在iOS Extension和app之间使用UIApplication.shared共享CocoaPod - Sharing CocoaPod using UIApplication.shared between iOS Extension and app 在iOS8中在App和Extension之间共享核心数据堆栈和数据 - Sharing a Core Data stack and data between App and Extension in iOS8 在 iOS 8 共享扩展和主应用程序之间共享数据 - Sharing data between an iOS 8 share extension and main app 在App Extension和Conainer App之间共享数据 - Sharing data between app extension and conainer app 在OSX和IOS应用之间共享代码的最佳实践 - Best practice for sharing code between OSX and IOS app iOS Sharing Extension应用程序图标未更改为新图标 - iOS Sharing Extension app icon not changed to new 在iOS 8扩展程序中访问核心数据SQL数据库(在App和Widget扩展程序之间共享数据) - Accessing Core Data SQL Database in iOS 8 Extension (Sharing Data Between App and Widget Extension)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM