Facebook/Twitter/LINEへの「画像・テキスト・URL」の投稿をシンプルな記述で実行する「DCSocial 」クラスを iOS 7に最適化しました。
このクラスを使用する際は「Social 」フレームワークが必要になります。
主な機能は下記の5個です。今後随時更新して行きます。
主な機能
Facebookへの画像/テキスト/URLの投稿
Twitterへの画像/テキスト/URLの投稿
LINEへの画像投稿
LINEへテキスト投稿
メール/Twitter/Facebookに共有
下記よりサンプルとソースコードのダウンロードが行えます。
ダウンロード
使用方法
Facebookへの画像/テキスト/URLの投稿
1
[ DCSocial postToFacebook: self text: POST_TEXT imageName: POST_IMG_NAME url: POST_URL ];
Twitterへの画像/テキスト/URLの投稿
1
[ DCSocial postToTwitter: self text: POST_TEXT imageName: POST_IMG_NAME url: POST_URL ];
LINEへの JPEG画像投稿
1
2
3
4
5
6
typedef NS_ENUM ( NSUInteger , imageExtId ) {
JPEG = 0 ,
PNG = 1
};
[ DCSocial postImageToLine: POST_IMG_NAME imageType: JPEG ];
LINEへの PNG画像投稿
1
2
3
4
5
6
typedef NS_ENUM ( NSUInteger , imageExtId ) {
JPEG = 0 ,
PNG = 1
};
[ DCSocial postImageToLine: POST_IMG_NAME imageType: PNG ];
LINEへテキスト投稿
1
[ DCSocial postTextToLine: POST_TEXT ];
メール/Twitter/Facebook共有
1
[ DCSocial socialShare: self shareText: POST_TEXT shareImage: [ UIImage imageNamed: POST_IMG_NAME ]];
ソースコード
DCSocial.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#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 ) postImageToLine: ( NSString * ) imageName imageType: ( NSUInteger ) imageType ;
+ ( void ) postTextToLine: ( NSString * ) text ;
+ ( 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#import "DCSocial.h"
@implementation DCSocial
typedef NS_ENUM ( NSUInteger , imageExtId ) {
JPEG = 0 ,
PNG = 1
};
// 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 ) postImageToLine: ( NSString * ) imageName imageType: ( NSUInteger ) imageType
{
UIPasteboard * pasteboard = [ UIPasteboard generalPasteboard ];
if ( imageType == JPEG ) {
[ pasteboard setData: UIImageJPEGRepresentation ([ UIImage imageNamed: imageName ], 1 ) forPasteboardType: @"public.jpeg" ];
} else if ( imageType == PNG ) {
[ pasteboard setData: UIImagePNGRepresentation ([ UIImage imageNamed: imageName ]) forPasteboardType: @"public.png" ];
}
NSString * LineUrlString = [ NSString stringWithFormat: @"line://msg/image/%@" , pasteboard . name ];
[[ UIApplication sharedApplication ] openURL: [ NSURL URLWithString: LineUrlString ]];
}
// LINEへテキスト投稿
+ ( void ) postTextToLine: ( NSString * ) text
{
NSString * plainString = text ;
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 ];
}
// シェアする
+ ( 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
お薦めの参考書
絶対に挫折しない iPhoneアプリ開発「超」入門 増補改訂第4版
Swiftについて知りたい開発者の方のみならず、プログラミング未経験者の方にも参考になる内容になっています。Swiftの基礎を一から丁寧に解説されており、この書籍があればネットで調べる手間をかなり省くことができると思います。