When I added the sharing in the afternoon, I still liked the default sharing Function of ios7 with so many third-party sdks, Which is concise and generous. It also displays your installed social apps.
Next I will explain how to use it in cocos2dx.
The following is the encapsulated IOSShareManager. h/m. This is an oc Singleton class that calls to share and save rootViewController. There are few codes. You can see the comments.
IOSShareManager. h
//// IOSShareManager.h// IOS7ShareSample//// Created by LiuYanghui on 14-3-22.////#import
#import
@interface IOSShareManager : NSObject+ (IOSShareManager *) getInstance;- (void)share:(CGPoint)pos;@property (nonatomic, readwrite, retain) id viewController;@end
IOSShareManager. m
/// IOSShareManager. m // IOS7ShareSample /// Created by LiuYanghui on 14-3-22. //// # import IOSShareManager. h @ interface trim () @ property (strong, nonatomic) UIPopoverController * activityPopover; @ end @ implementation progress + (response *) getInstance {static IOSShareManager * gameMgr = nil; static response onceToken; dispatch_once (& onceToken, ^ {gameMgr = [[self alloc] ini T];}); return gameMgr;}-(void) share :( CGPoint) pos {// you can capture the screenshot and save it to the document. The image here reads the document. UIImage * image = [UIImage imageNamed: @HelloWorld.png]; NSString * mesg = @ You can add some description info here !; UIActivityViewController * activityViewController = [[UIActivityViewController alloc] scheme: @ [image, mesg] applicationActivities: nil]; if ([[UIDevice currentDevice] userInterfaceIdiom] = UIUserInterfaceIdiomPhone) {// view [self. viewController presentViewController: activityViewController animated: YES completion: nil];} else {// iPad, view if (! [Self. activityPopover ispovervisible]) {self. activityPopover = [[UIPopoverController alloc] initWithContentViewController: activityViewController]; [self. activityPopover presentpoverfromrect: CGRectMake (pos. x, pos. y, 0, 0) inView :( (UIViewController *) self. viewController ). view permittedArrowDirections ctions: UIPopoverArrowDirectionAny animated: YES];} else {// Dismiss view. When you click it again, [self. activityPopover dismissPopoverAnimated: YES] ;}}- (id) init {self = [super init]; if (self) {// init code here} return self ;} -(void) dealloc {[super dealloc];} @ end
The above is the oc class, and the intermediate layer interface is added below for cpp to call. IOSShare. h/mm is the intermediate layer class method we need to add.
IOSShare. h
/// IOSShare. h // IOS7ShareSample /// Created by LiuYanghui on 14-3-22. /// # ifndef _ IOS7ShareSample _ IOSShare __# define _ IOS7ShareSample _ IOSShare _ class IOSShare {public :/*! @ Brief share ** displays the shared view. For ipad, view * @ param posX * @ param posY */static void share (float posX, float posY) ;};# endif/* defined (_ IOS7ShareSample _ IOSShare __)*/
IOSShare. mm
//// IOSShare.cpp// IOS7ShareSample//// Created by LiuYanghui on 14-3-22.////#include IOSShare.h#import IOSShareManager.hvoid IOSShare::share(float posX, float posY){ [[IOSShareManager getInstance] share:CGPointMake(posX, posY)];}
OK. Here is how to use it.
Step 1: Set the rootViewController of IOSShareManager.
In AppController. mm, add: # import "IOSShareManager. h" and set the IOSShareManager rootViewController in the following function.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // .... [[IOSShareManager getInstance] setViewController:viewController];}
Step 2: Call share in cpp. When the parameter coordinate is under the ipad, use this coordinate as the pop-up point to bring up the share view.
IOSShare::share(300, 300);