GooglePlusShareActivity 0.2.11

GooglePlusShareActivity 0.2.11

测试测试
语言语言 Obj-CObjective C
许可 MIT
发布上次发布2015年4月

Lysann Schlegel维护。



  • Lysann Schlegel

此库提供了一个用于Google+分享的UIActivity子类。它使用官方Google+ iOS SDK的原生分享构建器API进行分享,并使用GPPSignIn API进行登录。

屏幕截图

带有GooglePlusShareActivity的UIActivityViewController   GPPShareBuilder

设置及使用

以下是在您的iOS应用程序中集成和使用此库的详细说明。在Examples目录中也有一些Swift和Objective-C的示例项目。

安装

推荐通过CocoaPods进行安装

pod 'GooglePlusShareActivity'

如果您的应用程序是用Swift编写的,您必须在外部桥接头中导入一些头文件。

#import <GooglePlus/GooglePlus.h>
#import <GooglePlusShareActivity/GPPShareActivity.h>

有关Swift应用程序中的Objective-C pod的更多信息,请参阅这里

Google+ API

按照Google+ iOS SDK入门指南的第1步和第3步进行操作

  • 创建一个API控制台项目
  • 为您的iOS应用程序添加一个URL类型

客户端身份验证和登录回调

在适当的位置,通过提供您的API控制台项目的客户端ID来初始化Google+登录API

#import <GooglePlus/GooglePlus.h>

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // initialize Google+ Sign In API
    [GPPSignIn sharedInstance].clientID = @"xxxxxxxxxxxx.apps.googleusercontent.com";

    // ...
    return YES;
}

您的AppDelegate还必须将Google+登录回调URL转发给GPPSignIn

@implementation AppDelegate
// ...

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    // handle Google+ Sign In callback URL
    return [[GPPSignIn sharedInstance] handleURL:url sourceApplication:sourceApplication annotation:annotation];
}

分享活动设置

将一个GPPShareActivity添加到您的UIActivityViewController。它接受一个字符串和一个图像或URL。您也可以创建和自定义自己的分享构建器。

#import <GooglePlusShareActivity/GPPShareActivity.h>

- (void)actionButtonTapped:(UIBarButtonItem*)sender
{
    // set up items to share, in this case some text and an image
    NSArray* activityItems = @[ @"Hello Google+!", [UIImage imageNamed:@"example.jpg"] ];

    // URL sharing works as well. But you cannot share an image and a URL at the same time :(
    NSArray* activityItems = @[ @"Hello Google+!", [NSURL URLWithString:@"https://github.com/lysannschlegel/GooglePlusShareActivity"] ];

    // You can also set up a GPPShareBuilder on your own. All other items will be ignored
    id<GPPNativeShareBuilder> shareBuilder = (id<GPPNativeShareBuilder>)[GPPShare sharedInstance].nativeShareDialog;
    [shareBuilder setPrefillText:@"Hello Google+!"];
    [shareBuilder setURLToShare: [NSURL URLWithString:@"https://github.com/lysannschlegel/GooglePlusShareActivity"]];
    NSArray* activityItems = @[ @"Does not appear", shareBuilder ];


    // set up activity
    GPPShareActivity* gppShareActivity = [[GPPShareActivity alloc] init];
    gppShareActivity.canShowEmptyForm = YES;

    // ...
}

canShowEmptyForm 设置为 YES 将显示 Google+ 分享活动,即使没有项目被识别为可用于分享。在这种情况下,将显示空表单,用户可以添加文本。默认为 NO,即活动不包括在活动视图中。

iOS < 8 上的呈现

如果您想支持 iOS 7 及以下版本,呈现 UIActivityViewController 会有点复杂

// set up and present activity view controller
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[gppShareActivity]];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // present in popup
    self.activityPopoverController = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
    gppShareActivity.activityPopoverViewController = self.activityPopoverController;
    [self.activityPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

} else {
    // present modally
    gppShareActivity.activitySuperViewController = self;
    [self presentViewController:activityViewController animated:YES completion:NULL];
}

设置 activityPopoverViewControlleractivitySuperViewController 允许在显示 Google+ 分享视图之前取消弹出控制器或模态视图控制器。

如果在弹出视图可见的情况下再次触发动作,您还应该考虑取消 self.activityPopoverController

iOS >= 8 上的呈现

虽然上述代码在 iOS 8 上也能正常工作,但如果只针对 iOS 8 及以后的版本,可以简化代码。

// set up and present activity view controller
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[gppShareActivity]];
activityViewController.popoverPresentationController.barButtonItem = sender;
[self presentViewController:activityViewController animated:YES completion:NULL];

注意事项

活动处于活动状态时,不要更改 GPPSignInGPPShare 的代理。必须通知 GPPShareActivity 关于登录和分享进度。如果想要成为代理,请在开始活动之前分配代理。GPPShareActivity 在执行活动时会覆盖当前代理,并将所有通知转发给原始代理。

许可证

它是 MIT 许可。请参阅 LICENSE 文件。