最近,看到App Store上有些应用可以直接把做好的视频或图片分享到Instagram上,而且是那种直接打开Instagram,并且可以直接编辑的方式,这种分享非常的友好,不需要用户去登录,只要安装了Instagram就可以直接跳转过去,相当于我们说的一键发布,省去了很多的麻烦。
可是,在我去Instagram的开发者网站看的时候,发现并没有说怎么直接打开Instagram的方式,然后花了将近5天的时间在网上找答案。我研究别人的应用发现,他们其实并没用使用Instagram的SDK,因为并没有登录授权这一步,就像使用了iOS的原生分享,跳过了中间选择平台的步骤那样,直接选了Instagram。所以,我猜测是使用了Instagram的一个URL Schemes!
但是这个URL Scheme是什么,官方文档也没给出来,知道今天,我在网上看到一个国外的博客,终于实现这个方法:
原文戳这里:点击打开链接
主要就是这个了:
instagram://library?AssetPath=[URL ENCODED STRING OF PHOTO LIBRARY ASSET URL]&InstagramCaption=[URL ENCODED MESSAGE]
于是,自己写了一个原生实现方法:
//
// SocialShare.m
// Unity-iPhone
//
#import "SocialShare.h"
#import <Social/Social.h>
#import <AssetsLibrary/AssetsLibrary.h>
NSString * const STR_SPLITTER = @"|";
NSString * const STR_EOF = @"endofline";
NSString * const STR_ARRAY_SPLITTER = @"%%%";
@interface SocialShare ()
{
NSMutableArray *imageArray;//经过压缩的图片
NSString* tempPath;
}
@property (nonatomic, strong) NSString *theMainPath;
@property (nonatomic, strong) NSString *theVideoName;
@end
@implementation GJCSocialShare
static GJCSocialShare * shared_Instance;
+ (id)sharedInstance {
if (shared_Instance == nil) {
shared_Instance = [[self alloc] init];
[shared_Instance initData];
}
return shared_Instance;
}
+ (void)PostShareToInstagram:(NSString*)videoPath
{
NSLog(@"save video url: %@", videoPath);
NSURL* instagramURL = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]){
NSString *caption = @"Some Preloaded Caption";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:videoPath] completionBlock:^(NSURL *assetURL, NSError *error) {
NSString *escapedString = [SocialShare urlencodedString:[assetURL absoluteString]];
NSString *escapedCaption = [SocialShare urlencodedString:caption];
NSURL *insURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@", escapedString, escapedCaption]];
[[UIApplication sharedApplication] openURL:insURL];
}];
}else{
NSLog(@"无法打开Instagram,请确定是否安装了Instagram!");
UnitySendMessage("NativeShare", "OnNativeShareUninstall", [DataConvertor NSStringToChar:@"instagram"]);
}
}
+ (NSString*)urlencodedString:(NSString *)message{
return [message stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
}
@end
@implementation DataConvertor
+(NSString *) charToNSString:(char *)value {
if (value != NULL) {
return [NSString stringWithUTF8String: value];
} else {
return [NSString stringWithUTF8String: ""];
}
}
+(const char *)NSIntToChar:(NSInteger)value {
NSString *tmp = [NSString stringWithFormat:@"%ld", (long)value];
return [tmp UTF8String];
}
+ (const char *) NSStringToChar:(NSString *)value {
return [value UTF8String];
}
+ (NSArray *)charToNSArray:(char *)value {
NSString* strValue = [GJC_DataConvertor charToNSString:value];
NSArray *array;
if([strValue length] == 0) {
array = [[NSArray alloc] init];
} else {
array = [strValue componentsSeparatedByString:STR_ARRAY_SPLITTER];
}
return array;
}
+ (const char *) NSStringsArrayToChar:(NSArray *) array {
return [DataConvertor NSStringToChar:[GJC_DataConvertor serializeNSStringsArray:array]];
}
+ (NSString *) serializeNSStringsArray:(NSArray *) array {
NSMutableString * data = [[NSMutableString alloc] init];
for(NSString* str in array) {
[data appendString:str];
[data appendString: STR_ARRAY_SPLITTER];
}
[data appendString: STR_EOF];
NSString *str = [data copy];
#if UNITY_VERSION < 500
[str autorelease];
#endif
return str;
}
+ (NSMutableString *)serializeErrorToNSString:(NSError *)error {
NSString* description = @"";
if(error.description != nil) {
description = error.description;
}
return [self serializeErrorWithDataToNSString:description code: (int) error.code];
}
+ (NSMutableString *)serializeErrorWithDataToNSString:(NSString *)description code:(int)code {
NSMutableString * data = [[NSMutableString alloc] init];
[data appendFormat:@"%i", code];
[data appendString: STR_SPLITTER];
[data appendString: description];
return data;
}
+ (const char *) serializeErrorWithData:(NSString *)description code: (int) code {
NSString *str = [DataConvertor serializeErrorWithDataToNSString:description code:code];
return [DataConvertor NSStringToChar:str];
}
+ (const char *) serializeError:(NSError *)error {
NSString *str = [DataConvertor serializeErrorToNSString:error];
return [DataConvertor NSStringToChar:str];
}
@end
然后建立一个桥接的方法:
extern "C" {
void _TS_ShareVideoWithInstagram(char* path) {
NSString *savePath = [DataConvertor charToNSString:path];
[SocialShare PostShareToInstagram:savePath];
}
}
在Unity端加入调用方法:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_IPHONE && !UNITY_EDITOR
using System.Runtime.InteropServices;
#endif
/// <summary>
/// 添加的库文件 AVKit,Social,AVFoundation,Foundation,MediaPlayer,AssetsLibrary
/// </summary>
public class NativeShare : MonoBehaviour {
[DllImport ("__Internal")]
private static extern void _TS_ShareImageWithInstagram(string savePath);
[DllImport ("__Internal")]
private static extern void _TS_ShareVideoWithInstagram(string savePath);
private static NativeShare _instance = null;
public static NativeShare Instance {
get {
if (_instance == null) {
_instance = GameObject.FindObjectOfType(typeof(NativeShare)) as NativeShare;
if (_instance == null) {
_instance = new GameObject ().AddComponent<NativeShare> ();
_instance.gameObject.name = _instance.GetType ().FullName;
}
}
return _instance;
}
}
/// <summary>
/// 将本地图片分享到 Instagram 上去
/// </summary>
/// <param name="imagePath"></param>
public void ShareImageWithInstagram(string imagePath){
#if UNITY_IPHONE && !UNITY_EDITOR
_TS_ShareImageWithInstagram(imagePath);
#endif
}
/// <summary>
/// 将本地视频分享到 Instagram 上去
/// </summary>
/// <param name="videoPath"></param>
public void ShareVideoWithInstagram(string videoPath){
#if UNITY_IPHONE && !UNITY_EDITOR
_TS_ShareVideoWithInstagram(videoPath);
#endif
}
private void OnNativeShareSuccess(string result){
// Debug.Log("success: " + result);
}
private void OnNativeShareCancel(string result){
// Debug.Log("cancel: " + result);
}
private void OnNativeShareUninstall(string platform){}
}
然后在其他地方调用ShareVideoWithInstagram
注意,我们要在Info.plist文件中添加一个参数:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
不然可能会崩溃哦!
最后,我们需要传递的是我们图片或者视频的存储的位置,这里没有写出图片的来,其实和视频差不多哦,多加个函数就好了,至于视频怎么保存,可以在网上搜索下,或者等下次我发个blog吧。。
希望可以帮到更多的人~