Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions iOS/MacAddress/Macaddress.h
Original file line number Diff line number Diff line change
@@ -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 <Cordova/CDV.h>

@interface Macaddress : CDVPlugin
@property(nonatomic, copy) NSString *callBackId;

@end



89 changes: 89 additions & 0 deletions iOS/MacAddress/Macaddress.m
Original file line number Diff line number Diff line change
@@ -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 <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

#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
40 changes: 40 additions & 0 deletions iOS/MacAddress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<h1>MacAddress plugin for Phonegap (Cordova) </h1>
<p>By Joey Wong (Huang Jun Yan) Updated for Cordova 2.4.0</p>


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.


<h1>INSTALL</h1>
=========================

1. Drag the <code>Macaddress.h, Macaddress.m </code>into the <code>"Plugins"</code> folder in Xcode.

2. Attach the <code>"macaddress.js"</code> to your <code>www</code> folder

3. Don't forget to enable the plugins in Cordova <code>config.xml</code> (for Cordova 2.3 or higher)

Add this:

<code>
&lt;plugin name="Macaddress" value="Macaddress" /&gt;
</code>

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.

<h1> How to use</h1>
==========================================
Get the WIFI MAC address by following script :

<pre>
plugins.macaddress.getMacAddress(function(response){
alert('MAC Address:'+response);
},function(errorMsg){
alert('Error:'+errorMsg);
});
</pre>

Voila !~
19 changes: 19 additions & 0 deletions iOS/MacAddress/macaddress.js
Original file line number Diff line number Diff line change
@@ -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);
40 changes: 40 additions & 0 deletions iOS/MacAddress/read.me
Original file line number Diff line number Diff line change
@@ -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 <code>Macaddress.h, Macaddress.m </code>into the <code>"Plugins"</code> folder in Xcode.

2. Attach the <code>"macaddress.js"</code> to your <code>www</code> folder

3. Don't forget to enable the plugins in Cordova <code>config.xml</code> (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 !