【问题标题】:How do I check the reachability of a server [duplicate]如何检查服务器的可达性[重复]
【发布时间】:2012-12-25 02:35:15
【问题描述】:

可能重复:
Undefined symbols for architecture armv7: “_SCNetworkReachabilityCreateWithAddress”

我尝试了一些我在这里找到的东西,但都没有奏效。我将 Apple 的 reachability.h 和 .m 文件添加到我的项目中。我正在尝试测试 mogul 服务器的可达性,以下是我在实际程序中使用的代码:

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

reach = [Reachability reachabilityForInternetConnection];
[reach startNotifier];

NetworkStatus remoteHostStatus = [reach currentReachabilityStatus];

if (remoteHostStatus == NotReachable) {
    NSLog(@"no");
    textTest.text = @"Can't reach it";
}
else if(remoteHostStatus == ReachableViaWiFi || remoteHostStatus == ReachableViaWWAN){
    NSLog(@"Yes");
    textTest.text = @"Got it";
}
testServer.hidden = YES;

....

-(void)handleNetworkChange:(NSNotification *)notice{
    NetworkStatus remoteHostStatus = [reach currentReachabilityStatus];

    if (remoteHostStatus == NotReachable) {
        NSLog(@"no");
        textTest.text = @"Can't get it";
    }
    else if(remoteHostStatus == ReachableViaWiFi || remoteHostStatus == ReachableViaWWAN){
       NSLog(@"Got it");
       textTest.text = @"Got it";
    }
}

在 .h 文件中:

@property (retain, nonatomic) Reachability *reach;

....

-(void)handleNetworkChange:(NSNotification *)notice;

每次我尝试编译和运行时,都会得到以下结果:

Ld /Users/ConorMccallion/Library/Developer/Xcode/DerivedData/newClient-aunayptutxnlhjeoflldefcmybpk/Build/Products/Debug-iphonesimulator/newClient.app/newClient normal i386
    cd /Volumes/TRAVELDRIVE/Reseaech/ClientSide/newClient
    setenv IPHONEOS_DEPLOYMENT_TARGET 6.0
    setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.0.sdk -L/Users/ConorMccallion/Library/Developer/Xcode/DerivedData/newClient-aunayptutxnlhjeoflldefcmybpk/Build/Products/Debug-iphonesimulator -F/Users/ConorMccallion/Library/Developer/Xcode/DerivedData/newClient-aunayptutxnlhjeoflldefcmybpk/Build/Products/Debug-iphonesimulator -filelist /Users/ConorMccallion/Library/Developer/Xcode/DerivedData/newClient-aunayptutxnlhjeoflldefcmybpk/Build/Intermediates/newClient.build/Debug-iphonesimulator/newClient.build/Objects-normal/i386/newClient.LinkFileList -Xlinker -objc_abi_version -Xlinker 2 -fobjc-link-runtime -Xlinker -no_implicit_dylibs -mios-simulator-version-min=6.0 -framework UIKit -framework Foundation -framework CoreGraphics -o /Users/ConorMccallion/Library/Developer/Xcode/DerivedData/newClient-aunayptutxnlhjeoflldefcmybpk/Build/Products/Debug-iphonesimulator/newClient.app/newClient

Undefined symbols for architecture i386:
  "_SCNetworkReachabilityCreateWithAddress", referenced from:
      +[Reachability reachabilityWithAddress:] in ViewController.o
      +[Reachability reachabilityWithAddress:] in Reachability.o
  "_SCNetworkReachabilityCreateWithName", referenced from:
      +[Reachability reachabilityWithHostName:] in ViewController.o
      +[Reachability reachabilityWithHostName:] in Reachability.o
  "_SCNetworkReachabilityGetFlags", referenced from:
      -[Reachability connectionRequired] in ViewController.o
      -[Reachability currentReachabilityStatus] in ViewController.o
      -[Reachability connectionRequired] in Reachability.o
      -[Reachability currentReachabilityStatus] in Reachability.o
  "_SCNetworkReachabilityScheduleWithRunLoop", referenced from:
      -[Reachability startNotifier] in ViewController.o
      -[Reachability startNotifier] in Reachability.o
  "_SCNetworkReachabilitySetCallback", referenced from:
      -[Reachability startNotifier] in ViewController.o
      -[Reachability startNotifier] in Reachability.o
  "_SCNetworkReachabilityUnscheduleFromRunLoop", referenced from:
      -[Reachability stopNotifier] in ViewController.o
      -[Reachability stopNotifier] in Reachability.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这可能是显而易见的,因此我们将不胜感激。谢谢

【问题讨论】:

  • 这不是完全重复的。另一个问题只回答了问题的第二部分。第一部分“如何检查服务器的可达性”在其他问题中不存在。
  • @ACB 你是对的,我会投票重新开放,但应该说在一篇文章中结合两个完全不同的问题被认为是不好的做法,有充分的理由......
  • @jogojapan,是的,这是真的。他应该在谷歌上搜索第二个问题并且会得到它。但是其他问题中提供的解决方案可能对他的情况没有帮助。这可能是一个问题,因为没有为他添加框架,其他问题是基于它已经添加的假设。所以有可能它甚至可能对他根本没有帮助。

标签: iphone objective-c ios xcode reachability


【解决方案1】:

如果要检查特定主机的可达性,可以使用以下代码,

Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];

您可以在此处将您的服务器名称作为主机名来检查可达性。它应该工作。 查看apple documentation 了解更多详情。

对于您收到的错误消息,请尝试将SystemConfiguration.framework 添加到项目中。如果这没有帮助,请查看此Undefined symbols for architecture armv7: "_SCNetworkReachabilityCreateWithAddress"

【讨论】:

【解决方案2】:

关于 didFinishLaunchingWithOptions 方法的 put:

self.internetReach= [Reachability reachabilityWithHostName:@"your host name e.g.www.apple.com"];

[internetReach startNotifier];


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


[self performSelector:@selector(reachabilityChanged:) withObject:[NSNotification notificationWithName:kReachabilityChangedNotification object:internetReach]];

这将调用下面的方法

- (void)reachabilityChanged:(NSNotification*)note

{

Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

if(curReach == self.internetReach)
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    switch (netStatus)
    {
        case ReachableViaWiFi:
        {            
            isInternetConnectionAvilable=YES;
            if(isNetworkNotifierCalledOnce)
            {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Message!" message:@"Internet Available Now" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                [alert release];
            }
            break;
        }

        case NotReachable:
        {     

            isInternetConnectionAvilable=NO;

            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Message!" message:@"No Internet Connectivity" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [alert show];

            [alert release];

            break;
        }
    }

}

isNetworkNotifierCalledOnce=YES;

}

【讨论】:

  • 最初声明的“isInternetConnectionAvailable”和“isNetworkNotifierCalledOnce”在哪里?
猜你喜欢
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
相关资源
最近更新 更多