IOS

1. 什么是Passbook

Passbook是苹果公司于北京时间2012年6月12日上午,在全球开发者大会(WWDC)上宣布了iOS 6系统将提供操作一个全新的应用——Passbook
这是一款可以存放登机牌、会员卡和电影票的工具。该功能将整合来自各类服务的票据,包括电影票、登机牌、积分卡和礼品卡等
Passbook是基于地理位置的,通过定位功能,当用户走到相关商店或场所附近时,对应的票据将会被自动显示
Passbook只能在iPhone和iPod touch设备中使用
 
2.实例代码
#import "GViewController.h"
// 添加框架
#import <PassKit/PassKit.h>

@interface GViewController ()

// 存放加载的pkpass文件
@property (strong,nonatomic) NSArray *dataList;

@end

@implementation GViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 获得加载到得数据
    _dataList = [self loadPasses];
    
    // IOS6 和 IOS7 适配
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0 ) {
        [self.tableView setContentInset:UIEdgeInsetsMake(20, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.contentInset.right)];
        
    }
}

/**
 *  加载数据
 *
 *  @return 返回文件后缀名为pkpass 的文件
 */
- (NSArray *)loadPasses
{
    // 1.获得mainBundle 的路径
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    
    // 2.取出mainBundle中的文件,存放在数组中
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resourcePath error:nil];
    
    // 3.遍历数组,取出pkpass 文件
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSString *fileName in array) {
        // 获取文件后缀名为.pkpass 的文件
        if ([fileName hasSuffix:@".pkpass"]) {
            [arrayM addObject:fileName];
        }
    }
    
    // 4.返回获得的数据
    return arrayM;
}

-(void)openWithName:(NSString *)name
{
    // 1.根据文件名获取文件完整路径
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
    
    // 2.将文件加载到NSData中
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    // 3.实例化pkpass (根据NSData 数据)
    PKPass *pass = [[PKPass alloc] initWithData:data error:nil];
    
    // 4.将pass实例添加到pass视图中
    PKAddPassesViewController *controller = [[PKAddPassesViewController alloc] initWithPass:pass];
    
    // 5.显示pass视图控制器
    [self presentViewController:controller animated:YES completion:nil];
}
#pragma mark - tableView 的数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.设置cell的唯一标示符
    static NSString *ID = @"cell";
    
    // 2.cell重用
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    // 3.设置cell的内容
    cell.textLabel.text = _dataList[indexPath.row];
    
    return cell;
}

#pragma mark - tableView 的代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self openWithName:_dataList[indexPath.row]];
}
@end
原文地址:https://www.cnblogs.com/mcj-coding/p/3574564.html