简体   繁体   English

当我的应用程序处于后台时,将其他应用程序的文件/内容打开到我自己的iOS应用程序中

[英]Open file / content of other app into my own iOS app, when my app was in background

I am working on a mobile clipper. 我正在研究移动式飞剪机。 When in a galery app, I want to be able to send a picture to my app. 在galery应用程序中时,我希望能够将图片发送到我的应用程序。 When in the browser, I want to be able to send the current url to my app. 在浏览器中时,我希望能够将当前URL发送到我的应用程序。

Note : I'm already aware that I should add a CFBundleDocumentTypes to the plist file so that my app appears in the list of apps to which I can send data (see also How do I associate file types with an iPhone application? ) 注意 :我已经知道我应该将CFBundleDocumentTypes添加到plist文件中,以便我的应用程序出现在可以向其发送数据的应用程序列表中(另请参阅如何将文件类型与iPhone应用程序关联?

So when the "share" action is triggered while my app is not started, I can listen to application:didFinishLaunchingWithOptions and then extract the data from the launch options. 因此,当我的应用程序未启动时触发“共享”操作时,我可以监听application:didFinishLaunchingWithOptions ,然后从启动选项中提取数据。

But when my app is already started and is in background, and I share an image or an url to it, obviously the didFinishLaunchingWithOptions will not trigger so I won't be able to get a dictionnary to which I can get my data. 但是,当我的应用程序已经启动并且处于后台并且共享图像或URL时,显然didFinishLaunchingWithOptions不会触发,因此我将无法获得字典来获取数据。

See approved comment in original answer: 查看原始答案中已批准的评论:

it should be noted that -application:didFinishLaunchingWithOptions: in the app delegate is only called if your app was not backgrounded when it's opened to handle a file. 应该注意的是,只有在打开应用程序处理文件时未将其后台运行时,才会调用应用程序委托中的-application:didFinishLaunchingWithOptions:。

So, how can I handle both cases: when my app was in background and is made active, and when my app was simply started? 那么,我该如何处理这两种情况:当我的应用程序在后台运行且处于活动状态以及何时仅启动我的应用程序?

Sorry if this question is dumb as I'm not a iOS dev at all and trying to do my first cordova plugin. 抱歉,这个问题很愚蠢,因为我根本不是iOS开发人员,正在尝试做我的第一个cordova插件。

My app should be compatible with at least iOS 7. 我的应用程序至少应与iOS 7兼容。

You can use below application delegate method 您可以使用下面的应用程序委托方法

For IOS 9 and later 对于IOS 9及更高版本

  • (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options (布尔)应用程序:(UIApplication *)应用程序openURL:(NSURL *)url选项:(NSDictionary *)选项

Below IOS 9 在IOS 9以下

  • (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation (BOOL)应用程序:(UIApplication *)应用程序openURL:(NSURL *)URL sourceApplication :(可空NSString *)sourceApplication注释:(id)注释

如果您仍需要支持iOS 7:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation;

The process is described in the iOS Application Programming Guide 该过程在《 iOS应用程序编程指南》中进行了描述

An app that has its own custom URL scheme must be able to handle URLs passed to it. 具有自己的自定义URL方案的应用程序必须能够处理传递给它的URL。 All URLs are passed to your app delegate, either at launch time or while your app is running or in the background. 在启动时或在应用程序运行时或在后台,所有URL都将传递给您的应用程序委托。 To handle incoming URLs, your delegate should implement the following methods: 要处理传入的URL,您的委托应实现以下方法:

  • Use the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods to retrieve information about the URL and decide whether you want to open it. 使用application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions:方法检索有关URL的信息并确定是否要打开它。 If either method returns NO, your app's URL handling code is not called. 如果这两种方法均返回NO,则不会调用您应用的URL处理代码。
  • Use the application:openURL:sourceApplication:annotation: method to open the file. 使用application:openURL:sourceApplication:annotation:方法打开文件。

It is this second method that will be invoked if your app is running in the background 如果您的应用程序在后台运行,则将调用第二种方法

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM