Webページ内のリンクをランダムで開いていくiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *links;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.links = @[@”http://en.wikinews.org/wiki/Main_Page”];
}
– (void)viewDidAppear:(BOOL)animated
{
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
UICollectionView *cv = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
[cv setDataSource:self];
[cv setDelegate:self];
[cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@”CellIdentifier”];
[self.view addSubview:cv];
self.collectionView = cv;
}
– (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.links.count;
}
– (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@”CellIdentifier” forIndexPath:indexPath];
UIWebView *scrap = [[UIWebView alloc] initWithFrame:cell.bounds];
scrap.scalesPageToFit = YES;
[self collectionView:collectionView loadScrap:scrap cellForItemAtIndexPath:indexPath];
[cell addSubview:scrap];
scrap.userInteractionEnabled = NO;
return cell;
}
– (void)collectionView:(UICollectionView *)collectionView loadScrap:(UIWebView *)web cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:self.links[indexPath.item]]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[web loadData:data MIMEType:response.MIMEType textEncodingName:response.textEncodingName baseURL:response.URL];
}] resume];
}
– (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(CGRectGetMaxX(self.view.bounds)/2.05, CGRectGetMaxY(self.view.bounds)/1.5);
}
– (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
UIWebView *webView = [cell.subviews lastObject];
NSString *htmlSource = [webView stringByEvaluatingJavaScriptFromString:@”document.documentElement.outerHTML”];
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [detector matchesInString:htmlSource options:0 range:NSMakeRange(0, [htmlSource length])];
NSMutableArray *m = [NSMutableArray array];
for (NSTextCheckingResult *r in matches) {
NSString *string = [r.URL absoluteString];
if ([string hasSuffix:@”.png”] || [string hasSuffix:@”.jpg”] || [string hasSuffix:@”.js”] || [string hasSuffix:@”.json”] || [string hasSuffix:@”.rss”] || [string hasSuffix:@”.css”] || string.length > 100) {
continue;
}
[m addObject:string];
}
// shuffle
NSUInteger count = [m count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
NSInteger nElements = count – i;
NSInteger n = arc4random() % nElements + i;
[m exchangeObjectAtIndex:i withObjectAtIndex:n];
}
self.links = [m subarrayWithRange:NSMakeRange(0, MIN(m.count, 10))];
[collectionView reloadData];
}
@end