2014-10-27 16 views

回答

2

此代碼假定您知道如何創建擺在首位的通...

NSError * passerror; 

PKPass * pass = [[PKPass alloc] initWithData:data error:&passerror]; 

if(!pass) { 
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Pass Failed" message:@"Sorry there was a problem creating your Passbook." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 
    return; 
} 

//init a pass library 
PKPassLibrary* passLib = [[PKPassLibrary alloc] init]; 

//check if pass library contains this pass already 
if([passLib containsPass:pass]) { 

    //pass already exists in library, show an error message 
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Pass Exists" message:@"Pass is already in Passbook." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 

} else { 

    //present view controller to add the pass to the library 
    PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPass:pass]; 
    [vc setDelegate:(id)self]; 
    [self presentViewController:vc animated:YES completion:nil]; 
} 

編輯你需要導入

#import <PassKit/PassKit.h> 
+0

我需要進口什麼? – 2014-10-27 13:07:40

+0

更新了導入信息的答案 – Flexicoder 2014-10-27 13:21:54