Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
  • As an NSData item using [NSData dataWithContentsOfURL:];
  • As an NSURL
  • As an NSString using [myURL absoluteString];
  • With combination of NSURL (the url) and NSString (the title)

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

enter image description here

like image 918
Blake Avatar asked Aug 02 '15 21:08

Blake


People also ask

How to view a webpage open on another device in Safari?

View a webpage open on another device 1 In the Safari app on your Mac, click the “Show tab overview” button .#N#You can see the webpages open on your Mac along... 2 Click a webpage to open it.#N#Tip: You can also enter part of the page’s name in the Smart Search field, then click the... More ...

How to put icons on home screen Safari?

How to Put Safari Icons on Your Home Screen Open Safari and go to a website you visit frequently. Tap the Share icon at the bottom of the screen. Scroll across the bottom row of icons in the Sharing window and tap Add to Home Screen. Next to the icon that will represent this website is a name field.

How to open a webpage in Safari on Mac?

In the Safari app on your Mac, click the Tab Overview button . You can see the webpages open on your Mac along with lists of the webpages open on your other devices. Click a webpage to open it.

How to hide ‘shared with You’ on safari start page?

On this screen, locate the ‘Shared with You’ toggle, and when you do, tap and hold the Hamburger icon. You can now drag this section downwards to hide it further below the Safari Start page and drop it to your desired location. When done, you should see the Shared with You option at the bottom and its section much lower inside the Start Page.


2 Answers

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];
}
like image 163
Blake Avatar answered Sep 18 '22 00:09

Blake


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

like image 38
Pieter Meiresone Avatar answered Sep 18 '22 00:09

Pieter Meiresone