Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:06:29+00:00 2026-06-16T04:06:29+00:00

I use NSString method initWithContentsOfURL:usedEncoding:error: to get content of some page. I notice, when

  • 0

I use NSString method initWithContentsOfURL:usedEncoding:error: to get content of some page. I notice, when page that i try to access is not exist this method is executed long time and then fail with timeout error. I tried to use NSURLRequest and NSURLConnection classes for the same purposes, but get the same result – execution long time and then timeout error.
When i try to open the same page in browser, i get response more quickly and it returns page is not available error.

It looks like cocoa methods don’t do a dns resolution for page name, or they have longer timeout for that operation.

So my question, does cocoa method that i use do dns resolve? How to do that if they didn’t?

Samples of code i use:

NSURL* url = [NSURL URLWithString:@"http://unexisting.domain.local"]; 
NSError* err = nil;
NSString* content = [NSString stringWithContentsOfURL:url usedEncoding:nil error:&err];

if (err) {
    NSLog(@"error: %@", err); 
} else {
    NSLog(@"content: %@", content);
}

NSURL* url = [NSURL URLWithString:@"http://unexisting.domain.local"]; 
NSURLRequest* request = [NSURLRequest requestWithURL:url];

NSURLResponse* response = nil;
NSError* err = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

if (err) {
    NSLog(@"error: %@", err); 
} else {
    NSString* content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"content: %@", content);
}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:06:30+00:00Added an answer on June 16, 2026 at 4:06 am

    Gene M. answered how to do that with using of SCNetworkReachability. Here is sample code:

    bool success = false;
    const char *host_name = [@"stackoverflow.com" 
                             cStringUsingEncoding:NSASCIIStringEncoding];
    
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
                                                                            host_name);
    SCNetworkReachabilityFlags flags;
    success = SCNetworkReachabilityGetFlags(reachability, &flags);
    bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && 
                                 !(flags & kSCNetworkFlagsConnectionRequired);
    if (isAvailable) {
        NSLog(@"Host is reachable: %d", flags);
    }else{
        NSLog(@"Host is unreachable");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When reading an NSString from a file I can use initWithContentsOfFile:usedEncoding:error: and it will
I use this method to access my mysql database from Xcode: NSString *URL =
I'm just looking at NSString.h and wonder why some method declaration args use (NSString
When I use the this method to initialize a UITabBarItem : - (id)initWithTitle:(NSString *)title
I have a simple function that returns an NSString after decoding it. I use
I have some NSString varibales that incude items like Ð and Õ and if
I see many discussions saying that I should use copy for NSString property because
I've read in the documentation that the NSString category method drawInRect is good for
I use NSString stringWithFormat method for create an URL string. But now I have
I want to force user to use my own init method (for example -(id)initWithString:(NSString*)foo;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.