[iOS]wallet开发demo

来源:互联网 发布:手机数据恢复精灵免费 编辑:程序博客网 时间:2024/05/18 00:53
关于wallet开发,我觉得前端只需要添加.pkpass文件即可,服务器端负责制作这类文件。下面参考本地加载.pkpass文件来动手做一个demo:1、新建PassTest项目,把.pkpass文件目录拖到项目中。![添加pass文件](http://img.blog.csdn.net/20160708164633756)2、在.h文件添加头文件:#import <PassKit/PassKit.h> 并添加PKAddPassesViewControllerDelegate代理方法。3、检查api是否可用采用这种方法:
if (![PKPassLibrary isPassLibraryAvailable]) {        [[[UIAlertView alloc] initWithTitle:@"Error"                                    message:@"PassKit not available"                                   delegate:nil                          cancelButtonTitle:@"Pitty"                          otherButtonTitles: nil] show];        return;    }

4、获取一个.pkpass文件:

 NSString *passFile = [[[NSBundle mainBundle] resourcePath]                          stringByAppendingPathComponent:name];    NSData *passData = [NSData dataWithContentsOfFile:passFile];    NSError* error = nil;    PKPass *newPass = [[PKPass alloc] initWithData:passData                                             error:&error];    if (error != nil) {        [[[UIAlertView alloc] initWithTitle:@"Passes error"                                    message:[error                                             localizedDescription]                                   delegate:nil                          cancelButtonTitle:@"Ooops"                          otherButtonTitles: nil] show];        return;    }

5、构造一个PKAddPassesViewController对象,来准备添加.pkpass

PKAddPassesViewController *addController =    [[PKAddPassesViewController alloc] initWithPass:newPass];    addController.delegate = self;    [self presentViewController:addController                       animated:YES                     completion:nil];

6、在代理方法中打开wallet,查看添加的票据

-(void)addPassesViewControllerDidFinish:(PKAddPassesViewController *)controller {    [self dismissViewControllerAnimated:YES completion:nil];    if (selectPass) {        // 调转到wallet查看添加的票据        NSLog(@"%@",selectPass.passURL);        [[UIApplication sharedApplication] openURL:selectPass.passURL];    }}

整个代码很简单,主要是.pkpass 文件制作稍微麻烦。
参考网站:
http://pass.infothinker.com/#
http://www.passquan.cn
http://www.lxway.com/806148922.htm
http://blog.sina.com.cn/s/blog_62c942d20101d2y9.html

0 0
原创粉丝点击