Blake
Blake

Reputation: 1216

How do I get the "Open in Safari" icon to appear when sharing a URL using UIActivityViewController?

I'm struggling to get my application to share a URL properly so that the "Open in Safari" and "Open in Chrome" activity items show up in the share sheet. I've tried sharing the URL a few different ways:

NSURL *data = _article.url;
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[data] applicationActivities:nil];

For reference, these are the two activity items I'm trying to get to show up.

enter image description here

Upvotes: 16

Views: 4984

Answers (2)

Pieter Meiresone
Pieter Meiresone

Reputation: 2062

There a few (small) libraries you can use that provide custom UIActivity controls to get the "Open in Safari" and "Open in Chrome" activity items.

  • Safari: TUSafariActivity
  • Chrome: ARChromeActivity

    @IBAction func shareClick(_ sender: Any) {
       var sharingItems = [AnyObject]()
       var sharingActivities = [UIActivity]()
    
       sharingItems.append(URL(string: shareURL)! as AnyObject)
       sharingActivities.append(TUSafariActivity())
       sharingActivities.append(ARChromeActivity())
    
       let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: sharingActivities)
       activityViewController.popoverPresentationController?.barButtonItem =    self.navigationItem.rightBarButtonItem;
       self.present(activityViewController, animated: true, completion: nil)
    }
    

Even more custom UIActivity controls can be found on https://github.com/shu223/UIActivityCollection

Upvotes: 5

Blake
Blake

Reputation: 1216

It appears there is a very popular library "SVWebViewController" for displaying in app web views. The library also contains some nice activity items that you can use to do this.

https://github.com/TransitApp/SVWebViewController

Here is an example of the code you can use to make it work (don't forget to include the headers in your code as well)

#import "SVWebViewControllerActivityChrome.h"
#import "SVWebViewControllerActivitySafari.h"

- (void)share:(id)sender {
    NSArray *activities = @[[SVWebViewControllerActivitySafari new], [SVWebViewControllerActivityChrome new]];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[_article.url] applicationActivities:activities];
}

Upvotes: 3

Related Questions