- Objective-C基础
- Objective-C - 首页
- Objective-C - 概述
- Objective-C - 环境搭建
- Objective-C - 程序结构
- Objective-C - 基本语法
- Objective-C - 数据类型
- Objective-C - 变量
- Objective-C - 常量
- Objective-C - 运算符
- Objective-C - 循环
- Objective-C - 条件语句
- Objective-C - 函数
- Objective-C - 块 (Blocks)
- Objective-C - 数字
- Objective-C - 数组
- Objective-C - 指针
- Objective-C - 字符串
- Objective-C - 结构体
- Objective-C - 预处理器
- Objective-C - Typedef
- Objective-C - 类型转换
- Objective-C - 日志处理
- Objective-C - 错误处理
- 命令行参数
- 高级Objective-C
- Objective-C - 类与对象
- Objective-C - 继承
- Objective-C - 多态
- Objective-C - 数据封装
- Objective-C - 分类 (Categories)
- Objective-C - 伪装 (Posing)
- Objective-C - 扩展 (Extensions)
- Objective-C - 协议 (Protocols)
- Objective-C - 动态绑定
- Objective-C - 组合对象
- Obj-C - Foundation框架
- Objective-C - 快速枚举
- Obj-C - 内存管理
- Objective-C有用资源
- Objective-C - 快速指南
- Objective-C - 有用资源
- Objective-C - 讨论
Objective-C中的URL加载系统
URL加载用于访问URL,即互联网上的资源。它借助以下类实现:
- NSMutableURLRequest
- NSURLConnection
- NSURLCache
- NSURLAuthenticationChallenge
- NSURLCredential
- NSURLProtectionSpace
- NSURLResponse
- NSURLDownload
- NSURLSession
这是一个简单的URL加载示例。它无法在命令行运行,需要创建一个Cocoa应用程序。
这可以通过在XCode中选择“新建”>“项目”,然后在出现的窗口的OS X应用程序部分选择“Cocoa应用程序”来完成。
点击下一步完成后续步骤,系统会要求您提供项目名称,您可以随意命名。
appdelegate.h文件内容如下:
#import <Cocoa/Cocoa.h> @interface AppDelegate : NSObject <NSApplicationDelegate> @property (assign) IBOutlet NSWindow *window; @end
将AppDelegate.m文件更新为:
#import "AppDelegate.h"
@interface SampleClass:NSObject<NSURLConnectionDelegate> {
NSMutableData *_responseData;
}
- (void)initiateURLConnection;
@end
@implementation SampleClass
- (void)initiateURLConnection {
// Create the request.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://date.jsontest.com"]];
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
}
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSLog(@"%@",[[NSString alloc]initWithData:_responseData encoding:NSUTF8StringEncoding]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass initiateURLConnection];
// Insert code here to initialize your application
}
@end
现在,当我们编译并运行程序时,我们将得到以下结果。
2013-09-29 16:50:31.953 NSURLConnectionSample[1444:303] {
"time": "11:20:31 AM",
"milliseconds_since_epoch": 1380453631948,
"date": "09-29-2013"
}
在上面的程序中,我们创建了一个简单的URL连接,它以JSON格式获取时间并显示时间。
objective_c_foundation_framework.htm
广告