준비할것
- 카카오톡 API 홈페이지 들어가서 준비를 한다.
- 나머지 프레임 워크도 추가한다.
#import <MessageUI/MessageUI.h>
#import <KakaoOpenSDK/KakaoOpenSDK.h>
#import <Social/Social.h>
#import <Accounts/Accounts.h>
MFMessageComposeViewControllerDelegate 델리게이트를 등록한다. 메시지 전송 팝업을 컨트롤 할때 사용한다.
@interface ViewController () <MFMessageComposeViewControllerDelegate> {
}
@end
아래 코드는 실제 동작하는 코드
- (void) shareWithIndex:(NSInteger)buttonIndex text:(NSString *)text image:(UIImage *)image imageURLString:(NSString *)imageurl/*카톡 이미지 공유에서 쓰임*/ url:(NSURL *)url {
if (buttonIndex==0) {
// 문자 메세지
[self shareMessageWithText:text image:image url:url];
} else if (buttonIndex==1) {
// 카카오톡
if ([KOAppCall canOpenKakaoTalkAppLink]) {
// 카카오톡 공유
[self kakaoWithText:text image:image imageURLString:imageurl url:url];
} else {
// 카카오톡 설치
[self openInstallKakaoAlert];
}
} else if (buttonIndex==2) {
// 페이스북
[self shareWithServiceType:SLServiceTypeFacebook Text:text image:image url:url];
} else if (buttonIndex==3) {
// 트위터
[self shareWithServiceType:SLServiceTypeTwitter Text:text image:image url:url];
}
}
#pragma mark - 메시지
- (void) shareMessageWithText:(NSString *)text image:(UIImage *)image url:(NSURL *)url {
if(![MFMessageComposeViewController canSendText]) {
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"메시지 보내기 기능을 지원하지 않습니다." message:@" " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[warningAlert show];
return;
} else {
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText]) {
controller.body = [NSString stringWithFormat:@"%@\n\n%@", text, url];
// controller.recipients = recipients;
controller.messageComposeDelegate = self;
NSData *data = UIImageJPEGRepresentation(image, 0);
[controller addAttachmentData:data typeIdentifier:@"image/jpg" filename:@"thumbnail"];
[self presentViewController:controller animated:YES completion:nil];
}
}
}
#pragma mark - 메시지 전송 delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[controller dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 카카오톡
- (void) kakaoWithText:(NSString *)text image:(UIImage *)image imageURLString:(NSString *)imageurl url:(NSURL *)url {
// 카카오톡
KakaoTalkLinkAction *androidAppAction
= [KakaoTalkLinkAction createAppAction:KakaoTalkLinkActionOSPlatformAndroid
devicetype:KakaoTalkLinkActionDeviceTypePhone
marketparam:nil
execparam:@{@"kakaoFromData":[NSString stringWithFormat:@"{seq:\"%@\", type:\"%@\"}", self.dataInfo[@"contentsSeq"], self.dataInfo[@"contentsType"]]}];
KakaoTalkLinkAction *iphoneAppAction
= [KakaoTalkLinkAction createAppAction:KakaoTalkLinkActionOSPlatformIOS
devicetype:KakaoTalkLinkActionDeviceTypePhone
marketparam:nil
execparam:@{@"kakaoFromData":[NSString stringWithFormat:@"{seq:\"%@\", type:\"%@\"}", self.dataInfo[@"contentsSeq"], self.dataInfo[@"contentsType"]]}];
NSString *buttonTitle = @"앱으로 이동";
NSMutableArray *linkArray = [NSMutableArray array];
KakaoTalkLinkObject *button
= [KakaoTalkLinkObject createAppButton:buttonTitle
actions:@[androidAppAction, iphoneAppAction]];
[linkArray addObject:button];
/*[NSString stringWithFormat:@"%@ (%@)",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"], LOC(@"msg_invite_kakao", @"경영전문대학원 MBA 모바일 주소록 앱")]*/
if (text) {
KakaoTalkLinkObject *label;
label = [KakaoTalkLinkObject createLabel:text];
[linkArray addObject:text];
}
if (imageurl && image) {
KakaoTalkLinkObject *kimage
= [KakaoTalkLinkObject createImage:imageurl/*self.dataInfo[@"thumbnail1"]*/
width:image.size.width
height:image.size.height];
[linkArray addObject:kimage];
}
[KOAppCall openKakaoTalkAppLink:linkArray];
}
- (void) openInstallKakaoAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"카카오톡이 설치되어 있지 않습니다."
message:@"카카오톡을 설치하겠습니까?"// @"Do you want to install the KakaoTalk?"
delegate:self
cancelButtonTitle:@"취소"
otherButtonTitles:@"확인", nil];
alert.tag = 141;
[alert show];
}
#pragma mark - Alert View Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag==141) {
if (buttonIndex==[alertView cancelButtonIndex]) {
// cancel
} else {
// 카카오톡 링크로 이동
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/kr/app/id362057947"]];
}
}
}
#pragma mark - 페이스북 트위터
- (void) shareWithServiceType:(NSString *)serviceType Text:(NSString *)text image:(UIImage *)image url:(NSURL *)url {
if ([SLComposeViewController isAvailableForServiceType:serviceType]) {
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:serviceType];
if (text) [mySLComposerSheet setInitialText:text];
if (image) [mySLComposerSheet addImage:image];
if (url) [mySLComposerSheet addURL:url];
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
NSLog(@"Post Canceled");
break;
case SLComposeViewControllerResultDone:
NSLog(@"Post Sucessful");
break;
default:
break;
}
}];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
} else {
[[[UIAlertView alloc] initWithTitle:@"실패" message:@" " delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil] show];
}
}
페이스북, 트윗 등등 다른 기본 소셜 공유기능을 사용하려면
SOCIAL_EXTERN NSString *const SLServiceTypeTwitter NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeFacebook NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeSinaWeibo NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeTencentWeibo NS_AVAILABLE(10_9, 7_0);
SOCIAL_EXTERN NSString *const SLServiceTypeLinkedIn NS_AVAILABLE(10_9, NA);
상수 스트링을 사용하면 된다. 이 포스팅에서는 SLServiceTypeFacebook, SLServiceTypeTwitter 만 사용했다.