简体   繁体   中英

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. When in the browser, I want to be able to send the current url to my app.

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? )

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.

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.

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.

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.

My app should be compatible with at least iOS 7.

You can use below application delegate method

For IOS 9 and later

  • (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options

Below IOS 9

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

如果您仍需要支持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

An app that has its own custom URL scheme must be able to handle URLs passed to it. All URLs are passed to your app delegate, either at launch time or while your app is running or in the background. To handle incoming URLs, your delegate should implement the following methods:

  • Use the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods to retrieve information about the URL and decide whether you want to open it. If either method returns NO, your app's URL handling code is not called.
  • Use the application:openURL:sourceApplication:annotation: method to open the file.

It is this second method that will be invoked if your app is running in the background

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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