这次记录一下Unity接入Twitter分享的SDK,做海外版本开发的应该都会接触到。
由于项目中使用的是XUPorter管理IOS的SDK,所以进行扩展即可。
关于UnityIOS的SDK管理方案XUPorter可以看下这个大佬的文章:
https://blog.csdn.net/u013457933/article/details/87990998
1.下载SDK:
更新一下:
https://github.com/touren/twitter-kit-ios/releases/tag/v5.2.0
上面的TwitterKit里用到了UIWebView,提审会不通过。这个老哥改出来了一个不用UIWebView的版本。
2.接下来接入:
a.在application:didFinishLaunchingWithOptions里初始化SDK,可以理解为这是IOS程序启动时候的方法,在这里初始化比较合理。
初始化方法如下:(xxxxx填入你的SDK参数)
[[Twitter sharedInstance] startWithConsumerKey:@"xxxxxxxxxxxxx" consumerSecret:@"xxxxxxxxxxxxxxxxxxx"];
b.在application:openURL:options里调用下面的方法。
[[Twitter sharedInstance] application:application openURL:url options:options];
c.提供一个extern c方法给unity调用。(Unity要调IOS代码只能这么调)
extern "C"
{
void _share2Twitter(char* imagePath);
}
void _share2Twitter(char* imagePath)
{
NSString *strReadAddr = [NSString stringWithUTF8String:imagePath];
UIImage *image = [UIImage imageWithContentsOfFile:strReadAddr];
if ([[Twitter sharedInstance].sessionStore hasLoggedInUsers]) {
TWTRComposer *composer = [[TWTRComposer alloc] init];
[composer setImage:image];
[composer showFromViewController:GetAppController().window.rootViewController completion:^(TWTRComposerResult result){
if(result == TWTRComposerResultCancelled) {
//success
}else{
//failed
}
}];
} else {
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
if (session) {
TWTRComposer *composer = [[TWTRComposer alloc] init];
[composer setImage:image];
[composer showFromViewController:GetAppController().window.rootViewController completion:^(TWTRComposerResult result){
if(result == TWTRComposerResultCancelled) {
//success
}else{
//failed
}
}];
} else {
}
}];
}
}
3.在C#中调用分享接口。
4.Xcode注意事项:
此SDK需要SafariServices.framework和Accounts.framework,给加上
由于这个SDK的版本很乱,编译出来缺framework补上就行。