【问题标题】:App is not working with Cellular network connection(3G)应用程序无法使用蜂窝网络连接 (3G)
【发布时间】:2013-02-05 08:27:36
【问题描述】:

在我的 iPhone 应用程序中,我使用苹果的可达性类来检查网络可用性。 使用 Wifi 时它工作正常..但是当我使用移动网络(3G)的应用程序时,它不工作

我找不到解决方案。 如果有人遇到同样的问题..请帮助

编辑 - 这是我的可达性代码

AppDelegate.h

   #import <UIKit/UIKit.h>
   #import "Reachability.h"

   @class ViewController;

   @interface AppDelegate : UIResponder <UIApplicationDelegate>{
    Reachability* hostReach;
    Reachability* internetReach;
    Reachability* wifiReach;

    }
   @property (strong, nonatomic) UIWindow *window;
   @property (nonatomic,assign)BOOL networkAvailable;

  -(void)updateInterfaceWithReachability: (Reachability*) curReach;
  -(void)displayNetworkAvailability:(id)sender;

   @end

AppDelegate.m

 #import "AppDelegate.h"
 implementation AppDelegate

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//reachability
[[UIApplication sharedApplication]
 registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeAlert |
  UIRemoteNotificationTypeBadge |
  UIRemoteNotificationTypeSound)];

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

hostReach = [Reachability reachabilityWithHostName: @"www.google.com"] ;
[hostReach startNotifier];
[self updateInterfaceWithReachability: hostReach];

internetReach = [Reachability reachabilityForInternetConnection] ;
[internetReach startNotifier];
[self updateInterfaceWithReachability: internetReach];

wifiReach = [Reachability reachabilityForLocalWiFi] ;
[wifiReach startNotifier];
[self updateInterfaceWithReachability: wifiReach];




 -(void) updateInterfaceWithReachability: (Reachability*) curReach
  {

if(curReach == hostReach)
{

  }
if(curReach == internetReach)
{   NSLog(@"internet reach");
    //[self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
}
if(curReach == wifiReach)
{
    NSLog(@"wifi reach");
    //[self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
}
NetworkStatus hostStatus = [curReach currentReachabilityStatus];
switch (hostStatus)

 {
    case NotReachable:
    {
        NSLog(@"A gateway to the host server is down.");

        self.networkAvailable=NO;
        break;

    }
    case ReachableViaWiFi:
    {
        NSLog(@"A gateway to the host server is working via WIFI.");
        self.networkAvailable=YES;
        break;

    }
    case ReachableViaWWAN:
    {
        NSLog(@"A gateway to the host server is working via WWAN.");
        self.networkAvailable=YES;
        break;

    }
}
if (!self.networkAvailable ) {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NONETWORKFOUND"  object:@"NO"];


}else {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"NONETWORKFOUND"  object:@"YES"];




   }


 }

 - (void) reachabilityChanged: (NSNotification* )note{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];

    }

  - (void) displayNetworkAvailability:(id)sender{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldnot connect to server" message:@"Please check network connection!!"  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];


   [[NSNotificationCenter defaultCenter] postNotificationName:@"NONETWORKFOUND"  object:@"NO"];



   }

 @end

【问题讨论】:

  • Reachability 类中有 3g 和 wifi 两种方法,使用时贴出代码
  • 它不工作是什么意思?,你的应用程序崩溃了吗?,...
  • @SimonePistecchia 我已经添加了我的代码,请查看我的编辑
  • @tkanzakic -Nope 应用程序没有崩溃,无法登录(无法请求服务器 - 它显示正在加载)
  • 你有日志 NSLog(@"主机服务器的网关正在通过 WWAN 工作。");在 3g 模式下?

标签: iphone ios reachability


【解决方案1】:

我不明白你是否需要区分蜂窝和 WiFi,但如果你只需要一个类来检查互联网连接,你可以在这里查看我的 ALData 类: https://gist.github.com/andrealufino/4994901

【讨论】:

  • 请不要只宣传自己的github而不尝试解决OP
猜你喜欢
  • 1970-01-01
  • 2011-07-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多