Facebook/Twitter/LINEへの画像/テキスト/URLの投稿をシンプルな記述で実行する「DCSocial」クラスを作成しました。
このクラスを使用する際は「Social」フレームワークが必要になります。
下記よりサンプルとソースコードのダウンロードが行えます。
ダウンロード
使用方法
Facebookへの画像/テキスト/URLの投稿
1
2
3
4
[ DCSocial postToFacebook: self
text: POST_TEXT
imageName: POST_IMG_NAME
url: POST_URL ];
Twitterへの画像/テキスト/URLの投稿
1
2
3
4
[ DCSocial postToTwitter: self
text: POST_TEXT
imageName: POST_IMG_NAME
url: POST_URL ];
LINEへの画像投稿
1
[ DCSocial postToLine: POST_IMG_NAME ];
ソースコード
DCSocial.h
1
2
3
4
5
6
7
8
9
10
11
12
#import <Foundation/Foundation.h>
#import <Social/Social.h>
@interface DCSocial : NSObject
#pragma mark - public method
+ ( void ) postToFacebook: ( id ) delegate text: ( NSString * ) text imageName: ( NSString * ) imageName url: ( NSString * ) url ;
+ ( void ) postToTwitter: ( id ) delegate text: ( NSString * ) text imageName: ( NSString * ) imageName url: ( NSString * ) url ;
+ ( void ) postToLine: ( NSString * ) imageName ;
+ ( void ) socialShare: ( id ) delegate shareText: ( NSString * ) shareText shareImage: ( UIImage * ) shareImage ;
@end
DCSocial.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#import "DCSocial.h"
@implementation DCSocial
// Facebookへ投稿
+ ( void ) postToFacebook: ( id ) delegate text: ( NSString * ) text imageName: ( NSString * ) imageName url: ( NSString * ) url
{
SLComposeViewController * slc = [ SLComposeViewController composeViewControllerForServiceType: SLServiceTypeFacebook ];
[ slc setInitialText: text ];
[ slc addImage: [ UIImage imageNamed: imageName ]];
[ slc addURL: [ NSURL URLWithString: url ]];
[ delegate presentViewController: slc animated: YES completion: nil ];
}
// Twitterへ投稿
+ ( void ) postToTwitter: ( id ) delegate text: ( NSString * ) text imageName: ( NSString * ) imageName url: ( NSString * ) url
{
SLComposeViewController * slc = [ SLComposeViewController composeViewControllerForServiceType: SLServiceTypeTwitter ];
[ slc setInitialText: text ];
[ slc addImage: [ UIImage imageNamed: imageName ]];
[ slc addURL: [ NSURL URLWithString: url ]];
[ delegate presentViewController: slc animated: YES completion: nil ];
}
// LINEへ投稿
+ ( void ) postToLine: ( NSString * ) imageName
{
UIPasteboard * pasteboard = [ UIPasteboard pasteboardWithUniqueName ];
[ pasteboard setData: UIImagePNGRepresentation ([ UIImage imageNamed: imageName ]) forPasteboardType: @"public.png" ];
NSString * LineUrlString = [ NSString stringWithFormat: @"line://msg/image/%@" , pasteboard . name ];
[[ UIApplication sharedApplication ] openURL: [ NSURL URLWithString: LineUrlString ]];
}
// シェアする
+ ( void ) socialShare: ( id ) delegate shareText: ( NSString * ) shareText shareImage: ( UIImage * ) shareImage
{
if ([ UIActivityViewController class ]) {
NSString * textToShare = shareText ;
UIImage * imageToShare = shareImage ;
NSArray * itemsToShare = [[ NSArray alloc ] initWithObjects: textToShare , imageToShare , nil ];
UIActivityViewController * activityVC = [[ UIActivityViewController alloc ] initWithActivityItems: itemsToShare applicationActivities: nil ];
activityVC . excludedActivityTypes = [[ NSArray alloc ] initWithObjects: UIActivityTypePrint , UIActivityTypeCopyToPasteboard , UIActivityTypeAssignToContact , UIActivityTypeSaveToCameraRoll , UIActivityTypeMessage , UIActivityTypePostToWeibo , nil ];
[ delegate presentViewController: activityVC animated: YES completion: nil ];
}
}
@end
サンプルソースコード
ViewController.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#import <UIKit/UIKit.h>
#import "DCSocial.h"
#define POST_TEXT @"via Dolice Alternative Design"
#define POST_IMG_NAME @"Pandora_640_1136.jpg"
#define POST_URL @"https: //dolice.net/mb/"
@interface ViewController : UIViewController
- ( IBAction ) postToFacebook: ( id ) sender ;
- ( IBAction ) postToTwitter: ( id ) sender ;
- ( IBAction ) postToLine: ( id ) sender ;
@end
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- ( void ) viewDidLoad
{
[ super viewDidLoad ];
}
- ( IBAction ) postToFacebook: ( id ) sender
{
[ DCSocial postToFacebook: self
text: POST_TEXT
imageName: POST_IMG_NAME
url: POST_URL ];
}
- ( IBAction ) postToTwitter: ( id ) sender
{
[ DCSocial postToTwitter: self
text: POST_TEXT
imageName: POST_IMG_NAME
url: POST_URL ];
}
- ( IBAction ) postToLine: ( id ) sender
{
[ DCSocial postToLine: POST_IMG_NAME ];
}
@end
お薦めの参考書
詳解 Swift 改訂版
Swiftのかなり入り込んだところまで解説しながら、実践コードを多数収録しています。応用本になりますので、入門書を読み終えて中級者から上級者に差し掛かる時に読むことをお勧めします。読み物としてだけではなくリファレンスとしても使用できます。