iosからgoogle+へのSNS共有

ぐぐたすへのSNS共有が必要になったので調査

1.Google+ API を有効にする

→APIs Console プロジェクトを作成し、Google+ API を有効にしてから、クライアント ID を作成して取得する

2.Create Client IDを作成

・[iOS] を選択します。
・[Bundle ID] フィールドに、アプリケーションのプロジェクト概要のパッケージ ID、この場合は com.google.GooglePlusSample を入力します。→【注意】自分で作成したプロジェクトのBundle ID
・[App Store ID] フィールドは空白のままにしておきます。このクイック スタート アプリは AppleiTunes App Store で公開されていないからです。
・[Deep Linking] の [Enabled] をクリックします。
・[Create client ID] ボタンをクリックします。

3.SDK内をダウンロード

Getting Started with the Google+ Platform for iOS - Google+ Platform for iOSGoogle Developers - https://developers.google.com/+/mobile/ios/getting-started

4.SDK内のフォルダを追加

以下フォルダをXcodeに追加
・OpenSource
・GoogleOpenSource.framework※Freamworkとして、フォルダを追加
・GoogleOpenSource.framework※Freamworkとして、フォルダを追加

5.SDK内のファイルを追加

・GooglePlus.bundle

6.SDK内のFreamworkを追加

・SystemConfiguration.framework
・Security.framework
・CoreGraphics.framework
・CoreMotion.framework

7.ARCを自動設定している場合は、ファイルに対して、「-fno-objc-arc」を設定

Build Phase>Compile Sources でGTMとGTLから始まるファイルに「-fno-objc-arc」をつけていく。※GoogleOpenSourceには不要

8.Staticライブラリーでカテゴリで定義しているライブラリを読み込むように「-ObjC」のフラグを設定

→全てのオブジェクトファイルをロードするようリンカに促す。
たいていの場合実行ファイルのサイズが大きくなってしまいますが(アプリに追加のオブジェクトコードが読み込まれるので)、既存のクラスに対するカテゴリを含むObjective-C静的ライブラリを正しく使えるようになります。

Other Linker Flags: -ObjC

※ all_loadではない。

【補足】
以下エラーが出て、コンパイルが通らなかったので、Other Linker Flagsを外したら、コンパイル通った。従って、Other Linker Flagsの設定は行わない。(2014/03/22 現在)
※認証とshareのみ実装

clang: error: linker command failed with exit code 1 (use -v to see invocation)

9.取得したClient IDを設定

取得した

static NSString * const kClientId = @"YOUR_CLIENT_ID";

10.URLTYPEを設定

Info>URL Types
Bundle Identiferを設定

11.認証とShareを以下のように実装

googleplustestAppDelegate.h

#import <UIKit/UIKit.h>
#import <GooglePlus/GooglePlus.h>

@interface googleplustestAppDelegate : UIResponder <UIApplicationDelegate,GPPDeepLinkDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

googleplustestAppDelegate.m

import "googleplustestAppDelegate.h"

@implementation googleplustestAppDelegate

- (BOOL)application: (UIApplication *)application
            openURL: (NSURL *)url
  sourceApplication: (NSString *)sourceApplication
         annotation: (id)annotation {
    return [GPPURLHandler handleURL:url
                  sourceApplication:sourceApplication
                         annotation:annotation];
}

#pragma mark - GPPDeepLinkDelegate
- (void)didReceiveDeepLink:(GPPDeepLink *)deepLink {
    // An example to handle the deep link data.
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Deep-link Data"
                          message:[deepLink deepLinkID]
                          delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

googleplustestViewController.h

#import <UIKit/UIKit.h>
#import <GooglePlus/GooglePlus.h>
@interface googleplustestViewController : UIViewController <GPPSignInDelegate>
@end

#import "googleplustestViewController.h"
#import <GoogleOpenSource/GoogleOpenSource.h>
@interface googleplustestViewController ()

@end

static NSString * const kClientId = @"MY Client ID";
@implementation googleplustestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    GPPSignIn *signIn = [GPPSignIn sharedInstance];
    signIn.shouldFetchGooglePlusUser = YES;
    //signIn.shouldFetchGoogleUserEmail = YES;  // Uncomment to get the user's email
    
    // You previously set kClientId in the "Initialize the Google+ client" step
    signIn.clientID = kClientId;
    
    // Uncomment one of these two statements for the scope you chose in the previous step
    signIn.scopes = @[ kGTLAuthScopePlusLogin ];  // "https://www.googleapis.com/auth/plus.login" scope
    //signIn.scopes = @[ @"profile" ];            // "profile" scope
    
    // Optional: declare signIn.actions, see "app activities"
    signIn.delegate = self;
    
    //[signIn authenticate];
[signIn trySilentAuthentication];
}
- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                   error: (NSError *) error
{
    NSLog(@"Received error %@ and auth object %@",error, auth);
    if (error) {
        // Do some error handling here.
    } else {
        [self refreshInterfaceBasedOnSignIn];
    }
}
- (IBAction)shareButtonTapped:(id)sender {
    id<GPPShareBuilder> shareBuilder = [[GPPShare sharedInstance] shareDialog];
    [shareBuilder setPrefillText:@"Share!"];
    [shareBuilder setURLToShare:[NSURL URLWithString:@"http://apple.com"]];
    [shareBuilder open];
}

【参考】
Google+ Sign-In for iOS - Google+ Platform for iOSGoogle Developers - https://developers.google.com/+/mobile/ios/sign-in
※日本語より英語の方がわかりやすい

Objective-C - Google+ Platform on iOS のプロジェクトへのインストール - Qiita - http://qiita.com/mag4n/items/ed97b3ff9fb486df3099

GPPShare クラス リファレンス - Google+ Platform for iOSGoogle Developers - https://developers.google.com/+/mobile/ios/api/interface_g_p_p_share?hl=ja
※リファレンス
ビルド設定の "Other Linker Flags" に "-ObjC" を設定する意味 - Over&Out その後 - http://d.hatena.ne.jp/shu223/20110426/1304694650
→staticライブラリーを使用する場合に「-ObjC」を設定する意味

以上、無精ひげは5日以上の伸ばすとかっこよく見えるとの話を聞いた堀でした。(無精ひげ2日目)