Explain.
IOS 에서 Screen Capture한 이미지를 Social.framework를 활용하여 공유하는 방법정리.
Sequence.
1. Application.CaptureScreenShot() 으로 캡쳐진행.
2. 위의 이미지를 가지고 IOS의 social.framwork를 활용하여 쉐어진행.
In Xcode
아래 2파일[SocialHelper.h , mm]을 현재 프로젝트의 Plugin/IOS 위치 시킨다.SocialHelper.h
#import <Foundation/Foundation.h> @interface SocialHelper : NSObject { } +(void)ImageToTwitter:(NSString*)filePath; +(void)ImageToFaceBook:(NSString*)filePath; @end
SocialHelper.mm
#import "SocialHelper.h" #import <Social/Social.h> #import <UIKit/UIKit.h> #import <UnityAppController.h> #pragma mark - Unity methods @implementation SocialHelper + (void) ImageToTwitter:(NSString *) filePath { UIViewController *rootViewController = [GetAppController() rootViewController]; UIImage *image = [UIImage imageWithContentsOfFile:filePath]; if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){ SLComposeViewController *mySLComposeSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; [mySLComposeSheet setInitialText:@"Let's Play Together!"]; [mySLComposeSheet addImage:image]; [mySLComposeSheet addURL:[NSURL URLWithString:@"https://myurl.com"]]; [mySLComposeSheet setCompletionHandler:^(SLComposeViewControllerResult result) { switch (result) { case SLComposeViewControllerResultCancelled: NSLog(@"Twtitter - POST Cancel"); break; case SLComposeViewControllerResultDone : NSLog(@"Twitter - Post Success"); break; default: break; } }]; [rootViewController presentViewController:mySLComposeSheet animated:YES completion:nil]; } } + (void) ImageToFaceBook:(NSString *) filePath { UIViewController *rootViewController = [GetAppController() rootViewController]; UIImage *image = [UIImage imageWithContentsOfFile:filePath]; if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){ SLComposeViewController *mySLComposeSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; [mySLComposeSheet setInitialText:@"Let's Play Together!"]; [mySLComposeSheet addImage:image]; [mySLComposeSheet addURL:[NSURL URLWithString:@"https://myurl.com"]]; [mySLComposeSheet setCompletionHandler:^(SLComposeViewControllerResult result) { switch (result) { case SLComposeViewControllerResultCancelled: NSLog(@"Facebook - POST Cancel"); break; case SLComposeViewControllerResultDone : NSLog(@"Facebook - Post Success"); break; default: break; } }]; [rootViewController presentViewController:mySLComposeSheet animated:YES completion:nil]; } } extern "C" { void PostImageToTwitter(const char * filePath) { NSLog(@"Share Iamge Path : %@", [NSString stringWithUTF8String:filePath]); [SocialHelper ImageToTwitter:[NSString stringWithUTF8String:filePath]]; } void PostImageToFacebook(const char * filePath) { NSLog(@"Share Iamge Path : %@", [NSString stringWithUTF8String:filePath]); [SocialHelper ImageToFaceBook:[NSString stringWithUTF8String:filePath]]; } } @end
In Unity
using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public enum SocialType : int { Twitter, Facebook } public class SocialHelper : MonoBehaviour { public static IEnumerator PostImage(SocialType type) { string fileName = getCaptureFileName(); Application.CaptureScreenshot(fileName); yield return new WaitForSeconds(1.0f); if (type == SocialType.Facebook) PostImageToFacebook(string.Format("{0}/{1}", Application.persistentDataPath, fileName)); if (type == SocialType.Twitter) PostImageToTwitter(string.Format("{0}/{1}", Application.persistentDataPath, fileName)); } static string getCaptureFileName() { return string.Format("capture_{0}.png", System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")); } [DllImport("__Internal")] static extern void PostImageToTwitter(string filePath); [DllImport("__Internal")] static extern void PostImageToFacebook(string filePath); }
SocialHelper.PostImage(SocialType.Facebook); // 이후 이런식으로 메소드를 호출시 공유가 진행.
Reference.
IOS 에서 Screen Capture한 이미지 Camera Roll에 등록하기.
Social.Framework