SGShareKit: Designing a modern iOS sharing UISunday, 27 Sep 2015, 12:17
By SimonRecently I faced the challenge to support sharing with various social services in an iOS 5+ App. This meant I wouldn’t be able to use the built in UIActivity* classes.
Personally I don’t like the look of the recently introduced UIActivityViewController. Instead I decided to build a simpler to use and customize API which can accomplish similar results and works on iOS 5: SGShareKit
The design is based on Levey’s LeveyPopListView but I added rotation support and provided the actual sharing implementations.
The central class is the SGShareView which is designed to work like an UIAlertView.
For each view instance a new UIWindow is created, which is displayed above all other Windows of your application. This is accomplished by setting the window level to UIWindowLevelAlert:
- (void)show {
self.myWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.myWindow.windowLevel = UIWindowLevelAlert;
self.myWindow.backgroundColor = [UIColor colorWithWhite:0 alpha:0.24];
self.myWindow.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIViewController *vC = [SGShareViewController new];
vC.view = self;
vC.wantsFullScreenLayout = YES;
self.myWindow.rootViewController = vC;
[self.myWindow makeKeyAndVisible];
}
The API itself is inspired by UIActivityViewController, but works on iOS 5+
It’s easy to use this, you just have to init ActivityView with the objects you want to share and call - (void)show; on the activity view. This is very similar to an UIAlertView:
#import "SGActivityView.h"
// ...
NSString *text = @"Hello world";
NSURL *url = [NSURL URLWithString:@"http://google.com"];
NSURL *mail = [NSURL URLWithString:@"mailto:[email protected]"];
SGActivityView *activity = [[SGActivityView alloc] initWithActivityItems:@[text, url, mail]
applicationActivities:nil];
[activity show];