{"id":190924,"date":"2023-09-13T09:25:25","date_gmt":"2023-09-13T01:25:25","guid":{"rendered":"https:\/\/www.idc.net\/help\/190924\/"},"modified":"2023-09-13T09:25:25","modified_gmt":"2023-09-13T01:25:25","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91ios%e6%95%99%e7%a8%8b%ef%bc%9aios%e5%ba%94%e7%94%a8%e5%86%85%e8%b4%ad%e4%b9%b0","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/190924\/","title":{"rendered":"\u540e\u6d6a\u4e91IOS\u6559\u7a0b\uff1aiOS\u5e94\u7528\u5185\u8d2d\u4e70"},"content":{"rendered":"<h2>IOS\u5e94\u7528\u5185\u8d2d\u4e70<\/h2>\n<hr>\n<h2>\u7b80\u4ecb<\/h2>\n<p>\u5e94\u7528\u7a0b\u5e8f\u5185\u8d2d\u4e70\u662f\u5e94\u7528\u7a0b\u5e8f\u7528\u4e8e\u8d2d\u4e70\u989d\u5916\u5185\u5bb9\u6216\u5347\u7ea7\u529f\u80fd\u3002<\/p>\n<h3>\u5b9e\u4f8b\u6b65\u9aa4<\/h3>\n<p>1.\u5728 iTunes \u8fde\u63a5\u4e2d\u8bf7\u786e\u4fdd\u62e5\u6709\u4e00\u4e2a\u552f\u4e00\u7684 App ID\uff08unique App ID \uff09\uff0c\u5f53\u521b\u5efa\u6346\u7ed1\u7684ID\uff08 bundle ID\uff09\u5e94\u7528\u7a0b\u5e8f\u66f4\u65b0\u65f6\uff0c\u4ee3\u7801\u4f1a\u4ee5\u76f8\u5e94\u7684\u914d\u7f6e\u6587\u4ef6\u7b7e\u540d\u5728Xcode\u4e0a <\/p>\n<p>2.\u521b\u5efa\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u548c\u66f4\u65b0\u5e94\u7528\u7a0b\u5e8f\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u77e5\u9053\u66f4\u591a\u6709\u5173\u7684\uff0c\u5728\u82f9\u679c\u7684 \u6dfb\u52a0\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f \u6587\u6863\u4e2d<\/p>\n<p>3.\u5728\u5e94\u7528\u7a0b\u5e8f\u9875\u7684\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f( Manage In-App Purchase)\u4e2d\uff0c\u4e3aapp\u5185\u4ed8\u8d39\u6dfb\u52a0\u65b0\u4ea7\u54c1<\/p>\n<p>4.\u786e\u4fdd\u8bbe\u7f6e\u7684\u5e94\u7528\u7a0b\u5e8f\u4e3a\u7684\u94f6\u884c\u8be6\u7ec6\u3002\u9700\u8981\u5c06\u5176\u8bbe\u7f6e\u4e3a\u5728\u5e94\u7528\u7a0b\u5e8f\u5185\u8d2d\u4e70\uff08In-App purchase\uff09\u3002\u6b64\u5916\u5728 iTunes \u4e2d\u4f7f\u7528\u7ba1\u7406\u7528\u6237\uff08Manage Users\uff09\u9009\u9879\uff0c\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u7528\u6237\u5e10\u6237\u8fde\u63a5\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u9875\u3002<\/p>\n<p>5.\u4e0b\u4e00\u6b65\u662f\u4e0e\u5904\u7406\u4ee3\u7801\u548c\u4e3a\u6211\u4eec\u5728\u5e94\u7528\u7a0b\u5e8f\u5185\u8d2d\u4e70\u521b\u5efa\u6709\u5173\u7684 UI\u3002<\/p>\n<p>6.\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u7684\u89c6\u56fe\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5728 iTunes \u4e2d\u6307\u5b9a\u7684\u6807\u8bc6\u7b26\u8fde\u63a5\u8f93\u5165\u6346\u7ed1\u6807\u8bc6\u7b26<\/p>\n<p>7.\u66f4\u65b0ViewController.xib \uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/atts.IDC.NET.cn\/attachments\/day_171121\/201711211710075025.jpg\" class=\"aligncenter\"><\/p>\n<p>8.\u4e3a\u4e09\u4e2a\u6807\u7b7e\u521b\u5efaIBOutlets\uff0c\u4e14\u5c06\u6309\u94ae\u5206\u522b\u547d\u540d\u4e3a productTitleLabel\u3001 productDescriptionLabel\u3001 productPriceLabel \u548c purchaseButton<\/p>\n<p>9.\u9009\u62e9\u9879\u76ee\u6587\u4ef6\uff0c\u7136\u540e\u9009\u62e9\u76ee\u6807\uff0c\u7136\u540e\u6dfb\u52a0StoreKit.framework<\/p>\n<p>10.\u66f4\u65b0ViewController.h \uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>#import &lt;UIKit\/UIKit.h&gt;\n#import &lt;StoreKit\/StoreKit.h&gt;\n\n@interface ViewController : UIViewController&lt; SKProductsRequestDelegate,SKPaymentTransactionObserver&gt;\n{\n    SKProductsRequest *productsRequest;\n    NSArray *validProducts;\n    UIActivityIndicatorView *activityIndicatorView;\n    IBOutlet UILabel *productTitleLabel;\n    IBOutlet UILabel *productDescriptionLabel;\n    IBOutlet UILabel *productPriceLabel;\n    IBOutlet UIButton *purchaseButton;\n}\n- (void)fetchAvailableProducts;\n- (BOOL)canMakePurchases;\n- (void)purchaseMyProduct:(SKProduct*)product;\n- (IBAction)purchase:(id)sender;\n\n@end\n<\/pre>\n<p>11.\u66f4\u65b0ViewController.m \uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>#import \"ViewController.h\"\n#define kTutorialPointProductID \n@\"com.tutorialPoints.testApp.testProduct\"\n\n@interface ViewController ()\n\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    \/\/ Adding activity indicator\n    activityIndicatorView = [[UIActivityIndicatorView alloc]\n    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];\n    activityIndicatorView.center = self.view.center;\n    [activityIndicatorView hidesWhenStopped];\n    [self.view addSubview:activityIndicatorView];\n    [activityIndicatorView startAnimating];\n    \/\/Hide purchase button initially\n    purchaseButton.hidden = YES;\n    [self fetchAvailableProducts];    \n}\n\n- (void)didReceiveMemoryWarning\n{\n    [super didReceiveMemoryWarning];\n    \/\/ Dispose of any resources that can be recreated.\n}\n\n-(void)fetchAvailableProducts{\n    NSSet *productIdentifiers = [NSSet \n    setWithObjects:kTutorialPointProductID,nil];\n    productsRequest = [[SKProductsRequest alloc] \n    initWithProductIdentifiers:productIdentifiers];\n    productsRequest.delegate = self;\n    [productsRequest start];\n}\n\n- (BOOL)canMakePurchases\n{\n    return [SKPaymentQueue canMakePayments];\n}\n- (void)purchaseMyProduct:(SKProduct*)product{\n    if ([self canMakePurchases]) {\n        SKPayment *payment = [SKPayment paymentWithProduct:product];\n        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];\n        [[SKPaymentQueue defaultQueue] addPayment:payment];\n    }\n    else{\n        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:\n        @\"Purchases are disabled in your device\" message:nil delegate:\n        self cancelButtonTitle:@\"Ok\" otherButtonTitles: nil];\n        [alertView show];\n    }\n}\n-(IBAction)purchase:(id)sender{\n    [self purchaseMyProduct:[validProducts objectAtIndex:0]];\n    purchaseButton.enabled = NO; \n}\n\n#pragma mark StoreKit Delegate\n\n-(void)paymentQueue:(SKPaymentQueue *)queue \n updatedTransactions:(NSArray *)transactions {\n    for (SKPaymentTransaction *transaction in transactions) {\n        switch (transaction.transactionState) {\n            case SKPaymentTransactionStatePurchasing:\n                    NSLog(@\"Purchasing\");\n                break;                \n            case SKPaymentTransactionStatePurchased:\n               if ([transaction.payment.productIdentifier \n                  isEqualToString:kTutorialPointProductID]) {\n                   NSLog(@\"Purchased \");\n                   UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:\n                   @\"Purchase is completed succesfully\" message:nil delegate:\n                   self cancelButtonTitle:@\"Ok\" otherButtonTitles: nil];\n                   [alertView show];\n                }               \n                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];\n                break;                \n            case SKPaymentTransactionStateRestored:               \n                NSLog(@\"Restored \");               \n                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];\n                break;                \n            case SKPaymentTransactionStateFailed:\n                NSLog(@\"Purchase failed \");\n                break;\n            default:\n                break;\n        }\n    }\n}\n\n-(void)productsRequest:(SKProductsRequest *)request \n didReceiveResponse:(SKProductsResponse *)response\n{\n    SKProduct *validProduct = nil;\n    int count = [response.products count];\n    if (count&gt;0) {\n        validProducts = response.products;\n        validProduct = [response.products objectAtIndex:0];\n        if ([validProduct.productIdentifier \n           isEqualToString:kTutorialPointProductID]) {\n            [productTitleLabel setText:[NSString stringWithFormat:\n            @\"Product Title: %@\",validProduct.localizedTitle]];\n            [productDescriptionLabel setText:[NSString stringWithFormat:\n            @\"Product Desc: %@\",validProduct.localizedDescription]];\n            [productPriceLabel setText:[NSString stringWithFormat:\n            @\"Product Price: %@\",validProduct.price]];           \n        }        \n    } else {\n        UIAlertView *tmp = [[UIAlertView alloc]\n                            initWithTitle:@\"Not Available\"\n                            message:@\"No products to purchase\"\n                            delegate:self\n                            cancelButtonTitle:nil\n                            otherButtonTitles:@\"Ok\", nil];\n        [tmp show];\n    }    \n    [activityIndicatorView stopAnimating];\n    purchaseButton.hidden = NO;\n}\n\n@end\n<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> \u9700\u8981\u4fee\u6539\u4f60\u521b\u5efaIn-App Pur\uff08\u5e94\u7528\u5185\u8d2d\u4e70\uff09\u7684 kTutorialPointProductID \u3002\u901a\u8fc7\u4fee\u6539fetchAvailableProducts\u4ea7\u54c1\u6807\u8bc6\u7b26\u7684 NSSet, \u4f60\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u4ea7\u54c1\u3002<\/p>\n<h3>\u8f93\u51fa<\/h3>\n<p>\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f,\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b <\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/atts.IDC.NET.cn\/attachments\/day_171121\/201711211710517588.jpg\" class=\"aligncenter\"><\/p>\n<p>\u786e\u4fdd\u5df2\u7ecf\u4e2d\u767b\u5f55\u3002\u5355\u51fb\u8d2d\u4e70\u9009\u62e9\u73b0\u6709\u7684Apple ID\u3002\u8f93\u5165\u6709\u6548\u7684\u6d4b\u8bd5\u5e10\u6237\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u51e0\u79d2\u949f\u540e\uff0c\u663e\u793a\u4e0b\u9762\u7684\u4fe1\u606f<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/atts.IDC.NET.cn\/attachments\/day_171121\/201711211710574508.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4e00\u65e6\u4ea7\u54c1\u6210\u529f\u8d2d\u4e70\uff0c\u5c06\u83b7\u5f97\u4ee5\u4e0b\u4fe1\u606f\u3002\u53ef\u4ee5\u5728\u663e\u793a\u6b64\u4fe1\u606f\u7684\u5730\u65b9\uff0c\u66f4\u65b0\u5e94\u7528\u529f\u80fd\u76f8\u5173\u7684\u4ee3\u7801<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/atts.IDC.NET.cn\/attachments\/day_171121\/201711211711083482.jpg\" class=\"aligncenter\"><\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IOS\u5e94\u7528\u5185\u8d2d\u4e70 \u7b80\u4ecb \u5e94\u7528\u7a0b\u5e8f\u5185\u8d2d\u4e70\u662f\u5e94\u7528\u7a0b\u5e8f\u7528\u4e8e\u8d2d\u4e70\u989d\u5916\u5185\u5bb9\u6216\u5347\u7ea7\u529f\u80fd\u3002 \u5b9e\u4f8b\u6b65\u9aa4 1.\u5728 iTunes  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":190925,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189686],"tags":[],"class_list":["post-190924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=190924"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190924\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/190925"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=190924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=190924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=190924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}