这是我的应用程序的布局。我在标签栏控制器中有一个 UICollectionView。我在服务器上有两个不同的 XML 文件。根据订阅购买,将解析两个 xml 中的一个,并将每个项目添加到一个数组中,然后 UICollectionView 会显示该数组。我使用 MKiCloudSync 跨 iCloud 存储 NSUserDefault 值作为确定订阅是否仍然有效的方法,因此用户可以在多个设备上访问该应用程序。
我在 AppDelegate 实现文件中调用[MKiCloudSync start];
了 didFinishLaunchingWithOptions。
在 UICollectionViewController 文件中,布局本质上是这样的:
viewDidLoad 为 MKiCloudSync 更改添加侦听器。如果进行了更改,它将运行代码以更新 UI。viewWillAppear 检查 NSUserDefaults 以查看它是否应该加载付费或免费 XML,并据此进行解析。如果用户没有订阅,右栏按钮项目允许他们购买它,这会调用代码这样做。代码成功后,它会运行解析付费 xml 的代码 refreshpaid。
这是理论上的设置。我从 Ad Hoc build 在我的设备上运行该应用程序,并在沙盒环境中购买了订阅。UI 已按原样更新,以显示付费 XML 中可用的其他内容。
然后我删除了应用程序,重新启动了设备,并重新安装了临时构建以确保 iCloud 同步正常工作。当应用程序启动时,它为每个项目显示 11 个单元格(假设以某种方式刷新的代码以某种方式被调用了 11 次)。如果我导航到另一个选项卡并返回,或者单击一个单元格并弹回,它会自行更正并仅在 XML 中显示每个项目 1 个单元格。
现在,这是我的 UICollectionView.m 文件中的完整代码:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updatetheui) name: NSUserDefaultsDidChangeNotification object: nil];
UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"shelves.png"]];
}
-(void)updatetheui {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *status = [defaults objectForKey:@"Pay?"];
NSString *expirationdate = [defaults objectForKey:@"expiration"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:expirationdate];
NSLog(@"Date: %@", [dateFormat stringFromDate:date]);
if ([status isEqualToString:@"PaidUp"]|([date timeIntervalSinceNow] > 0.0)) {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Status" style:UIBarButtonItemStyleBordered target:self action:@selector(statusofsubscription)];
self.navigationItem.leftBarButtonItem = nil;
NSLog(@"UPDATEUI PAY");
[_allEntries removeAllObjects];
[self.collectionView reloadData];
[self refreshpaid];
} else {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Subscribe" style:UIBarButtonItemStyleBordered target:self action:@selector(buyButtonTapped:)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Restore" style:UIBarButtonItemStyleBordered target:self action:@selector(timetorestore)];
[_allEntries removeAllObjects];
[self.collectionView reloadData];
[self refreshfree];
NSLog(@"UPDATEUI FREE");
}
}
- (void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *status = [defaults objectForKey:@"Pay?"];
NSString *expirationdate = [defaults objectForKey:@"expiration"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:expirationdate];
NSLog(@"Date: %@", [dateFormat stringFromDate:date]);
if ([status isEqualToString:@"PaidUp"]|([date timeIntervalSinceNow] > 0.0)) {
[self refreshpaid];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Status" style:UIBarButtonItemStyleBordered target:self action:@selector(statusofsubscription)];
self.navigationItem.leftBarButtonItem = nil;
NSLog(@"WILLAPPEARPAID");
}
else {
[self refreshfree];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Subscribe" style:UIBarButtonItemStyleBordered target:self action:@selector(buyButtonTapped:)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Restore" style:UIBarButtonItemStyleBordered target:self action:@selector(timetorestore)];
}
//[self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)timetorestore {
[MKiCloudSync start];
UIAlertView *syncing = [[UIAlertView alloc]initWithTitle:@"Syncing" message:@"Now attempting to restore your subscription from iCloud. If unsuccessful, your subscription may have expired, or you may need to try again in a stronger network." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles: nil];
[syncing show];
[syncing release];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
NSLog(@"1");
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [_allEntries count];
NSLog(@"2");
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[UIView animateWithDuration:5.0
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
NSLog(@"animation start");
[cell setBackgroundColor:[UIColor colorWithRed: 180.0/255.0 green: 238.0/255.0 blue:180.0/255.0 alpha: 1.0]];
}
completion:^(BOOL finished){
NSLog(@"animation end");
[cell setBackgroundColor:[UIColor whiteColor]];
}
];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
if (_webViewController2 == nil) {
self.webViewController2 = [[[WebViewController2 alloc] initWithNibName:@"WebViewController2" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController2.entry = entry;
[self.navigationController pushViewController:_webViewController2 animated:YES];
}
else {
if (_webViewController == nil) {
self.webViewController = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}
#endif
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
static NSString *cellIdentifier = @"cvCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UIImageView *titleLabel = (UIImageView *)[cell viewWithTag:100];
UILabel *titleLabel2 = (UILabel *)[cell viewWithTag:200];
NSString *thearticleImage = entry.articleImage;
[titleLabel setImageWithURL:[NSURL URLWithString:entry.articleImage] placeholderImage:[UIImage imageNamed:@"icon@2x.png"]];
// [titleLabel2 setText:entry.articleTitle];
return cell;
}
- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
NSArray *channels = [rootElement elementsForName:@"channel"];
for (GDataXMLElement *channel in channels) {
NSString *blogTitle = [channel valueForChild:@"title"];
NSArray *items = [channel elementsForName:@"item"];
for (GDataXMLElement *item in items) {
NSString *issueTitle = [item valueForChild:@"title"];
NSString *issueURL = [item valueForChild:@"link"];
NSString *articleDateString = [item valueForChild:@"pubDate"];
NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
NSString *issueCoverArt = [item valueForChild:@"guid"];
NSLog(@"Title%@", issueTitle);
NSLog(@"Link%@", issueURL);
NSLog(@"Date%@", articleDateString);
NSLog(@"CoverArt%@", issueCoverArt);
RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
articleTitle:issueTitle
articleUrl:issueURL
articleDate:articleDate
articleImage:issueCoverArt] autorelease];
[entries addObject:entry];
}
}
}
- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
if ([rootElement.name compare:@"rss"] == NSOrderedSame) {
[self parseRss:rootElement entries:entries];
} else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {
[self parseAtom:rootElement entries:entries];
} else {
NSLog(@"Unsupported root element: %@", rootElement.name);
}
}
- (void)requestFinished:(ASIHTTPRequest *)request {
[_queue addOperationWithBlock:^{
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
options:0 error:&error];
if (doc == nil) {
NSLog(@"Failed to parse %@", request.url);
} else {
NSMutableArray *entries = [NSMutableArray array];
[self parseFeed:doc.rootElement entries:entries];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
for (RSSEntry *entry in entries) {
int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
RSSEntry *entry1 = (RSSEntry *) a;
RSSEntry *entry2 = (RSSEntry *) b;
return [entry1.articleDate compare:entry2.articleDate];
}];
[_allEntries insertObject:entry atIndex:insertIdx];
[self.collectionView reloadData];
}
}];
}
}];
}