跳到内容

iOS 模态富媒体

从 Pushwoosh SDK 6.7.5 版本开始,您可以发送模态富媒体 (Modal Rich Media)

我们引入了可自定义的全新模态富媒体。新的模态富媒体不会完全遮挡屏幕,并且可以放置在屏幕的不同位置(顶部、底部和中央)。

有关富媒体页面的更多信息,请参阅我们的指南

//for silent push notifications
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Pushwoosh.sharedInstance().handlePushReceived(userInfo)
completionHandler(.noData)
}
  1. 要启用模态富媒体的显示,请在您的 info.plist 文件中设置参数 Pushwoosh_RICH_MEDIA_STYLE,并为其分配值 MODAL_RICH_MEDIA

Info.plist:

<key>Pushwoosh_RICH_MEDIA_STYLE</key>
<string>MODAL_RICH_MEDIA</string>
  1. 默认情况下,模态富媒体将显示在屏幕中央,并带有从下到上的出现动画。
  1. 要配置模态富媒体的显示(屏幕位置、显示动画、关闭动画),您需要使用以下方法:
// Modal Rich Media Configuration
PWModalWindowConfiguration.shared().configureModalWindow(with: .PWModalWindowPositionCenter,
present: .PWAnimationPresentFromBottom,
dismiss: .PWAnimationDismissUp)

模态富媒体定位

Anchor link to

模态富媒体可以放置在三个位置:顶部、底部或中央。

/**
Enum defining the possible positions for displaying a modal window.
- `PWModalWindowPositionTop`: The modal window appears at the top of the screen, within the safe area.
- `PWModalWindowPositionCenter`: The modal window appears at the center of the screen, within the safe area.
- `PWModalWindowPositionBottom`: The modal window appears at the bottom of the screen, within the safe area.
- `PWModalWindowPositionBottomSheet`: The modal window appears at the very bottom of the screen, ignoring the safe area.
- `PWModalWindowPositionDefault`: The default position is the center of the screen, within the safe area.
*/
typedef NS_ENUM(NSInteger, ModalWindowPosition) {
PWModalWindowPositionTop, // Appears at the top of the screen (within safe area)
PWModalWindowPositionCenter, // Appears at the center of the screen (within safe area)
PWModalWindowPositionBottom, // Appears at the bottom of the screen (within safe area)
PWModalWindowPositionBottomSheet, // Appears at the very bottom of the screen (ignores safe area)
PWModalWindowPositionFullScreen, // Fullscreen, ignores safe area insets
PWModalWindowPositionDefault // Default position (center of the screen, within safe area)
};

下面的示例展示了一个显示在屏幕顶部的模态富媒体。

模态富媒体的显示动画包括:

typedef NS_ENUM(NSInteger, PresentModalWindowAnimation) {
PWAnimationPresentFromBottom,
PWAnimationPresentFromTop,
PWAnimationPresentFromRight,
PWAnimationPresentFromLeft,
PWAnimationPresentNone
};

模态富媒体的关闭动画包括:

typedef NS_ENUM(NSInteger, DismissModalWindowAnimation) {
PWAnimationDismissDown,
PWAnimationDismissUp,
PWAnimationDismissLeft,
PWAnimationDismissRight,
PWAnimationCurveEaseInOut,
PWAnimationDismissNone,
/**
* Default dismiss animation is `PWAnimationCurveEaseInOut`
*/
PWAnimationDismissDefault
};

下面的示例演示了在屏幕底部显示模态富媒体,并带有从左到右的显示动画和向右的关闭动画:

模态富媒体的附加参数

Anchor link to

显示模态富媒体的附加参数包括添加振动类型的触觉反馈、启用滑动手势以及设置在指定持续时间后自动关闭的计时器等选项。

// Haptic Feedback Type
PWModalWindowConfiguration.shared().setPresent(.PWHapticFeedbackLight)
/**
enum HapticFeedbackType
typedef NS_ENUM(NSInteger, HapticFeedbackType) {
PWHapticFeedbackLight, // Light vibration feedback
PWHapticFeedbackMedium, // Medium vibration feedback
PWHapticFeedbackHard, // Strong vibration feedback
/**
* Vibration is off by default.
*/
PWHapticFeedbackNone
};
*/
// Swipe directions
let directions: [NSNumber] = [
NSNumber(value: DismissSwipeDirection.PWSwipeDismissDown.rawValue),
NSNumber(value: DismissSwipeDirection.PWSwipeDismissUp.rawValue)
]
PWModalWindowConfiguration.shared().setDismissSwipeDirections(directions)
/**
typedef NS_ENUM(NSInteger, DismissSwipeDirection) {
PWSwipeDismissDown,
PWSwipeDismissUp,
PWSwipeDismissLeft,
PWSwipeDismissRight,
PWSwipeDismissNone
};
*/
// Set Rich Media corner radius
PWModalWindowConfiguration.shared().setCornerType([.PWCornerTypeTopLeft, .PWCornerTypeBottomRight], withRadius: 30.0)
// Close Modal Rich Media after N seconds
PWModalWindowConfiguration.shared().closeModalWindow(after: 3)

PWRichMediaPresentingDelegate

Anchor link to

要管理模态富媒体的队列,您需要实现 PWRichMediaPresentingDelegate 的委托方法。 使用此功能时,模态富媒体会按顺序呈现,直到用户关闭当前的富媒体,下一个才会被显示。一旦用户关闭了已呈现的富媒体,属于不同推送通知的下一个富媒体将会被显示。

要实现此功能,请使用下面提供的代码:

import UIKit
import PushwooshFramework
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
PWRichMediaManager.shared().delegate = ChainedRichMediaPresentingDelegate.init(queue: [], inApp: false)
}
}
class ChainedRichMediaPresentingDelegate: NSObject, PWRichMediaPresentingDelegate {
var queue: [PWRichMedia]
var inAppIsPresenting: Bool
init(queue: [PWRichMedia], inApp: Bool) {
self.queue = queue
self.inAppIsPresenting = inApp
super.init() // can actually be omitted in this example because will happen automatically.
}
convenience override init() {
self.init(queue: [], inApp: false)
}
func richMediaManager(_ richMediaManager: PWRichMediaManager!, shouldPresent richMedia: PWRichMedia!) -> Bool {
if !queue.contains(where: { $0 === richMedia }) {
queue.append(richMedia)
}
return !inAppIsPresenting
}
func richMediaManager(_ richMediaManager: PWRichMediaManager!, didPresent richMedia: PWRichMedia!) {
inAppIsPresenting = true
}
func richMediaManager(_ richMediaManager: PWRichMediaManager!, didClose richMedia: PWRichMedia!) {
inAppIsPresenting = false
if let idx = queue.firstIndex(where: { $0 === richMedia }) {
queue.remove(at: idx)
}
if ((queue.count) != 0) {
PWModalWindowConfiguration.shared().presentModalWindow(queue.first!)
}
}
func richMediaManager(_ richMediaManager: PWRichMediaManager!, presentingDidFailFor richMedia: PWRichMedia!, withError error: Error!) {
self.richMediaManager(richMediaManager, didClose: richMedia)
}
}