Objective-C can't use stringWithContentsOfURL

2019-08-01 09:04发布

Hey, I'm trying to ping my server and check the version of the app - all goes well so far:

 NSURL *url = [NSURL URLWithString:@"http://thetalkingcloud.com/static/ping_desktop_app.php?v=1.1"];
  NSError *theNetworkError;
  NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&theNetworkError];
  //make sure something was returned
  if (!content) {
   //didn't connect
   //my code that goes here is all fine and works
  } else {
//there was some content returned by the server
//this NSLog prints out the expected data ('success' without the quotes)
   NSLog(@"server returned data: >|%@|<", content);
//PROBLEM:
//neither of these two EVER become true, even when the server DOES return success or update
   //connected, check what the server returned
   if (content == @"success") {
    NSLog(@"all went perfect");
   } else if (content == @"update") {
    NSLog(@"version error");
   }
  }

The IF statements aren't working for some reason, can anyone help me out please? Thanks.

2条回答
一纸荒年 Trace。
2楼-- · 2019-08-01 09:15

You are not checking the contents of content with your current conditional statement, you are checking to see if it is a valid object/pointer and not nil, which it is (valid) since you've just declared it.

Use NSString's length method and test for 0 (or isEqualToString:)

Also see this previous question for another alternative.

查看更多
【Aperson】
3楼-- · 2019-08-01 09:34

Use the isEqualToString: method, not pointer equality (==).

查看更多
登录 后发表回答