mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-04-24 03:00:11 -04:00
Merge pull request #552 from andrewpthorp/master
UUID Generator (PhoneGap and Cordova versions)
This commit is contained in:
17
iOS/UniqueIdentifier/README.md
Normal file
17
iOS/UniqueIdentifier/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Unique Identifier support for iOS applications
|
||||
|
||||
_Created by `Andrew Thorp`_
|
||||
|
||||
Usage:
|
||||
|
||||
// To GENERATE a Unique ID
|
||||
PGUniqueIdentifier.generateUUID(function(result){
|
||||
// result will be the new UUID or the UUID that is stored in this apps NSDefaults
|
||||
});
|
||||
|
||||
// To GET the Unique ID
|
||||
PGUniqueIdentifier.getUUID(function(result){
|
||||
// result will be the UUID stored in this apps NSDefaults
|
||||
}, function(error){
|
||||
// If a UUID has not yet been generated, you will receive the error "ERROR".
|
||||
});
|
||||
40
iOS/UniqueIdentifier/UniqueIdentifier.h
Normal file
40
iOS/UniqueIdentifier/UniqueIdentifier.h
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// UniqueIdentifier.h
|
||||
// UniqueIdentifierPlugin
|
||||
//
|
||||
// Created by Andrew Thorp on 4/13/12
|
||||
//
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE ANDREW THORP "AS IS" AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
// EVENT SHALL ANDREW TRICE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#ifdef CORDOVA_FRAMEWORK
|
||||
#import <CORDOVA/CDVPlugin.h>
|
||||
#else
|
||||
#import "CORDOVA/CDVPlugin.h"
|
||||
#endif
|
||||
|
||||
@interface UniqueIdentifier : CDVPlugin {
|
||||
NSString* callbackId;
|
||||
NSString* uuidString;
|
||||
CFUUIDRef uuid;
|
||||
}
|
||||
|
||||
@property (nonatomic, copy) NSString* callbackID;
|
||||
|
||||
//Public Instance Method (visible in phonegap API)
|
||||
- (void) generateUUID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
|
||||
- (void) getUUID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
|
||||
|
||||
@end
|
||||
44
iOS/UniqueIdentifier/UniqueIdentifier.js
Normal file
44
iOS/UniqueIdentifier/UniqueIdentifier.js
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// UniqueIdentifier.js
|
||||
// UniqueIdentifierPlugin
|
||||
//
|
||||
// Created by Andrew Thorp on 4/13/12
|
||||
//
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE ANDREW THORP "AS IS" AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
// EVENT SHALL ANDREW TRICE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
|
||||
var UniqueIdentifier = function(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate new UUID
|
||||
*/
|
||||
UniqueIdentifier.prototype.generateUUID = function(success, fail){
|
||||
Cordova.exec(success, fail, "UniqueIdentifier", "generateUUID", []);
|
||||
};
|
||||
|
||||
/**
|
||||
* Return UUID stored in NSDefaults
|
||||
*/
|
||||
UniqueIdentifier.prototype.getUUID = function(success, fail){
|
||||
Cordova.exec(success, fail, "UniqueIdentifier", "getUUID", []);
|
||||
};
|
||||
|
||||
Cordova.addConstructor(function(){
|
||||
if (!window.plugins){
|
||||
window.plugins = {};
|
||||
}
|
||||
|
||||
window.plugins.uniqueIdentifier = new UniqueIdentifier();
|
||||
});
|
||||
68
iOS/UniqueIdentifier/UniqueIdentifier.m
Normal file
68
iOS/UniqueIdentifier/UniqueIdentifier.m
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// UniqueIdentifier.m
|
||||
// UniqueIdentifierPlugin
|
||||
//
|
||||
// Created by Andrew Thorp on 4/13/12
|
||||
//
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE ANDREW THORP "AS IS" AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
// EVENT SHALL ANDREW TRICE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
|
||||
#import "UniqueIdentifier.h"
|
||||
#define UUID_USER_DEFAULTS_KEY @"UUID"
|
||||
|
||||
@implementation UniqueIdentifier
|
||||
|
||||
@synthesize callbackID;
|
||||
|
||||
NSString* UNIQUE_IDENTIFIER_OK = @"OK";
|
||||
NSString* UNIQUE_IDENTIFIER_ERROR = @"ERROR";
|
||||
|
||||
- (void) generateUUID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
|
||||
{
|
||||
self.callbackID = [arguments pop];
|
||||
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
if ([defaults stringForKey:UUID_USER_DEFAULTS_KEY] == nil) {
|
||||
uuid = CFUUIDCreate(NULL);
|
||||
if (uuid) {
|
||||
uuidString = (NSString *)CFUUIDCreateString(NULL, uuid);
|
||||
CFRelease(uuid);
|
||||
}
|
||||
|
||||
[defaults setObject:uuidString forKey:UUID_USER_DEFAULTS_KEY];
|
||||
[defaults synchronize];
|
||||
}
|
||||
|
||||
NSString *uniqueIdentifier = [defaults stringForKey:UUID_USER_DEFAULTS_KEY];
|
||||
|
||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: uniqueIdentifier];
|
||||
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
|
||||
}
|
||||
|
||||
- (void) getUUID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
|
||||
{
|
||||
self.callbackID = [arguments pop];
|
||||
CDVPluginResult* pluginResult;
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
|
||||
if ([defaults stringForKey:UUID_USER_DEFAULTS_KEY] == nil){
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: UNIQUE_IDENTIFIER_ERROR];
|
||||
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
|
||||
} else {
|
||||
NSString *uniqueIdentifier = [defaults stringForKey:UUID_USER_DEFAULTS_KEY];
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: uniqueIdentifier];
|
||||
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
35
iPhone/UniqueIdentifier/PGUniqueIdentifier.h
Normal file
35
iPhone/UniqueIdentifier/PGUniqueIdentifier.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// PGUniqueIdentifier.h
|
||||
// UniqueIdentifierPlugin
|
||||
//
|
||||
// Created by Andrew Thorp on 4/13/12
|
||||
//
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE ANDREW THORP "AS IS" AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
// EVENT SHALL ANDREW TRICE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <PhoneGap/PGPlugin.h>
|
||||
|
||||
@interface PGUniqueIdentifier : PGPlugin {
|
||||
NSString* callbackId;
|
||||
NSString* uuidString;
|
||||
CFUUIDRef uuid;
|
||||
}
|
||||
|
||||
@property (nonatomic, copy) NSString* callbackID;
|
||||
|
||||
//Public Instance Method (visible in phonegap API)
|
||||
- (void) generateUUID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
|
||||
- (void) getUUID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
|
||||
|
||||
@end
|
||||
67
iPhone/UniqueIdentifier/PGUniqueIdentifier.m
Normal file
67
iPhone/UniqueIdentifier/PGUniqueIdentifier.m
Normal file
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// PGUniqueIdentifier.m
|
||||
// UniqueIdentifierPlugin
|
||||
//
|
||||
// Created by Andrew Thorp on 4/13/12
|
||||
//
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE ANDREW THORP "AS IS" AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
// EVENT SHALL ANDREW TRICE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
|
||||
#import "PGUniqueIdentifier.h"
|
||||
#define UUID_USER_DEFAULTS_KEY @"UUID"
|
||||
|
||||
@implementation PGUniqueIdentifier
|
||||
|
||||
@synthesize callbackID;
|
||||
|
||||
NSString* UNIQUE_IDENTIFIER_OK = @"OK";
|
||||
NSString* UNIQUE_IDENTIFIER_ERROR = @"ERROR";
|
||||
|
||||
- (void) generateUUID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
|
||||
{
|
||||
self.callbackID = [arguments pop];
|
||||
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
if ([defaults stringForKey:UUID_USER_DEFAULTS_KEY] == nil) {
|
||||
uuid = CFUUIDCreate(NULL);
|
||||
if (uuid) {
|
||||
uuidString = (NSString *)CFUUIDCreateString(NULL, uuid);
|
||||
CFRelease(uuid);
|
||||
}
|
||||
[defaults setObject:uuidString forKey:UUID_USER_DEFAULTS_KEY];
|
||||
[defaults synchronize];
|
||||
}
|
||||
|
||||
NSString *uniqueIdentifier = [defaults stringForKey:UUID_USER_DEFAULTS_KEY];
|
||||
|
||||
PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString: uniqueIdentifier];
|
||||
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
|
||||
}
|
||||
|
||||
- (void) getUUID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
|
||||
{
|
||||
self.callbackID = [arguments pop];
|
||||
PluginResult* pluginResult;
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
|
||||
if ([defaults stringForKey:UUID_USER_DEFAULTS_KEY] == nil){
|
||||
pluginResult = [PluginResult resultWithStatus:PGCommandStatus_ERROR messageAsString: UNIQUE_IDENTIFIER_ERROR];
|
||||
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
|
||||
} else {
|
||||
NSString *uniqueIdentifier = [defaults stringForKey:UUID_USER_DEFAULTS_KEY];
|
||||
pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString: uniqueIdentifier];
|
||||
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
17
iPhone/UniqueIdentifier/README.md
Normal file
17
iPhone/UniqueIdentifier/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Unique Identifier support for iOS applications
|
||||
|
||||
_Created by `Andrew Thorp`_
|
||||
|
||||
Usage:
|
||||
|
||||
// To GENERATE a Unique ID
|
||||
PGUniqueIdentifier.generateUUID(function(result){
|
||||
// result will be the new UUID or the UUID that is stored in this apps NSDefaults
|
||||
});
|
||||
|
||||
// To GET the Unique ID
|
||||
PGUniqueIdentifier.getUUID(function(result){
|
||||
// result will be the UUID stored in this apps NSDefaults
|
||||
}, function(error){
|
||||
// If a UUID has not yet been generated, you will receive the error "ERROR".
|
||||
});
|
||||
11
iPhone/UniqueIdentifier/UniqueIdentifier.js
Normal file
11
iPhone/UniqueIdentifier/UniqueIdentifier.js
Normal file
@@ -0,0 +1,11 @@
|
||||
var PGUniqueIdentifier = {
|
||||
|
||||
generateUUID: function(success, fail) {
|
||||
return PhoneGap.exec(success, fail, "PGUniqueIdentifier", "generateUUID", []);
|
||||
},
|
||||
|
||||
getUUID: function(success, fail) {
|
||||
return PhoneGap.exec(success, fail, "PGUniqueIdentifier", "getUUID", []);
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user