0

Unfortunately, there's currently a lack of sample code oder proper information on App Extension / Share Sheets on iOS 9. (Even Apples Dev Support is very disappointing in this regard. Just today I got the answer "We don’t have an action extension sample either unfortunately". And they won't send me.

Anyway, maybe someone is willing to share his knowledge on this topic. So, adding a Share Extension in xcode is the easy part, but I struggle with

  1. sending and receiving strings: I want the Share Sheet to open in Safari with the current URL (maybe even with the Character Counter like in the WWDC 2015 Video "App Extension Best Practices") and send it to the app by tapping "Post". Is it really necessary to open an NSURLSession?

  2. adding an Share-icon under / General / "App Icons and Launch Images" / App Icons Source is not possible. Some Sources suggest to use Asset Catalog - but before I do, is there no other way to simply add ONE image?

Other Sources I found include:

Today Extension: raywenderlich.com/83809/ios-8-today-extension-tutorial

How to Build a Simple Action Extension: http://code.tutsplus.com/tutorials/ios-8-how-to-build-a-simple-action-extension--cms-22794

Basic Share Extensions with Data Sharing on iOS 8: http://www.andypierz.com/blog/2014/9/19/basic-share-extensions-with-data-sharing-on-ios-8

What I was able to accomplish so far is to add a title to the Share Sheet on shareViewController.m:

- (void)loadView
{
    [super loadView];
    self.title = @"Title of the Share Sheet";
}
  • There's no such thing as ShareViewController so what exactly are you talking about here? Are you asking about writing a Share Extension? – matt Oct 02 '15 at 16:54
  • Yes, I want to add a Share Extension to my App. When I choose "File > New > Target > iOS Application Extension > Share Extension" then I get 4 Files: a shareViewController.h/.m, a Storyboard and a Info.plist. I'm a little confused about your reply "There's no such thing as ShareViewController"? – Dev Studio Wien Oct 02 '15 at 17:58
  • Well, it's just an ordinary SLComposeServiceViewController subclass. And that's what matters. "ShareViewController" is just an arbitrary name they supply in the template; you are not married to it. – matt Oct 02 '15 at 18:33
  • So, now we've established that you want to write a Share Extension and you're asking a question about the SLComposeServiceViewController. So now, what's your question? I'm afraid I can't quite see it, hidden in all the wandering around you do... – matt Oct 02 '15 at 18:34
  • I want to send an URL over SLComposeServiceViewController to my App. First I need an App Group, right? I've done that (under capabilites) both for my sharing target and my app. My understanding is that strings are stored in a shared NSUserDefaults? – Dev Studio Wien Oct 02 '15 at 19:22
  • You can do it that way, sure. I've referred you to the appropriate docs in my answer. And there's a very good WWDC 2015 video on this topic. – matt Oct 02 '15 at 19:26

2 Answers2

1

I want the Share Sheet to open in Safari with the current URL (maybe even with the Character Counter like in the WWDC 2015 Video "App Extension Best Practices")

It is not up to you what app the user uses your share extension in, and it is not up to you what data that app supplies to the share extension. The app will share what the app wants to share. All you can do is define what kind of data you are willing to accept.

and send it to the app by tapping "Post"

When the user taps "Post", your didSelectPost override is called. What you do is up to you. What do you mean by "the app" here? Do you mean, your app that provides the share extension? Then devise a custom URL scheme for communication from the extension to the app, or use one of the other data sharing methods described in the docs:

https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

adding an Share-icon under / General / "App Icons and Launch Images" / App Icons Source is not possible. Some Sources suggest to use Asset Catalog - but before I do, is there no other way to simply add ONE image?

The icon for a share extension is automatically the icon of the app that supplies the share extension.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Well - as for the App Icon: the share sheet in safari shows my app (app that provides the share extension), but a blank icon, not a monochrome version of my icon (which I want to set and know where to put - in resources? any naming conventions?) "_Then devise a custom URL scheme for communication from the extension to the app, or use one of the other data sharing methods described in the docs_" that's exactly what I'm struggling with. – Dev Studio Wien Oct 02 '15 at 19:56
  • Did you read what I said? The icon for a share extension is _not_ "a monochrome version of my icon". It is your app's icon. You don't get to substitute another icon. – matt Oct 02 '15 at 20:06
  • "that's exactly what I'm struggling with" Sounds good, but "I'm struggling" is not a question. I've pointed you to the resources on this. If you have a specific new question, ask a new question! I think I've answered the question you asked (to the extent that I could even figure out what it was). – matt Oct 02 '15 at 20:07
  • Seems like the app Icon for Sharing is coloured in the share Sheet, since my app appears (with a placeholder icon) in the first row of coloured icons. The reason, why I don't see any Icon Artwork but the app as such lies somewhere else then. **I'm willing to accept that part as an answer but that leaves the title of my Post still unanswered**. I know that `didSelectPost` is triggered, I also have a vague idea what's happening in `didSelectCancel`, the question is HOW (code) – Dev Studio Wien Oct 02 '15 at 20:59
  • The question is how _what_? How to communicate from the extension to the app? Well, I've told you one thing you can do, and I've posted sample code showing how to do it (use a custom URL scheme); and I've steered you to two kinds of Apple documentation, both written and video. I'm not going to physically come over to your house and write your code for you. – matt Oct 02 '15 at 21:59
0

to extract the URL, you will have to enable the ExtensionPreprocessingJS part first

the following is slightly modified from AppCode iOS8 action extension tutorial

Add a new file GetURL.js to your share extension folder

var GetURL = function() {};

GetURL.prototype = {

   run: function(arguments) {
       arguments.completionFunction({ "currentUrl" : document.URL });
   }    
};

var ExtensionPreprocessingJS = new GetURL;

Next, you will have to edit your Info.plist so that the host app will call the JS. Refer to AppCode tutorial on how to do so. enter image description here

Then include the following function in your ShareViewController viewDidLoad

    //swift code
    let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
    let itemProvider = extensionItem.attachments?.first as! NSItemProvider

    let propertyList = String(kUTTypePropertyList)
    if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
        itemProvider.loadItemForTypeIdentifier(propertyList, options: nil, completionHandler: { (item, error) -> Void in
            let dictionary = item as! NSDictionary
            NSOperationQueue.mainQueue().addOperationWithBlock {
                let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! NSDictionary
                let url = NSURL(string: (results["currentURL"] as! String))                                        
                //now you can do what you like with this url
            }
        })
    } else {
        print("error")
    }
William Ku
  • 798
  • 5
  • 17