Skip to content

Instantly share code, notes, and snippets.

@eccfcco15
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eccfcco15/bf76159f229825164cdf to your computer and use it in GitHub Desktop.
Save eccfcco15/bf76159f229825164cdf to your computer and use it in GitHub Desktop.
SharingAddon.m
#import "SharingAddon.h"
#import "StandaloneCodeaViewController.h"
#import "lua.h"
#import "lauxlib.h"
#pragma mark - Lua Functions
static int share(struct lua_State *L);
static int email(struct lua_State *L);
static int print(struct lua_State *L);
#pragma mark - Lua Function Mappings
static const luaL_Reg sharingLibs[] =
{
{"share", share},
{"email", email},
{"print", print},
{NULL, NULL}
};
#pragma mark - Codea Hidden Functions
struct image_type_t;
struct image_type_t *checkimage(lua_State *L, int i);
typedef enum PersistenceImageAlpha
{
PersistenceImageAlphaPremultiply,
PerseistenceImageAlphaNoPremultiply
} PersistenceImageAlpha;
UIImage *createUIImageFromImage(struct image_type_t *image, PersistenceImageAlpha alphaOption);
#pragma mark - Sharing Addon
@interface SharingAddon ()
@property (nonatomic, weak) StandaloneCodeaViewController *currentCodeaController;
@property UIPopoverController *popup;
@end
@implementation SharingAddon
#pragma mark - Singleton
+ (instancetype) sharedInstance
{
static id _sharedObject = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
#pragma mark - Sharing Utility Methods
- (void)shareWithMessage:(NSString *)text andImage:(UIImage* )image
{
NSArray *activityItems = @[text, image];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
if ([activityVC respondsToSelector:@selector(popoverPresentationController)]) // iOS 8
{
activityVC.popoverPresentationController.sourceView = self.currentCodeaController.view;
activityVC.popoverPresentationController.sourceRect = CGRectMake(670, 30, 19, 27);
[self.currentCodeaController presentViewController:activityVC animated:YES completion:nil];
}
else // iOS 7
{
self.popup = [[UIPopoverController alloc] initWithContentViewController:activityVC];
[self.popup presentPopoverFromRect:CGRectMake(670, 30, 19, 27) inView:self.currentCodeaController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
- (void)sendContactUsEmail
{
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
mailVC.mailComposeDelegate = self;
[mailVC setMessageBody:@"" isHTML:NO];
[mailVC setSubject:@"Feedback"];
[mailVC setToRecipients:@[@"PiWorksLLC@gmail.com"]];
[self.currentCodeaController presentViewController:mailVC animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self.currentCodeaController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Codea Addon Protocol Implementation
- (void) codeaDidRegisterAddon:(StandaloneCodeaViewController *)controller
{
NSLog(@"Registered Sharing Addon");
}
- (void) codea:(StandaloneCodeaViewController*)codeaController didCreateLuaState:(struct lua_State*)L
{
self.currentCodeaController = codeaController;
//Register sharing functions with Lua
luaL_openlib(L, "sharing", sharingLibs, 0);
}
#pragma mark - Lua Function Implementations
static int share(struct lua_State* L)
{
NSString *message = [NSString stringWithCString:lua_tostring(L, 1) encoding:NSUTF8StringEncoding];
struct image_type_t *imageType = checkimage(L, 2);
UIImage *picture = createUIImageFromImage(imageType, PerseistenceImageAlphaNoPremultiply);
dispatch_async(dispatch_get_main_queue(), ^{
[[SharingAddon sharedInstance] shareWithMessage:message andImage:picture];
});
return 0;
}
static int email(struct lua_State *L)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[SharingAddon sharedInstance] sendContactUsEmail];
});
return 0;
}
static int print(struct lua_State* L)
{
NSLog([NSString stringWithFormat:[NSString stringWithCString:lua_tostring(L, 1) encoding:NSUTF8StringEncoding]]);
return 0;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment