Objective-C 中的 Url 加载系统

URL 加载在访问 URL(即来自 internet 的项目)时很有用。它使用了以下类:

  • NSMutableURLRequest
  • NSURLConnection
  • NSURLCache
  • NSURLAuthenticationChallenge
  • NSURLCredential
  • NSURLProtectionSpace
  • NSURLResponse
  • NSURLDownload
  • NSURLSession

下面是一个简单的 url 加载实例。这不能在命令行上运行。我们需要创建 Cocoa 应用程序。

这可以通过在 XCode 中选择 New,然后在出现的窗口的 OSX 应用程序部分下投影并选择 Cocoa 应用程序来完成。

单击 "下一步" 完成一系列步骤,系统将要求您提供项目名称,您可以为其命名。

appdelegate.h 文件如下:

  1. #import <Cocoa/Cocoa.h>
  2. @interface AppDelegate : NSObject <NSApplicationDelegate>
  3. @property (assign) IBOutlet NSWindow *window;
  4. @end

更新 AppDelegate.m 文件如下:

  1. #import "AppDelegate.h"
  2. @interface SampleClass:NSObject<NSURLConnectionDelegate> {
  3. NSMutableData *_responseData;
  4. }
  5. - (void)initiateURLConnection;
  6. @end
  7. @implementation SampleClass
  8. - (void)initiateURLConnection {
  9. // Create the request.
  10. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://date.jsontest.com"]];
  11. // Create url connection and fire request
  12. NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  13. [conn start];
  14. }
  15. #pragma mark NSURLConnection Delegate Methods
  16. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  17. // A response has been received, this is where we initialize the instance var you created
  18. // so that we can append data to it in the didReceiveData method
  19. // Furthermore, this method is called each time there is a redirect so reinitializing it
  20. // also serves to clear it
  21. _responseData = [[NSMutableData alloc] init];
  22. }
  23. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  24. // Append the new data to the instance variable you declared
  25. [_responseData appendData:data];
  26. }
  27. - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
  28. willCacheResponse:(NSCachedURLResponse*)cachedResponse {
  29. // Return nil to indicate not necessary to store a cached response for this connection
  30. return nil;
  31. }
  32. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  33. // The request is complete and data has been received
  34. // You can parse the stuff in your instance variable now
  35. NSLog(@"%@",[[NSString alloc]initWithData:_responseData encoding:NSUTF8StringEncoding]);
  36. }
  37. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  38. // The request has failed for some reason!
  39. // Check the error var
  40. }
  41. @end
  42. @implementation AppDelegate
  43. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  44. SampleClass *sampleClass = [[SampleClass alloc]init];
  45. [sampleClass initiateURLConnection];
  46. // Insert code here to initialize your application
  47. }
  48. @end

结果如下:

  1. 2022-07-07 16:50:31.953 NSURLConnectionSample[1444:303] {
  2. "time": "11:20:31 AM",
  3. "milliseconds_since_epoch": 1380453631948,
  4. "date": "07-07-2022"
  5. }

在上面的程序中,我们创建了一个简单的 URL 连接,它以 JSON 格式占用时间并显示时间。

分类导航