下午添加分享的時候,看著這麼多第三方sdk,我還是喜歡ios7默認的分享功能,簡潔大方。它也能顯示你已安裝的社交app。
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 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);