Cocos2d-x add iOS7 default share/AirDrop

Source: Internet
Author: User

 

 

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);

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.