分享的原理很简单与iOS或Android下没有什么太大的区别。这篇是iOS平台的之后再写一篇Android平台的。
需要注意的C++如何去调用OC的方法,解决这个问题就解决了。
我们先说一下思路C++调用OC的方法,需要一个C的函数作为中转。
下面我们直接看代码吧。
先导入ShareSDK的第三方库。如果不会参考这边文章。
http://blog.csdn.net/qqmcy/article/details/10371957
先创建一个OC的类:
ShareViewController.h
// // ShareViewController.h // Dome // // Created by 杜甲 on 13-9-20. // // #import <UIKit/UIKit.h> @interface ShareViewController : UIViewController -(void)shareMothed; - (void)shareToSinaWeiboClickHandler; -(void)loginSina; @end
// // ShareViewController.m // Dome // // Created by 杜甲 on 13-9-20. // // #import "ShareViewController.h" #import <ShareSDK/ShareSDK.h> #define BUNDLE_NAME @"Resource" #define IMAGE_NAME @"sharesdk_img" #define IMAGE_EXT @"jpg" #define CONTENT @"ShareSDK不仅集成简单、支持如QQ好友、微信、新浪微博、腾讯微博等所有社交平台,而且还有强大的统计分析管理后台,实时了解用户、信息流、回流率、传播效应等数据,详情见官网http://sharesdk.cn @ShareSDK" #define SHARE_URL @"http://www.sharesdk.cn" @interface ShareViewController () @end @implementation ShareViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(void)loginSina { [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions :nil result:^(BOOL result, id<ISSUserInfo> userInfo, id<ICMErrorInfo> error) { if (result) { NSLog(@"%@",userInfo.nickname); } else { NSLog(@"失败"); } }]; } -(void)shareMothed { printf("fffbbbbbbbbbbb\n"); } - (void)shareToSinaWeiboClickHandler { //创建分享内容 NSString *imagePath = [[NSBundle mainBundle] pathForResource:IMAGE_NAME ofType:IMAGE_EXT]; id<ISSContent> publishContent = [ShareSDK content:CONTENT defaultContent:@"" image:[ShareSDK imageWithPath:imagePath] title:nil url:nil description:nil mediaType:SSPublishContentMediaTypeText]; //创建弹出菜单容器 id<ISSContainer> container = [ShareSDK container]; [container setIPadContainerWithView:nil arrowDirect:UIPopoverArrowDirectionUp]; id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES allowCallback:YES authViewStyle:SSAuthViewStyleFullScreenPopup viewDelegate:nil authManagerViewDelegate:nil]; //在授权页面中添加关注官方微博 [authOptions setFollowAccounts:[NSDictionary dictionaryWithObjectsAndKeys: [ShareSDK userFieldWithType:SSUserFieldTypeName value:@"ShareSDK"], SHARE_TYPE_NUMBER(ShareTypeSinaWeibo), [ShareSDK userFieldWithType:SSUserFieldTypeName value:@"ShareSDK"], SHARE_TYPE_NUMBER(ShareTypeTencentWeibo), nil]]; //显示分享菜单 [ShareSDK showShareViewWithType:ShareTypeSinaWeibo container:container content:publishContent statusBarTips:YES authOptions:authOptions shareOptions:[ShareSDK defaultShareOptionsWithTitle:nil oneKeyShareList:[NSArray defaultOneKeyShareList] qqButtonHidden:NO wxSessionButtonHidden:NO wxTimelineButtonHidden:NO showKeyboardOnAppear:NO shareViewDelegate:nil friendsViewDelegate:nil picViewerViewDelegate:nil] result:^(ShareType type, SSPublishContentState state, id<ISSStatusInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) { if (state == SSPublishContentStateSuccess) { NSLog(@"发表成功"); } else if (state == SSPublishContentStateFail) { NSLog(@"发布失败!error code == %d, error code == %@", [error errorCode], [error errorDescription]); } }]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
ChannelClass.h
// // ChannelClass.h // Dome // // Created by 杜甲 on 13-9-20. // // #ifndef __Dome__ChannelClass__ #define __Dome__ChannelClass__ #include "cocos2d.h" USING_NS_CC; class ChannelClass { public: void channel(); }; #endif /* defined(__Dome__ChannelClass__) */
// // ChannelClass.cpp // Dome // // Created by 杜甲 on 13-9-20. // // #include "ChannelClass.h" #include "ShareViewController.h" void shareMothed() { ShareViewController* share = [[ShareViewController alloc]init]; printf("ffff\n"); [share shareToSinaWeiboClickHandler]; } void ChannelClass::channel() { shareMothed(); }
这样我们只要调用这个C++中分享的函数就OK了。
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "ChannelClass.h" class HelloWorld : public cocos2d::CCLayer { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); ChannelClass* channel; // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } channel = new ChannelClass(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(100, 200)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); return true; } void HelloWorld::menuCloseCallback(CCObject* pSender) { channel->channel(); }
下面是代码例子的下载地址:
http://pan.baidu.com/share/link?shareid=1722659878&uk=3189484501