IP address of iOS device

I need to get the IP address of an iOS device. GStack.GetLocalAddressList returns all the addresses including the one for the Cellular interface.
How can I get the address associated with the wifi interface (en0)?



@widi
if a similar question was asked on a different language platform, it doesn't mean that there is an answer for the one I specified - read the tags - it says "delphi" "firemonkey". It's like asking a question in C# and rejoice getting a code example in COBOL.



Answers

You'd have to implement your own IP address enumeration that also returns adapter info. Here's an example:



uses IPAddressHelper, IdStack, IdGlobal;

procedure TForm2.Button1Click(Sender: TObject);
var
LocalIPs: TIdStackLocalAddressList;
I: Integer;
begin
Memo1.Lines.Clear;
TIdStack.IncUsage;
try
LocalIPs := TIdStackLocalAddressList.Create;
try
GStack.GetLocalAddressList(LocalIPs);
for I := 0 to LocalIPs.Count - 1 do
begin
if LocalIPs[I] is TIdStackLocalAddressIPv4Ex then
Memo1.Lines.Add(TIdStackLocalAddressIPv4Ex(LocalIPs[I]).IfaName + LocalIPs[I].IPAddress + ' ' + BoolToStr(TIdStackLocalAddressIPv4Ex(LocalIPs[I]).IsWifi, True));
end;
finally
LocalIPs.Free;
end;
finally
TIdStack.DecUsage;
end;
end;


This unit implements an advanced Stack class that includes the adapter name and some functions that help decide if a network is cellular or wifi.



unit IPAddressHelper;

interface

uses
Classes,
IdStack,
IdStackConsts,
IdGlobal,
IdStackBSDBase,
IdStackVCLPosix;

type
TIdStackLocalAddressIPv4Ex = class(TIdStackLocalAddressIPv4)
protected
FFlags: Cardinal;
FIfaName: string;
public
function IsWifi: Boolean;
function IsPPP: Boolean;
property IfaName: string read FIfaName;
constructor Create(ACollection: TCollection; const AIPAddress, ASubNetMask: string; AName: MarshaledAString; AFlags: Cardinal); reintroduce;
end;

TIdStackLocalAddressIPv6Ex = class(TIdStackLocalAddressIPv6)
protected
FFlags: Cardinal;
FIfaName: string;
public
function IsWifi: Boolean;
function IsPPP: Boolean;
property IfaName: string read FIfaName;
constructor Create(ACollection: TCollection; const AIPAddress: string; AName: MarshaledAString; AFlags: Cardinal); reintroduce;
end;

TIdStackVCLPosixEx = class(TIdStackVCLPosix)
public
procedure GetLocalAddressList(AAddresses: TIdStackLocalAddressList); override;
end;

implementation

uses
Posix.Base,
Posix.NetIf,
Posix.NetinetIn,
SysUtils;

function getifaddrs(ifap: pifaddrs): Integer; cdecl; external libc name _PU + 'getifaddrs'; {do not localize}
procedure freeifaddrs(ifap: pifaddrs); cdecl; external libc name _PU + 'freeifaddrs'; {do not localize}

procedure TIdStackVCLPosixEx.GetLocalAddressList(AAddresses: TIdStackLocalAddressList);
var
LAddrList, LAddrInfo: pifaddrs;
LSubNetStr: String;
begin
if getifaddrs(@LAddrList) = 0 then
try
AAddresses.BeginUpdate;
try
LAddrInfo := LAddrList;
repeat
if (LAddrInfo^.ifa_addr <> nil) and ((LAddrInfo^.ifa_flags and IFF_LOOPBACK) = 0) then
begin
case LAddrInfo^.ifa_addr^.sa_family of
Id_PF_INET4: begin
if LAddrInfo^.ifa_netmask <> nil then begin
LSubNetStr := TranslateTInAddrToString( PSockAddr_In(LAddrInfo^.ifa_netmask)^.sin_addr, Id_IPv4);
end else begin
LSubNetStr := '';
end;
TIdStackLocalAddressIPv4Ex.Create(AAddresses, TranslateTInAddrToString( PSockAddr_In(LAddrInfo^.ifa_addr)^.sin_addr, Id_IPv4), LSubNetStr, LAddrInfo^.ifa_name, LAddrInfo^.ifa_flags);
end;
Id_PF_INET6: begin
TIdStackLocalAddressIPv6Ex.Create(AAddresses, TranslateTInAddrToString( PSockAddr_In6(LAddrInfo^.ifa_addr)^.sin6_addr, Id_IPv6), LAddrInfo^.ifa_name, LAddrInfo^.ifa_flags);
end;
end;
end;
LAddrInfo := LAddrInfo^.ifa_next;
until LAddrInfo = nil;
finally
AAddresses.EndUpdate;
end;
finally
freeifaddrs(LAddrList);
end;
end;

const
IFF_UP = $1;
IFF_BROADCAST = $2;
IFF_LOOPBACK = $8;
IFF_POINTOPOINT = $10;
IFF_MULTICAST = $8000;

{ TIdStackLocalAddressIPv4Ex }

constructor TIdStackLocalAddressIPv4Ex.Create(ACollection: TCollection;
const AIPAddress, ASubNetMask: string; AName: MarshaledAString; AFlags: Cardinal);
begin
inherited Create(ACollection, AIPAddress, ASubnetMask);
FFlags := AFlags;
if Assigned(AName) then
FIfaName := AName;
end;

