29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSのOS登録情報を使って認証なしでFacebook・Twitterでシェアする(LINEも追加)

Last updated at Posted at 2014-06-08

メモ

事前にProject>TARGET>Build Phases>Link Binaly With Librariesで、Social.frameworkを追加しておく。

↑LINEだけなら必要ないです。

Social.h
#import <Foundation/Foundation.h>

@interface Social : NSObject
+(void)facebookShare:(UIViewController *)vc;
+(void)twitterShare:(UIViewController *)vc;
+(void)lineShare:(UIViewController *)vc;
@end
Social.m
#import "Social.h"
#import <Social/Social.h>

@implementation Social

+(void)facebookShare:(UIViewController *)vc
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        void (^completion) (SLComposeViewControllerResult result) = ^(SLComposeViewControllerResult result) {
            [composeViewController dismissViewControllerAnimated:YES completion:nil];
        };
        [composeViewController setCompletionHandler:completion];
        [composeViewController setInitialText:@"テスト"];
        [composeViewController addURL:[NSURL URLWithString:@"http://google.com"]];
        [vc presentViewController:composeViewController animated:YES completion:Nil];
    }
}


+(void)twitterShare:(UIViewController *)vc
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        void (^completion) (SLComposeViewControllerResult result) = ^(SLComposeViewControllerResult result) {
            [composeViewController dismissViewControllerAnimated:YES completion:nil];
        };
        [composeViewController setCompletionHandler:completion];
        [composeViewController setInitialText:@"テスト"];
        [composeViewController addURL:[NSURL URLWithString:@"http://google.com"]];
        [vc presentViewController:composeViewController animated:YES completion:Nil];
    }
}

+(void)lineShare:(UIViewController *)vc {
    NSString *plainString = @"テスト";
    NSString *contentKey = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)plainString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
    NSString *contentType = @"text";
    NSString *urlString = [NSString stringWithFormat: @"http://line.naver.jp/R/msg/%@/?%@", contentType, contentKey];
    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];
}

@end

呼び出し時

SampleViewController.m
#import "Social.h"
・・・
[Social facebookShare:self];
or
[Social twitterShare:self];
or
[Social lineShare:self];
29
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?