2011-09-30 47 views

回答

1

首先還需要獲得Reachability.h和Reachability.m文件

* 你可以得到可達性文件從這裏: - *

http://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

還需要在你想檢查網絡連接的文件中導入.h文件

下面定義的變量在.h文件中

NSMutableData *urlData; 
NSMutableString *currentElementValue; 

還定義的NSMutableString

屬性下面是URLConnection的

reachability = [Reachability reachabilityForInternetConnection]; 
    [reachability startNotifier]; 

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 

    if(remoteHostStatus == NotReachable) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert Title" 
                 message:@"Internet connection not currently available." 
                 delegate:nil 
               cancelButtonTitle:nil 
               otherButtonTitles:@"Ok", nil]; 
     [alert show]; 
     [alert release]; 

     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

     return; 
    } 

    NSString *post = [NSString stringWithFormat:@""]; 

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding 
          allowLossyConversion:NO]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 

    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your Information Which You Want To Pass"]]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 
    urlData=[[NSMutableData alloc] init]; 
    [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 

代碼XML解析以下重要方法

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [urlData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [urlData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 

} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    xmlParser = [[NSXMLParser alloc] initWithData:urlData]; 

    [xmlParser setDelegate:self]; 

    [xmlParser parse]; 

    [urlData release]; 

} 

#pragma mark - 
#pragma mark XML Parsing Delegate Methods 

- (void)parserDidStartDocument:(NSXMLParser *)parser 
{ 

} 

-(void)parserDidEndDocument:(NSXMLParser *)parser 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict 
{ 

} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    NSCharacterSet *charsToTrim = [NSCharacterSet whitespaceAndNewlineCharacterSet]; 
    string = [string stringByTrimmingCharactersInSet:charsToTrim]; 

    if(!currentElementValue) 
     currentElementValue = [[NSMutableString alloc] initWithString:string]; 
    else 
     [currentElementValue appendString:string]; 
    //NSLog(@"Current Element Value :- '%@'",currentElementValue); 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    [currentElementValue release]; 
    currentElementValue = nil; 
} 

你也可以中止下面

[xmlParser abortParsing]; 
的解析由