2012-12-25 39 views
4

可能重複的可達性:
Undefined symbols for architecture armv7: 「_SCNetworkReachabilityCreateWithAddress」如何檢查服務器的

我試了幾件事情,我發現就在這裏,並沒有工作過。我將Apple的reachability.h和.m文件添加到我的項目中。我想測試大亨服務器的可達性,下面是我在我的實際程序中使用的代碼:

[[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) 

這也可能是一些顯而易見的,所以答案將不勝感激。謝謝

+0

這不完全相同。只有問題的第二部分由另一個問題回答。其他問題中沒有「如何檢查服務器的可達性」的第一部分。 – iDev

+0

@ACB你是對的,我會投票重新開放,但應該說,在一篇文章中結合兩個相當不同的問題被認爲是不好的做法,有充分的理由...... – jogojapan

+0

@jogojapan,是的,這是真的。他應該做一個谷歌搜索第二個問題,並會得到它。但在其他問題中提供的解決方案可能無助於他的情況。這可能是一個問題,因爲沒有爲他添加框架,而其他問題是基於已經添加的假設。所以有可能它甚至可能根本沒有幫助他。 – iDev

回答

3

如果要檢查特定主機是否可達,可以使用下面的代碼,

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

在這裏,你可以通過你的服務器名稱作爲主機名來檢查可達性。它應該工作。 查看apple documentation瞭解更多詳情。

對於您收到的錯誤消息,請嘗試將SystemConfiguration.framework添加到項目中。如果這沒有幫助,請檢查這個Undefined symbols for architecture armv7: "_SCNetworkReachabilityCreateWithAddress"

+0

可能是一個考慮不周的問題:我在哪裏可以獲得SystemConfiguration.framework,或者它已經在XCode上了,但是並不像我期望的那樣? – user1927534

+1

@ user1927534,檢查這個問題。按照答案中的相同步驟添加此框架,而不是那裏提到的框架。 http://stackoverflow.com/questions/3352664/how-to-add-existing-frameworks-in-xcode-4。另請查看http://blog.thinketg.com/blog/david-brainer-banker/xcode-4-tips-adding-frameworks-to-your-project。 – iDev

0

上didFinishLaunchingWithOptions方法放:

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; 

}

+0

最初聲明「isInternetConnectionAvailable」和「isNetworkNotifierCalledOnce」在哪裏? – user1927534