12

I Have integrated share extension in my app but I want to do few modification in the SLComposeServiceViewController pop up as per the project requirement like change the button titles and set background colour for text view and header. How do I do that?

iMash
  • 1,178
  • 1
  • 12
  • 33
  • The [documentation](https://developer.apple.com/library/mac/documentation/Social/Reference/SLComposeServiceViewController_Class/) should help. – twe4ked Oct 26 '14 at 18:03
  • 1
    I am also looking for more info on customising SLComposeServiceViewController – Refael.S Oct 29 '14 at 10:51

4 Answers4

5

I am answering my question if someone could help this out. After gone through many articles and reading I have come up with following solution. Because there is not enough content to try.

I change base class SLComposeServiceViewController as UIViewController so that I can do some customisation. So as we know we can add popup like evernote also we can apply animationt to that popup.

You can get post method callback in viewddidload method of your viewController.Where you can do something like this:

- (void)viewDidLoad {
    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) {
                                  NSString *urlString = url.absoluteString;
                                  NSLog(@"%@",urlString);

                          }];
    }
}

From above code you can obtain url link. for more information to get image and other things best to refer only apple documentation.

Apple Documentation

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
iMash
  • 1,178
  • 1
  • 12
  • 33
  • 1
    I think in the same documentation it mentions that it's preferable to use the constants defined in MobileCoreServices for the UTI types. something like: `if([currentItem hasItemConformingToTypeIdentifier:(__bridge NSString *)kUTTypeURL])` – Louis Tur Jan 23 '15 at 00:42
4

To change the Post button text, here's a Swift solution that should most likely pass review (caveat being I haven't tried yet). Do it in an SLComposeServiceViewController subclass:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // Reset Post button text.
    for item in (self.navigationController?.navigationBar.items)! {
        if let rightItem = item.rightBarButtonItem {
            rightItem.title = "Save"
            break
        }
    }
}
1

You can change e.g. button titles of SLComposeServiceViewController by doing this:

class CustomServiceViewController: SLComposeServiceViewController {
    override func viewDidLoad() {
        let navigationBar = view.subviews.first?.subviews?.last? as? UINavigationBar
        let postButton = navigationBar?.subviews.last? as? UIButton
        let cancelButton = navigationBar?.subviews.last? as? UIButton
        postButton?.setTitle("Done", forState: .Normal)
    }
}

Be warned - it's a fragile solution, based on undocumented internals of SLComposeServiceViewController

average Joe
  • 4,377
  • 2
  • 25
  • 23
0

This works as of Xcode 6.4 and iOS 8.4

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    for(UIView* v in self.navigationController.navigationBar.subviews){
        if(![v isKindOfClass:[UIButton class]]){
            continue;
        }
        UIBarButtonItem* b = (UIBarButtonItem*)v;
        if([b.title isEqualToString:@"Post"]){
            b.title = @"Save";
        }
    }
}

Note its in viewDidAppear and not viewDidLoad. The button is actually an instance of UINavigationButton however I cast it to a UIBarButtonItem which is its public counterpart. I haven't verified if this will get past Apple review because they could argue its using a private API. If the post title gets localised on other languages then this won't work, but that isn't a big deal because changing the title is purley cosmetic. I'd be hesitant to change the title based on its subview order, because of the right to left problem in some countries, however one thing that could be done is checking which is in bold/bordered and that'll be the post button.

malhal
  • 26,330
  • 7
  • 115
  • 133