function TIdStackLocalAddressIPv4Ex.IsPPP: Boolean;
// The network connection to the carrier is established via PPP
// so GPRS, EDGE, UMTS connections have the flag IFF_POINTOPOINT set
begin
Result := (FFlags and (IFF_UP or IFF_POINTOPOINT) = (IFF_UP or IFF_POINTOPOINT))
and (FFlags and (IFF_LOOPBACK) = 0);
end;

function TIdStackLocalAddressIPv4Ex.IsWifi: Boolean;
// WLAN connections support Multicast
// WLAN connections do not use PPP
// Filter out the loopback interface (just for completeness, in case the
// network enumeration is changed so that loopback is also included)
begin
Result := ((FFlags and (IFF_UP or IFF_MULTICAST)) = (IFF_UP or IFF_MULTICAST))
and (FFlags and (IFF_LOOPBACK or IFF_POINTOPOINT) = 0);
end;

{ TIdStackLocalAddressIPv6Ex }

constructor TIdStackLocalAddressIPv6Ex.Create(ACollection: TCollection;
const AIPAddress: string; AName: MarshaledAString; AFlags: Cardinal);
begin
inherited Create(ACollection, AIPAddress);
FFlags := AFlags;
if Assigned(AName) then
FIfaName := AName;
end;

function TIdStackLocalAddressIPv6Ex.IsPPP: Boolean;
begin
Result := (FFlags and (IFF_UP or IFF_POINTOPOINT) = (IFF_UP or IFF_POINTOPOINT))
and (FFlags and (IFF_LOOPBACK) = 0);
end;

function TIdStackLocalAddressIPv6Ex.IsWifi: Boolean;
begin
Result := ((FFlags and (IFF_UP or IFF_MULTICAST)) = (IFF_UP or IFF_MULTICAST))
and (FFlags and (IFF_LOOPBACK or IFF_POINTOPOINT) = 0);
end;

initialization
SetStackClass(TIdStackVCLPosixEx);
end.


Please keep in mind that a wifi interface might also use a metered connection if it connects to a wireless hotspot.





php update quantity on cart page

I want to update the quantity of an item in the cart



These are the actions:



   if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM menu WHERE item_code='" . $_GET["code"] . "'");
$itemArray = array($productByCode[0]["item_code"]=>array('name'=>$productByCode[0]["item_name"], 'code'=>$productByCode[0]["item_code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["item_price"]));

if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["item_code"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["item_code"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;

case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}


This is the cart code:



<?php       
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["name"]; ?></strong></td>
<td><?php echo $item["quantity"]; ?></td>
<td><?php echo "₹".$item["price"]; ?></td>
<td><a id="remove" href="index.php?action=remove&code=<?php echo $item["code"]; ?>"><span class="fa fa-times fa-lg" style="color:red;"></span></a></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>


I need something like when click on update button so it can take the value and update the session array of particular product quantity
Please help me out...



Answers

This is a pretty basic example, but you should make a function per action so you can more easily port the cart. Also, I am not sure why you have a $_GET in the switch and a $_POST in the case():



function AddToCart($default = 1)
{
// Set a default quantity (1). Check that the value is set and is number
// Not sure why you are using a get for the code but not for these
// attributes. Should it not be $_GET['quantity']??
$qty = (isset($_POST['quantity']) && is_numeric($_POST['quantity']))? $_POST['quantity']:$default;
// Check that there is a valid numeric itemcode
$item = (isset($_POST['item_code']) && is_numeric($_POST['item_code']))? $_POST['item_code']:false;
// If false, stop
if(!$item)
return;
// Do your product query
$productByCode = $db_handle->runQuery("SELECT * FROM menu WHERE item_code='" . $_GET["code"] . "'");
// Assign the item code
$itmcd = $_GET["code"];
$itemArray['code'] = $itmcd;
$itemArray['name'] = $productByCode[0]["item_name"];
$itemArray['price'] = $productByCode[0]["item_price"];
$itemArray['quantity'] = (isset($_SESSION["cart_item"][$itmcd]))? $_SESSION["cart_item"][$itmcd]['quantity']+$qty : $qty;

$_SESSION["cart_item"][$itmcd] = $itemArray;
}


To use:



if(isset($_GET["action"]) && $_GET["action"] == 'add'))
AddToCart();




Value array selector as a variable jquery

I hope someone can help me. I been crashing down my head trying to end up with a solution for this, here is the deal, I have a function passing through some variables like this:



function createTaskView(results, listName, container, url, groupby) {


The variables are so simples, "results" it's an array, the other ones are simply strings. So far so good.



Then I start an $.each loop in the array like this:



function createTaskView(results, listName, container, url, groupby) {

$.each(results, function (index, task) {
var tr = '<tr>' +
'<td><a href="'+ url +'Lists/'+ listName +'/EditForm.aspx?ID='+ task.ID +' ">' + task.ID + '</a></td>' +
'<td>' + task.Year + '</td>' +
'<td>' + task.Site_x0020_Name + '</td>' +
'</tr>';

$(container + ' .' + task.groupby).append(tr);

});

});


The problems starts here



$(container + ' .' + task.groupby).append(tr);


I know there is no key in my DB call "groupby", I meant to use the string value passed into the function in here:



function createTaskView(results, listName, container, url, groupby) {


So, if the variable groupby is like this:



var groupby = Year;


I want this:



task.groupby


To be equal like this:



task.Year


I don't know if I make myself clear.
Please someone help me.



Thanks.



Answers

You need to use bracket notation to access a property whose name is stored in a variable so



$(container + ' .' + task[groupby]).append(tr);


Also make sure that the value of container is a proper selector like #myid for an id selector not just myid





↑このページのトップヘ