diff --git a/iOS/MacAddress/Macaddress.h b/iOS/MacAddress/Macaddress.h new file mode 100644 index 00000000..1905d433 --- /dev/null +++ b/iOS/MacAddress/Macaddress.h @@ -0,0 +1,17 @@ +// +// Macaddress.h +// Get the Wifi Mac Address +// +// Created by Joey W.(Huang Jun Yan) on 17/3/13. +// Copyright (c) 2013 Appswood. All rights reserved. +// + +#import + +@interface Macaddress : CDVPlugin +@property(nonatomic, copy) NSString *callBackId; + +@end + + + diff --git a/iOS/MacAddress/Macaddress.m b/iOS/MacAddress/Macaddress.m new file mode 100644 index 00000000..cbbcd23d --- /dev/null +++ b/iOS/MacAddress/Macaddress.m @@ -0,0 +1,89 @@ +// +// Macaddress.m +// Get the Wifi Mac Address +// +// Created by Joey W.(Huang Jun Yan) on 17/3/13. +// Copyright (c) 2013 Appswood. All rights reserved. +// + +#include +#include +#include +#include + +#import "Macaddress.h" + +@implementation Macaddress + +- (void)getMacAddress:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options +{ + self.callBackId=[arguments objectAtIndex:0]; + + int mgmtInfoBase[6]; + char *msgBuffer = NULL; + size_t length; + unsigned char macAddress[6]; + struct if_msghdr *interfaceMsgStruct; + struct sockaddr_dl *socketStruct; + NSString *errorFlag = NULL; + + // Setup the management Information Base (mib) + mgmtInfoBase[0] = CTL_NET; // Request network subsystem + mgmtInfoBase[1] = AF_ROUTE; // Routing table info + mgmtInfoBase[2] = 0; + mgmtInfoBase[3] = AF_LINK; // Request link layer information + mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces + + // With all configured interfaces requested, get handle index + if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0) + errorFlag = @"if_nametoindex failure"; + else + { + // Get the size of the data available (store in len) + if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0) + errorFlag = @"sysctl mgmtInfoBase failure"; + else + { + // Alloc memory based on above call + if ((msgBuffer = malloc(length)) == NULL) + errorFlag = @"buffer allocation failure"; + else + { + // Get system information, store in buffer + if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0) + errorFlag = @"sysctl msgBuffer failure"; + } + } + } + + if (errorFlag != NULL) + { + errorFlag = @"No MAC address found"; + + } + + interfaceMsgStruct = (struct if_msghdr *) msgBuffer; + + socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1); + + memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6); + + NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", + macAddress[0], macAddress[1], macAddress[2], + macAddress[3], macAddress[4], macAddress[5]]; + + free(msgBuffer); + + if([macAddressString length] > 0) + { + //NSLog(@"Mac Address: %@", macAddressString); + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:macAddressString]; + [super writeJavascript:[result toSuccessCallbackString:self.callBackId]]; + }else { + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorFlag]; + [super writeJavascript:[result toErrorCallbackString:self.callBackId]]; + } + +} + +@end diff --git a/iOS/MacAddress/README.md b/iOS/MacAddress/README.md new file mode 100644 index 00000000..a728875f --- /dev/null +++ b/iOS/MacAddress/README.md @@ -0,0 +1,40 @@ +

MacAddress plugin for Phonegap (Cordova)

+

By Joey Wong (Huang Jun Yan) Updated for Cordova 2.4.0

+ + +This is the plugins to help you get the iOS WIFI MAC address + +The UDID API is deprecated in iOS 5. To get a unique identifier, maybe you need to turn to iPhones/iPad/iPod touch' MAC address instead. + + +

INSTALL

+========================= + +1. Drag the Macaddress.h, Macaddress.m into the "Plugins" folder in Xcode. + +2. Attach the "macaddress.js" to your www folder + +3. Don't forget to enable the plugins in Cordova config.xml (for Cordova 2.3 or higher) + +Add this: + + +<plugin name="Macaddress" value="Macaddress" /> + + +For older version, please modify the PhoneGap.plist file of your application. Under the key "Plugins" add another one with key name +Macaddress and value Macaddress. + +

How to use

+========================================== +Get the WIFI MAC address by following script : + +
+plugins.macaddress.getMacAddress(function(response){
+                                    alert('MAC Address:'+response);
+                                  },function(errorMsg){
+                                    alert('Error:'+errorMsg);
+                                  });
+
+ +Voila !~ diff --git a/iOS/MacAddress/macaddress.js b/iOS/MacAddress/macaddress.js new file mode 100644 index 00000000..ce69b823 --- /dev/null +++ b/iOS/MacAddress/macaddress.js @@ -0,0 +1,19 @@ +var Macaddress = function(){} + +Macaddress.prototype = { + getMacAddress : function(onSuccess,onError) { + cordova.exec(onSuccess, onError, 'Macaddress', 'getMacAddress', []); + }, + +}; + +Macaddress.install = function(){ + if(!window.plugins){ + window.plugins = {}; + } + + window.plugins.macaddress = new Macaddress(); + return window.plugins.macaddress; +}; + +cordova.addConstructor(Macaddress.install); diff --git a/iOS/MacAddress/read.me b/iOS/MacAddress/read.me new file mode 100644 index 00000000..62318926 --- /dev/null +++ b/iOS/MacAddress/read.me @@ -0,0 +1,40 @@ +MacAddress plugin for Phonegap (Cordova) +By Joey Wong (Huang Jun Yan) Updated for Cordova 2.4.0 + + +This is the plugins to help you get the iOS WIFI MAC address + +The UDID API is deprecated in iOS 5. To get a unique identifier, maybe you need to turn to iPhones' MAC address instead. + + +INSTALL +========================= + +1. Drag the Macaddress.h, Macaddress.m into the "Plugins" folder in Xcode. + +2. Attach the "macaddress.js" to your www folder + +3. Don't forget to enable the plugins in Cordova config.xml (for Cordova 2.3 or higher) + +Add this: + + + + +For older version, please modify the PhoneGap.plist file of your application. Under the key "Plugins" add another one with key name +Macaddress and value Macaddress. + +How to use +========================================== +Get the WIFI MAC address by following script : + + +plugins.macaddress.getMacAddress(function(response){ + alert('MAC Address:'+response); + },function(errorMsg){ + alert('Error:'+errorMsg); + }); + + +Voila ! +