Objective-C URL加载系统
2016-08-05 10:17
323 查看
该文转载自http://www.yiibai.com/objective_c/objective_c_url_loading_system.html,留下已备后续学习查看。
要访问互联网的URL,即项目是有用的URL加载。它提供的下面的类的帮助:
NSMutableURLRequest
NSURLConnection
NSURLCache
NSURLAuthenticationChallenge
NSURLCredential
NSURLProtectionSpace
NSURLResponse
NSURLDownload
NSURLSession
下面是一个简单的URL加载的例子。这是不能在命令行中运行。我们需要创建Cocoa应用程序。
appdelegate.h 文件内容如下:#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@end更新以下AppDelegate.m文件:
要访问互联网的URL,即项目是有用的URL加载。它提供的下面的类的帮助:
NSMutableURLRequest
NSURLConnection
NSURLCache
NSURLAuthenticationChallenge
NSURLCredential
NSURLProtectionSpace
NSURLResponse
NSURLDownload
NSURLSession
下面是一个简单的URL加载的例子。这是不能在命令行中运行。我们需要创建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" }
相关文章推荐
- [LINUX][HOWTO]配置系统启动时加载的服务
- 如何不重启系统加载.SYS文件
- 【windows】将程序启动加载到系统服务
- 解决 System.IO.FileNotFoundException: 未能加载文件或程序集“Team.DLinqDAL”或它的某一个依赖项。系统找不到指定的文件。
- 文件系统管理 之 在Fedora core 4.0 加载NTFS和FAT32分区详述
- 如何使Object Arx.Net 程序随ACAD系统启动加载?
- suse9 自动加载文件系统
- 我晕了,未能加载文件或程序集,系统找不到指定的文件。
- DALFactory出现"未能加载文件或程序集“Flashlm.SQLServerDAL”或它的某一个依赖项。系统找不到指定的文件”的解决方案
- 将oracle加载为linux标准系统服务脚本
- GIB系统中的剥离和加载,
- 系统启动时加载模块出错的解决方案
- VB加载程序进入系统服务
- 实时情况下将事务系统中的增量数据加载到数据仓库的技术和优缺点
- 在Windows启动菜单中加载Linux系统选项的方法(以Lilo为例)
- 解决系统无法加载声音
- 列举系统中所有被加载的DLL
- VMWare 重装后加载了系统导致XWindow启动不了
- 使用JavaService让java程序加载为winNT系统服务
- Windows2000启动时加载的系统支持进程