Many programs have a button "Share", which opens a list of applications that can handle this type of file, for example:

Share file

I want my application to process html files. For example, the user clicks the "Share" button in Google Chrome or in DropBox, and the link is transferred to my application. Where to look for information?

1 answer 1

First, in Info.plist you write:

<key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeName</key> <string>HTML</string> <key>LSHandlerRank</key> <string>Alternate</string> <key>LSItemContentTypes</key> <array> <string>public.html</string> <string>public.xhtml</string> </array> </dict> </array> 

In AppDelegate:

 //Swift func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { //do something with html file here return true } //Objective-C - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { //do something with html file here return YES; } 

And transmit:

enter image description here


To get the Safari url, create the App Extension:

 File -> New -> Target -> Share Extension 

enter image description here

Agree to create a scheme, run and see:

enter image description here

Scheme of how it works: enter image description here

Read more in the Documentation.

Next you need to configure. For starters in

 - (BOOL)isContentValid { // Do validation of contentText and/or NSExtensionContext attachments here return YES; } 

Check what you need and, if appropriate, return YES; if something is wrong then return NO; .

Enable and configure App Groups:

enter image description here

And you can save data through NSUserDefaults:

 NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.test.one"]; [shared setObject:object forKey:@"yourkey"]; [shared synchronize]; 

Receive:

 NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.test.one"]; id value = [shared valueForKey:@"yourkey"]; NSLog(@"%@",value); 
  • I tried to do as you describe - everything works in DropBox and file managers. But it does not work for Safari and Google Chrome (my application is not displayed in the list for import). At the same time, Safari and Google Chrome displays a bunch of other applications. Perhaps as it is possible to specify what url is imported? Do not know where to look? - Ivan Kramarchuk
  • @IvanKramarchuk updated the answer. - VAndrJ
  • Thanks, you really helped. - Ivan Kramarchuk