iOS를 지원하는 상위 수준의 API는 없지만 iOS는 단순히 예쁘기 때문에 유닉스에서는 물론 표준 API를 사용할 수있다.
이 경우 장치의 다양한 인터페이스를 설명하는 메모리 구조를 검색하는 getifaddrs를 찾고있을 것입니다.
그런 다음 원하는 인터페이스 세부 정보를 얻기 위해 이러한 C 구조를 구문 분석해야합니다.
은 문서화 된 (사람 getifaddr)이며이를 사용할 수 있습니다.
약간의 코드가 추가되지만 처음에는 몇 가지주의 사항이 추가됩니다. 애플은이 물건을 유지 관리하는 것을 정말로 신경 쓰지 않아서 네트워크 수는 32 비트이므로 4Gb 롤오버를 처리해야합니다. 패킷 수는 대부분 거기에 있습니다, 나는 tx 패킷 수는 항상 0이지만 누가 패킷 수에 관심이 있다고 생각합니다.
getifaddrs를 검색하면 더 나은 코드를 쉽게 찾을 수 있지만 시작하려면 간단한 버전을 사용하십시오.
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if_var.h>
struct ifaddrs* addrs;
int getIFAddrsResult = getifaddrs(&addrs);
if (getIFAddrsResult == 0)
{
struct ifaddrs* addrsPtr = addrs; // need to keep original for freeifaddrs
while (addrsPtr != NULL)
{
NSString *name = [NSString stringWithFormat:@"%s",addrsPtr->ifa_name];
//NSLog(@"ifa_name %s == %@\n", cursor->ifa_name,name);
// names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN
if (addrsPtr->ifa_addr->sa_family == AF_LINK)
{
const struct if_data* stats = (const struct if_data *) addrsPtr->ifa_data;
// We only consider the active data interface, and that will normally be the '0' interface.
if ([name isEqualToString:@"en0"])
{
// Wifi
// look at stats->ifi_obytes & ifi_ibytes
} else if ([name isEqualToString:@"pdp_ip0"])
{
// mobile broad band
} else {
// could be bluetooth, usb, ad hoc wifi, out of band signalling...
}
}
addrsPtr = addrsPtr->ifa_next;
}
freeifaddrs(addrs);
} else {
NSLog(@"getifaddrs failed with error : %s", strerror(getIFAddrsResult));
return nil;
}
P. 내 다이아몬드 부활 오래된 질문 배지 갈거야 :)
당신은 귀하의 응용 프로그램 또는 전반적인 사용량을 추적하는거야? – sangony