Getting URL in SLComposeServiceViewController in the extension for Safari on iOS

This question may have been asked earlier in a different form, but I'm trying to try a little deeper to understand the Share extensions, and therefore this question has more detailed information than the previously asked version of this question.

When writing the Share extension, we can subclass SLComposeServiceViewController and get viewDidLoad () and didSelectPost () events among others, but the only properties in VC are the contentText and textView and placeholder properties according to Apple's documentation

https://developer.apple.com/library/prerelease/ios/documentation/Social/Reference/SLComposeServiceViewController_Class/

Given that this is the case, the best way to do the following is:

First, the VC pad that appears with the website url

Secondly, access the URL to go to sharedDefaults as follows

let shareDefaults = NSUserDefaults(suiteName: "groupName") shareDefaults?.setObject(self.contentText, forKey: "stringKey") shareDefaults?.synchronize() 

to be able to save it for later access from the application.

Any help in getting the URL would be greatly appreciated.

+5
source share
1 answer

You can access a URL like this:

 - (void)didSelectPost { NSExtensionItem *item = self.extensionContext.inputItems.firstObject; NSItemProvider *itemProvider = item.attachments.firstObject; if ([itemProvider hasItemConformingToTypeIdentifier:@"public.url"]) { [itemProvider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(NSURL *url, NSError *error) { // Do what you want to do with url [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil]; }]; } } 

Same thing in Swift:

 override func didSelectPost() { if let item = extensionContext?.inputItems.first as? NSExtensionItem { if let itemProvider = item.attachments?.first as? NSItemProvider { if itemProvider.hasItemConformingToTypeIdentifier("public.url") { itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (url, error) -> Void in if let shareURL = url as? NSURL { // do what you want to do with shareURL } self.extensionContext?.completeRequestReturningItems([], completionHandler:nil) }) } } } } 
+8
source

Source: https://habr.com/ru/post/1232433/


All Articles