Skip to content

Commit f861ced

Browse files
committed
initial commit
0 parents  commit f861ced

File tree

7 files changed

+208
-0
lines changed

7 files changed

+208
-0
lines changed

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
{
3+
"version": "0.1.0",
4+
"name": "com.simplertrading.simpler-share",
5+
"cordova_name": "SimplerShare",
6+
"description": "Share file to other application",
7+
"license": "Apache 2.0",
8+
"keywords": [
9+
"share",
10+
"share, open in"
11+
],
12+
"engines": []
13+
}

plugin.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
3+
id="cordova-plugin-simpler-share" version="0.2.3">
4+
<name>Simper Share</name>
5+
<description>Cordova Share Plugin</description>
6+
<license>Apache 2.0</license>
7+
<keywords>cordova,share</keywords>
8+
<js-module src="www/share.js" name="SharePlugin">
9+
<clobbers target="SharePlugin" />
10+
</js-module>
11+
<platform name="ios">
12+
<config-file target="config.xml" parent="/*">
13+
<feature name="SharePlugin">
14+
<param name="ios-package" value="SharePlugin"/>
15+
</feature>
16+
</config-file>
17+
<header-file src="src/ios/SharePlugin.h" />
18+
<source-file src="src/ios/SharePlugin.m" />
19+
<header-file src="src/ios/SharePluginViewController.h" />
20+
<source-file src="src/ios/SharePluginViewController.m" />
21+
</platform>
22+
</plugin>

src/ios/SharePlugin.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#import <Foundation/Foundation.h>
2+
#import <Cordova/CDVPlugin.h>
3+
4+
@interface SharePlugin : CDVPlugin {
5+
}
6+
7+
- (void)share:(CDVInvokedUrlCommand*)command;
8+
9+
@end

src/ios/SharePlugin.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#import "SharePlugin.h"
2+
#import "SharePluginViewController.h"
3+
4+
@implementation SharePlugin
5+
6+
- (void)share:(CDVInvokedUrlCommand*)command {
7+
8+
BOOL pluginSuccess = NO;
9+
CDVPluginResult* pluginResult = nil;
10+
NSString* message = [command.arguments objectAtIndex:0];
11+
if (message != nil && [message length] > 0) {
12+
13+
NSURL* filePath = [NSURL fileURLWithPath:message];
14+
SharePluginViewController* previewViewController = [[SharePluginViewController alloc] init];
15+
pluginSuccess = [previewViewController viewFile:message usingViewController:[super viewController]];
16+
17+
} else {
18+
}
19+
20+
}
21+
22+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@interface SharePluginViewController : UIViewController <UIDocumentInteractionControllerDelegate>
2+
{
3+
UIDocumentInteractionController* documentInteractionController;
4+
UIViewController* myViewController;
5+
}
6+
7+
@property (retain, nonatomic) UIDocumentInteractionController* documentInteractionController;
8+
@property (retain, nonatomic) UIViewController* myViewController;
9+
10+
- (BOOL)viewFile:(NSString *)filePath usingViewController: (UIViewController *) viewController;
11+
- (BOOL)openFile:(NSURL *)URL usingViewController: (UIViewController *) viewController;
12+
13+
@end
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#import "SharePluginViewController.h"
2+
#import <MobileCoreServices/MobileCoreServices.h> // For UTI
3+
4+
@implementation SharePluginViewController
5+
6+
@synthesize documentInteractionController, myViewController;
7+
8+
#pragma mark -
9+
#pragma mark Utility methods
10+
11+
- (NSString *)UTIForURL:(NSURL *)url
12+
{
13+
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)url.pathExtension, NULL);
14+
return (NSString *)CFBridgingRelease(UTI) ;
15+
}
16+
17+
- (BOOL) validateFileUrl: (NSString *)candidate
18+
{
19+
NSString* lowerCased = [candidate lowercaseString];
20+
return [lowerCased hasPrefix:@"file://"];
21+
}
22+
23+
#pragma mark -
24+
#pragma mark View / Share methods
25+
26+
- (BOOL)viewFile:(NSString *)filePath usingViewController:(UIViewController *) viewController
27+
{
28+
self.myViewController = viewController;
29+
NSURL* URL = [NSURL URLWithString:filePath];
30+
31+
if (![self validateFileUrl:filePath]) {
32+
URL = [[NSURL alloc] initFileURLWithPath:filePath];
33+
}
34+
35+
BOOL filePreviewSuccess = NO; // Success is true if it was possible to open the controller and there are apps available
36+
37+
if (URL) {
38+
// Initialize Document Interaction Controller
39+
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
40+
self.documentInteractionController.UTI = [self UTIForURL:URL];
41+
self.documentInteractionController.delegate = self;
42+
43+
UIView *filePreviewView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
44+
[[self.myViewController view] addSubview:filePreviewView];
45+
self.view = filePreviewView;
46+
47+
// Configure Document Interaction Controller
48+
[self.documentInteractionController setDelegate:self];
49+
50+
// Preview
51+
filePreviewSuccess = [self.documentInteractionController presentPreviewAnimated:YES];
52+
53+
if(!filePreviewSuccess) {
54+
// Next, try the options menu in openFile below...
55+
return [self openFile:URL usingViewController:viewController];
56+
}
57+
} else {
58+
return NO;
59+
}
60+
61+
return YES;
62+
}
63+
64+
- (BOOL) openFile:(NSURL *)URL usingViewController:(UIViewController *) viewController
65+
{
66+
self.myViewController = viewController;
67+
68+
BOOL fileOpenSuccess = NO; // Success is true if it was possible to open
69+
70+
if (URL) {
71+
// Initialize Document Interaction Controller
72+
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
73+
self.documentInteractionController.UTI = [self UTIForURL:URL];
74+
self.documentInteractionController.delegate = self;
75+
76+
// Configure Document Interaction Controller
77+
[self.documentInteractionController setDelegate:self];
78+
79+
// Open
80+
CGRect screenRect = [[viewController view] bounds];
81+
CGFloat screenWidth = screenRect.size.width;
82+
CGFloat screenHeight = screenRect.size.height;
83+
fileOpenSuccess = [self.documentInteractionController
84+
presentOptionsMenuFromRect:CGRectMake((screenWidth / 2), screenHeight, 1, 1)
85+
inView:[self.myViewController view] animated:YES];
86+
87+
if(!fileOpenSuccess) {
88+
// There is no app to handle this file
89+
NSString *deviceType = [UIDevice currentDevice].localizedModel;
90+
NSString *message = [NSString stringWithFormat:NSLocalizedString(
91+
@"Your %@ doesn't seem to have any other Apps installed that can open this document.",
92+
@"Your %@ doesn't seem to have any other Apps installed that can open this document."),
93+
deviceType];
94+
95+
// Display alert
96+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(
97+
@"No suitable Apps installed", @"No suitable App installed")
98+
message:message
99+
delegate:nil
100+
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
101+
otherButtonTitles:nil];
102+
[alert show];
103+
return NO;
104+
}
105+
} else {
106+
return NO;
107+
}
108+
109+
return YES;
110+
}
111+
112+
#pragma mark -
113+
#pragma mark Document Interaction Controller Delegate Methods
114+
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
115+
return self.myViewController;
116+
}
117+
118+
@end

www/share.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var exec = require('cordova/exec');
2+
3+
function SharePlugin() {};
4+
5+
SharePlugin.prototype = {
6+
share : function(content, success, fail){
7+
exec(success, fail, "SharePlugin", "share", [content]);
8+
}
9+
};
10+
11+
module.exports = new SharePlugin();

0 commit comments

Comments
 (0)