简单ios url请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单ios url请求相关的知识,希望对你有一定的参考价值。

Simple synchronous web request for ios.
// Credit to the original authors:
// Sam Walton
// Ethan Irish
// www.seven-labs.com
// Be kind & share :)
  1. // Quick synchronous URL request
  2. NSURL *url = [NSURL URLWithString:@"http://some-internet-place"];
  3. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  4. NSURLResponse *response;
  5. NSError *error;
  6. NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  7. NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  8. // need to check for errors, like if it was a bad call there will be no responsestring
  9. // but your error will have some data
  10. if(!error)
  11. {
  12. NSLog(@"response = %@", responseString);
  13.  
  14. // Take all data and split into array by comma
  15. NSArray* mountArray = [responseString componentsSeparatedByString: @","];
  16. // Just a test var called testItem at the index of 10
  17. NSString* testItem = [mountArray objectAtIndex: 10];
  18. NSLog(@"test item at 10 = %@", testItem);
  19. }
  20. // Authors:
  21. // Sam Walton
  22. // Ethan Irish
  23. // www.seven-labs.com
  24. // Be kind & share :)

以上是关于简单ios url请求的主要内容,如果未能解决你的问题,请参考以下文章

简单ios url请求

根据图片的url地址下载图片到本地保存代码片段

java网络URL请求编程入门

前端面试题之手写promise

为啥这段代码会泄露? (简单的代码片段)

代码片段 - Golang 实现简单的 Web 服务器