Skip to content

Instantly share code, notes, and snippets.

@sfszh
Last active December 20, 2015 17:39
Show Gist options
  • Save sfszh/6170542 to your computer and use it in GitHub Desktop.
Save sfszh/6170542 to your computer and use it in GitHub Desktop.
simple demo sharing to sina weibo using ShareSDK and Untiy3D in iOS. we share a sentence and a captured image.
//
// WeiboShare.h
// Unity-iPhone
//
// Created by Sophos Zhang on 6/25/13.
//
//
#import <Foundation/Foundation.h>
#import <ShareSDK/ShareSDK.h>
#import <QuartzCore/QuartzCore.h>
@interface WeiboShare : NSObject
@end
//
// WeiboShare.m
// Unity-iPhone
//
// Created by Sophos Zhang on 6/25/13.
//
//
#import "WeiboShare.h"
@implementation WeiboShare
bool initilized = false;
+ (void)InitilizePlat{
NSLog(@"initializing");
[ShareSDK registerApp:@""]; // add your ShareSDK APPkey here
[ShareSDK ssoEnabled:NO];
// sina weibo
[ShareSDK connectSinaWeiboWithAppKey:@"" //add your weibo appkey here
appSecret:@"" //
redirectUri:@""]; //
initilized = true;
}
CGImageRef UIGetScreenImage(void);
+ (void)Share:(const char*)rawJson {
if(!initilized){
[self InitilizePlat];
}
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);
NSString *sv = [[NSString alloc] initWithCString:rawJson encoding:enc];
NSString *sourceString = [[NSString alloc] initWithFormat:@"\357\273\277%@", sv]; // add bom
UIImage* img = [self imageWithScreenContents];
//构造分享内容
id<ISSContent> publishContent = [ShareSDK content: sv //@"weibotest]"
defaultContent:@"DefaultContent"
image: [ShareSDK pngImageWithImage: img]
title:@"ShareSDK"
url:@"http://www.sharesdk.cn"
description:@"这是一条测试信息"
mediaType:SSPublishContentMediaTypeNews];
NSLog(@"string 内容: %@", sourceString);
[ShareSDK shareContent:publishContent
type:ShareTypeSinaWeibo
authOptions:nil//authOptions
statusBarTips:YES
result:^(ShareType type, SSPublishContentState state, id<ISSStatusInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
if (state == SSPublishContentStateSuccess)
{
UnitySendMessage("-ShareManager", "ShowResult", "成功发送!");
NSLog(@"发表成功");
}
else if (state == SSPublishContentStateFail)
{
NSLog(@"发布失败!error code == %d, error code == %@", [error errorCode], [error errorDescription]);
}
}];
[sv release];
[sourceString release];
}
+ (UIImage *)imageWithScreenContents
{
CGImageRef cgScreen = UIGetScreenImage();
if (cgScreen) {
UIImage *result = [UIImage imageWithCGImage:cgScreen];
CGImageRelease(cgScreen);
return result;
}
return nil;
}
// bindng from unity
void _WeiboShare(const char* rawJson) {
[WeiboShare Share:rawJson];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment