Sharing to Google+ from your iOS app
The Share dialog provides a means for users to share rich content from your app into the Google+ stream, including text, photos, and URL attachments. The code examples in this page demonstrate how to display and share content with a native share dialog within your app. The native share dialog requires that you use Google+ Sign-In in your app and ensures that your visitors stay in your app in context while sharing content.
Additional examples demonstrate how to share interactive posts, upload media with posts, use deep linking, use basic sharing for anonymous users, and detect if posts are shared successfully.
Before you begin
You must create a Google Developers Console project and initialize the Google+ client. If you plan on building deep linking into your app, you must create a client ID for each platform that you plan on supporting within the same Developers Console project.
The native sharing dialog requires that you use Google+ Sign-In and request the
https://www.googleapis.com/auth/plus.login (kGTLAuthScopePlusLogin).
Add native sharing
To integrate the Google+ share in your application, add a
UIButton
and configure it with these steps:
-
In your ViewController, set the target of your
UIButtonto anIBAction, which isdidTapSharein the example below.- (IBAction) didTapShare: (id)sender { // Share builder code will go here } -
Create a share dialog builder instance by calling
GGPShare'snativeShareDialogmethod, and then callopento open it. This example brings up a mostly-empty share dialog using the native share dialog.- (IBAction) didTapShare: (id)sender { id<GPPNativeShareBuilder> shareBuilder = [[GPPShare sharedInstance] nativeShareDialog]; [shareBuilder open]; }
Sharing a URL
When you share a URL, the title, thumbnail image, and link for the
share dialog are based on the URL you provide. You specify this URL by using the
GPPShareBuilder's setUrlToShare method.
- (IBAction) didTapShare: (id)sender {
id<GPPNativeShareBuilder> shareBuilder = [[GPPShare sharedInstance] nativeShareDialog];
// This line will fill out the title, description, and thumbnail from
// the URL that you are sharing and includes a link to that URL.
[shareBuilder setURLToShare:[NSURL URLWithString:@"https://www.example.com/restaurant/sf/1234567/"]];
[shareBuilder open];
}
If you have trouble configuring the preview of the shared content based on the URL, see the snippet documentation.
Additional sharing options
You might also want to explore the following approaches to sharing content from your app:
- Share interactive posts. Display a prominent call-to-action button in your posts that drives more traffic to your site or app.
- Attaching media to your shared posts. You can attach pictures and movies to your posts for your users to share to their stream.
- Enable deep linking into your app from your posts. Deep link has two benefits: it directs users to the correct context deep in your app, and prompts mobile users to install your app when they click the link from the stream in the Google+ mobile app if they do not already have your app installed.
- Prefill the share dialog. Suggest content for your user to share. You can also prefill recipients for the user to share directly with if you use Google+ Sign-In in your app.
- Allow non-signed in users to share from your app. The
browser based method (
GPPShareBuilder) allows apps that do not use Google+ Sign-In to enable sharing of content from their apps. This option is limited in capabilities compared to using the native share dialog method. - Detect successful shares. You can detect if a post was shared successfully from your app.
Next steps
After you implement sharing in your app, you might consider implementing the following features: