菜鸟教程小白 发表于 2022-12-12 13:25:08

ios - 如何在 iOS 中检测 wifi 或 3G 信号强度?


                                            <p><p>我正在使用 iOS 中的 MPMediaplayer 框架开发视频播放器,我需要在我的应用程序中播放两个视频。(即当用户有强网络信号时,我的第一个视频将播放)。
我的第二个视频播放器将在他们的网络低时播放。我需要在 wifi 或 3G 等中播放我的视频......我的第一件事是如何检测我的 iphone 手机中的 wifi 速度和 3G 速度。我需要以 MBPS 为单位获得移动 wifi 速度。我正在尝试编写一些代码。但这对我不起作用。提前致谢。</p>

<pre><code>#import &#34;Reachability.h&#34;

@interface NetworkViewController ()

@end

@implementation NetworkViewController

- (void)viewDidLoad
{
    ;   
    self.view.backgroundColor = ;
    ;

    networkLabel = [initWithFrame:CGRectMake(20, 100, 300, 40)];
    networkLabel.textColor = ;
    networkLabel.backgroundColor = ;
    networkLabel.userInteractionEnabled = NO;
    networkLabel.text = myNewString;

    ;
}

- (NSArray *)getDataCounters
{
    BOOL success;
    struct ifaddrs *addrs;

    const struct ifaddrs *cursor;
    const struct if_data *networkStatisc;

    int WiFiSent = 0;
    int WiFiReceived = 0;
    int WWANSent = 0;
    int WWANReceived = 0;

    NSString *name=[init];
    success = getifaddrs(&amp;addrs) == 0;

    if (success)
    {
         cursor = addrs;

         while (cursor != NULL)
         {
             name=;            
             NSLog(@&#34;ifa_name %s == %@\n&#34;, cursor-&gt;ifa_name,name);

             // names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN

             if (cursor-&gt;ifa_addr-&gt;sa_family == AF_LINK)
             {
               if ()
               {
                      networkStatisc = (const struct if_data *) cursor-&gt;ifa_data;   
                      WiFiSent+=networkStatisc-&gt;ifi_obytes;   
                      WiFiReceived+=networkStatisc-&gt;ifi_ibytes;
                      NSLog(@&#34;WiFiSent %d ==%d&#34;,WiFiSent,networkStatisc-&gt;ifi_obytes);
                      NSLog(@&#34;WiFiReceived %d ==%d&#34;,WiFiReceived,networkStatisc-&gt;ifi_ibytes);
                      NSLog(@&#34;wifi data is%.2f&#34;,(float)WiFiReceived/1048576);

                  myNewString = ;   
                  networkLabel.text = myNewString;
                }

                if ()
                {
                     networkStatisc = (const struct if_data *) cursor-&gt;ifa_data;   
                     WWANSent+=networkStatisc-&gt;ifi_obytes;
                     WWANReceived+=networkStatisc-&gt;ifi_ibytes;
                     NSLog(@&#34;WWANSent %d ==%d&#34;,WWANSent,networkStatisc-&gt;ifi_obytes);
                     NSLog(@&#34;WWANReceived %d ==%d&#34;,WWANReceived,networkStatisc-&gt;ifi_ibytes);
                }
            }

            cursor = cursor-&gt;ifa_next;
      }

      freeifaddrs(addrs);
    }

    return , ,,, nil];
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以播放 wifi 的高分辨率视频和蜂窝数据的低分辨率视频。</p>

<pre><code>Reachability *reachability = ;
;

NetworkStatus status = ;

if(status == NotReachable)
{
    //No internet
}
else if (status == ReachableViaWiFi)
{
    //WiFi    Play high resolution video
}
else if (status == ReachableViaWWAN)
{
    //3G    Play low resolution video
}
</code></pre>

<p>如果您将通过 3g 网络传输高分辨率视频,Apple 可以拒绝您的应用。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 iOS 中检测 wifi 或 3G 信号强度?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27631996/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27631996/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 iOS 中检测 wifi 或 3G 信号强度?