猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢!
原文地址: http://www.cocos2dev.com/?p=530
下午添加分享的时候,看着这么多第三方sdk,我还是喜欢ios7默认的分享功能,简洁大方。它也能显示你已安装的社交app。
IOSShareManager.h
//
// IOSShareManager.h
// IOS7ShareSample
//
// Created by LiuYanghui on 14-3-22.
//
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@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 IOSShareManager()
@property (strong, nonatomic) UIPopoverController *activityPopover;
@end
@implementation IOSShareManager
+ (IOSShareManager *) getInstance
{
static IOSShareManager* gameMgr = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
gameMgr = [[self alloc] init];
});
return gameMgr;
}
- (void)share:(CGPoint)pos
{
// 你可以先截屏保存到document下,这里的image读取document下面。
UIImage *image = [UIImage imageNamed:@"HelloWorld.png"];
NSString *mesg = @"You can add some description info here!";
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[image, mesg] applicationActivities:nil];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//iPhone 从底部向上滑出view
[self.viewController presentViewController:activityViewController animated:YES completion:nil];
} else {
//iPad, 弹出view
if (![self.activityPopover isPopoverVisible]) {
self.activityPopover = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[self.activityPopover presentPopoverFromRect:CGRectMake(pos.x, pos.y, 0, 0) inView:((UIViewController *)self.viewController).view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
//Dismiss view,当重复点击时
[self.activityPopover dismissPopoverAnimated:YES];
}
}
}
- (id)init
{
self = [super init];
if (self) {
// init code here
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
@end
//
// IOSShare.h
// IOS7ShareSample
//
// Created by LiuYanghui on 14-3-22.
//
//
#ifndef __IOS7ShareSample__IOSShare__
#define __IOS7ShareSample__IOSShare__
class IOSShare {
public:
/*! @brief share
*
* 显示分享view,如果是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.h"
void IOSShare::share(float posX, float posY)
{
[[IOSShareManager getInstance] share:CGPointMake(posX, posY)];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ....
[[IOSShareManager getInstance] setViewController:viewController];
}
IOSShare::share(300, 300);