您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > iPhone开发教程之将Google的云计算功能连接到Apple的iPhone中
中国移动互联网研发培训专家#importUIKit/UIKit.h#importFoundation/Foundation.h#importReachability.h#importGRplistControllerDelegateProtocol.htypedefenum{kGRplistDownloadCannotInitiate=0,kGRplistConnectionFailure,kGRplistFileFormatFailure}GRErrorCode;中国移动互联网研发培训专家#defineGR_ERROR_DOMAIN@GRplistController_Error_Domain@classGRplistModel;//Thisclasswillgrabaremoteplistfromtheserverwheneverupdate//iscalled.//AlsoregisterswiththeReachabilityobjectfornotificationswhen//theremotehostchangesavailabilityandupdatesaccordingly//askingthedelegatefirstifdownloadingnewdataisdesirable.中国移动互联网研发培训专家@interfaceGRplistController:NSObject{NSURL*remoteURL;NSObjectGRplistControllerDelegate*delegate;NetworkStatusremoteHostStatus;NetworkStatusinternetConnectionStatus;NetworkStatuslocalWiFiConnectionStatus;BOOLloadingData;中国移动互联网研发培训专家@privateGRplistModel*_model;BOOLhostIsReachable;NSMutableDictionary*plistIndex;NSMutableData*receivedData;NSURLConnection*connection;}@property(nonatomic,retain)NSURL*remoteURL;@property(nonatomic,assign)NSObject*delegate;@property(nonatomic,readonly)GRplistModel*model;中国移动互联网研发培训专家//dateoflastdownload@property(nonatomic,retain)NSDate*lastUpdate;@property(nonatomic,getter=isLoadingData)BOOLloadingData;@propertyNetworkStatusremoteHostStatus;@propertyNetworkStatusinternetConnectionStatus;@propertyNetworkStatuslocalWiFiConnectionStatus;//designatedInitializer-(id)initWithRemoteURL:(NSURL*)aRemoteURL;中国移动互联网研发培训专家-(void)updateDataFromDisk;-(void)download;-(void)cancelDownload;@end为了使用此类,我们提供了一个NSURL,XMLplist就位于其中,我们告知它从磁盘下载或拉取数据。GRplistController在此用户的数据存储目录生成一个plist字典文件,以便存储组成远程URL的缓存后的plist的位置和最近一次下载日期。plist被下载一次后,后续的数据加载就可直接从磁盘进行。中国移动互联网研发培训专家另外,还可以针对给定URL的最后一次下载的日期和时间使用lastUpdate属性来查询GRplistController对象,并决定何时从Web刷新数据。GRplistController总是使用NSURLConnection异步下载数据,这样用户界面不会在等待新数据时冻结起来。如果新的数据不可用或不可访问,可以继续使用所缓存的数据,直至新数据可用且可完全下载。您还可以将一个对象设为GRplistController的一个代理(delegate),借此就可对数据下载进行细粒度控制,就能在更新数据到来时提供通知,在远端数据被证实不可访问时提供详细的错误报告。GRplistController的这些代理方法在GRplistControllerDelegateProtocol.h内定义,如清单5所示。中国移动互联网研发培训专家@classGRplistController;@protocolGRplistControllerDelegate@optional//thelistcontrollerwillautomaticallytrytoupdatedatawhenthenetworkstatus//changes,soit'saskingpermission.中国移动互联网研发培训专家-(BOOL)listControllerShouldDownloadRemoteData:(GRplistController*)listController;-(void)listController:(GRplistController*)listControllerdownloadDidFailWithError:(NSError*)err;//ifthedatafromtheserverhaschanged...-(void)listControllerDataWillChange:(GRplistController*)listController;-(void)listControllerDataDidChange:(GRplistController*)listController;@end中国移动互联网研发培训专家我们的iPhone演示应用程序Sonnet的源代码包含在本文后面的下载部分,此应用程序能从我们在AppEngine服务器上的项目拉取所有莎士比亚十四行诗。这就让我们能不时地上传修正(比如拼写错误、不准确之处等),而在此之前,这常常需要进行重新编译和应用程序更新(如果数据由应用程序附带的话)。我们既希望能不时地更新通用的应用程序数据,又希望能够避免进行应用程序重编译,因为我们的UI并未更改。这就让应用程序数据更新能够与特性添加和bug修复区分开来。而且,通过在连接到Internet的时候启动应用程序,用户总是可以拥有最新的数据,而不像原来那样,必须等待我们应用程序的更新出现在iPhoneApplicationStore,而这要花些时间。中国移动互联网研发培训专家Sonnet的用户界面非常简单。应用程序首先用UITableView加载RootViewController,而后者会立即显示来自所有可用缓存数据的每个十四行诗的前三行,并会在之后显示任何被更新的数据。中国移动互联网研发培训专家图1.表视图iSonnet中国移动互联网研发培训专家如果用户触及了此表视图内的一个单元格,RootViewController就会将GRSonnetViewController推到屏幕上来。GRSonnetViewController之后会显示相应的完整的十四行诗。中国移动互联网研发培训专家图2.Sonnet视图中国移动互联网研发培训专家第一次运行时,应用程序没有数据显示给用户,所以在试图从AppEngine服务器拉取数据却没有获得数据时最好是显示错误消息。否则,用户将会看到一个空白的表视图。(应用程序的正式版本将会在应用程序包中包括数据,但演示应用程序却没有。)中国移动互联网研发培训专家图3.显示错误中国移动互联网研发培训专家这是惟一一次应用程序向用户显示网络通信错误的时候,因为在所有其他情况下应用程序已经下载并缓存了数据。有关UI实现的细节,可以参考示例代码。在Sonnet,RootViewController对象充当了GRPlistController的一个代理。RootViewController的初始化代码如清单6所示。中国移动互联网研发培训专家(void)viewDidLoad{[superviewDidLoad];//AddthefollowinglineifyouwantthelisttobeeditableNSString*rootURL=[[selfclass]defaultURL];self.sonnetsController=[[[GRplistControlleralloc]initWithRemoteURL:[NSURLURLWithString:rootURL]]autorelease];self.sonnetsController.delegate=self;[self.sonnetsControllerupdateDataFromDisk];中国移动互联网研发培训专家self.sonnets=nil;[selfupdateSonnetsFromModel];//hitthewebfornewinformation[selfupdateSonnets];}中国移动互联网研发培训专家在Sonnet启动时,RootViewController先是试图从磁盘加载所缓存的plist数据。如果所缓存的数据可用,它就会立即被
本文标题:iPhone开发教程之将Google的云计算功能连接到Apple的iPhone中
链接地址:https://www.777doc.com/doc-2877642 .html