# 从 iOS 应用内消息进行购买

为了让您的用户可以直接从 iOS 应用中显示的应用内消息进行购买，您可以为 Rich Media 实现开箱即用的解决方案，并添加回调以获取有关应用内购买的更多信息。

<Aside>
适用于 **6.4.5 iOS SDK 版本及更高版本**。
</Aside>

## Rich Media JavaScript 实现

要在您的 Rich Media 中实现应用内购买，请使用简单的 **JavaScript 函数调用**：

```javascript
pushwooshImpl.makePurchaseWithIdentifier("Premium"); // product identifier specified in your App Store product information
```

## AppDelegate 回调方法

如果您需要在购买时执行回调，请按如下方式实现 **PWPurchaseDelegate 协议**：

1. 使您项目的 AppDelegate 符合 PWPurchaseDelegate 协议：

```objective-c
@interface AppDelegate : PWAppDelegate <PushNotificationDelegate, PWPurchaseDelegate>
```

2. 在您的 AppDelegate 中实现 PWPurchaseDelegate 的方法：

```objective-c
#pragma mark - Purchase delegate methods
- (void) onPWInAppPurchaseHelperPaymentComplete:(NSString*) identifier {
    NSLog(@“Custom purchase delegate -- payment complete for identifier: %@“, identifier);
}
-(void)onPWInAppPurchaseHelperProducts:(NSArray<SKProduct *>* _Nullable)products {
    NSLog(@“Custom purchase delegate -- retrieved list of products”);
}
-(void)onPWInAppPurchaseHelperPaymentFailedProductIdentifier:(NSString* _Nullable)identifier error:(NSError* _Nullable)error {
    NSLog(@“Custom purchase delegate -- payment failed with identifier %@ and error %@“, identifier, error.description);
}
```

### 可用的 PWPurchaseDelegate 方法列表

#### 产品列表

```objective-c
@protocol PWPurchaseDelegate <NSObject>
// Detailed information about list of products in your app
-(void)onPWInAppPurchaseHelperProducts:(NSArray<SKProduct *>* _Nullable)products;
@end
```

#### 成功交易

```objective-c
@protocol PWPurchaseDelegate &#x3C;NSObject>
// A successfully processed transaction
<strong>-(void)onPWInAppPurchaseHelperPaymentComplete:(NSString* _Nullable)identifier;
</strong>
@end
```

#### 失败交易

```objective-c
@protocol PWPurchaseDelegate &#x3C;NSObject>
<strong>// A failed transaction
</strong>-(void)onPWInAppPurchaseHelperPaymentFailedProductIdentifier:(NSString* _Nullable)identifier error:(NSError* _Nullable)error;
@end
```

#### 从 App Store 推广

```objective-c
@protocol PWPurchaseDelegate <NSObject>
// An in-app purchase is initiated from the App Store, and the transaction continues in your app.
-(void)onPWInAppPurchaseHelperCallPromotedPurchase:(NSString* _Nullable)identifier;
@end
```

#### 恢复已完成交易失败

```objective-c
@protocol PWPurchaseDelegate <NSObject>
// An error occurred while restoring transactions
-(void)onPWInAppPurchaseHelperRestoreCompletedTransactionsFailed:(NSError * _Nullable)error;
@end
```

<Aside type="note">
如果本指南中指定的方法未能满足您的需求，请联系我们的 [客户支持团队](https://help.pushwoosh.com/hc/en-us/requests/new?utm\_content=pwwebsite\&utm\_term=footer\&utm\_campaign=support)，我们将很乐意为您提供进一步的指导。
</Aside>