Skip to content

Commit 376651e

Browse files
committed
添加iOS-Swift代码示例
1 parent 756b89a commit 376651e

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import UIKit
2+
import React
3+
import React_RCTAppDelegate
4+
import ReactAppDependencyProvider
5+
6+
@main
7+
class AppDelegate: UIResponder, UIApplicationDelegate {
8+
var window: UIWindow?
9+
10+
var reactNativeDelegate: ReactNativeDelegate?
11+
var reactNativeFactory: RCTReactNativeFactory?
12+
13+
func application(
14+
_ application: UIApplication,
15+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
16+
) -> Bool {
17+
let delegate = ReactNativeDelegate()
18+
let factory = RCTReactNativeFactory(delegate: delegate)
19+
delegate.dependencyProvider = RCTAppDependencyProvider()
20+
21+
reactNativeDelegate = delegate
22+
reactNativeFactory = factory
23+
24+
window = UIWindow(frame: UIScreen.main.bounds)
25+
26+
factory.startReactNative(
27+
withModuleName: "HelloWord",
28+
in: window,
29+
launchOptions: launchOptions
30+
)
31+
32+
//************************************************ JPush Need************************************************
33+
let entity = JPUSHRegisterEntity()
34+
entity.types = 0
35+
JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
36+
37+
return true
38+
}
39+
40+
//************************************************ JPush Need************************************************
41+
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
42+
// JPush 注册devicetoken
43+
JPUSHService.registerDeviceToken(deviceToken)
44+
}
45+
46+
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
47+
// 注意调用
48+
JPUSHService.handleRemoteNotification(userInfo)
49+
NotificationCenter.default.post(name: NSNotification.Name(J_APNS_NOTIFICATION_ARRIVED_EVENT), object: userInfo)
50+
completionHandler(.newData)
51+
}
52+
53+
}
54+
55+
class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate {
56+
override func sourceURL(for bridge: RCTBridge) -> URL? {
57+
self.bundleURL()
58+
}
59+
60+
override func bundleURL() -> URL? {
61+
#if DEBUG
62+
RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
63+
#else
64+
Bundle.main.url(forResource: "main", withExtension: "jsbundle")
65+
#endif
66+
}
67+
}
68+
69+
70+
//************************************************JPush Need ************************************************
71+
72+
extension AppDelegate:JPUSHRegisterDelegate {
73+
//MARK - JPUSHRegisterDelegate
74+
@available(iOS 10.0, *)
75+
func jpushNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
76+
withCompletionHandler completionHandler: ((Int) -> Void)) {
77+
let userInfo = notification.request.content.userInfo
78+
79+
if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true) {
80+
// 注意调用
81+
JPUSHService.handleRemoteNotification(userInfo)
82+
NotificationCenter.default.post(name: NSNotification.Name(J_APNS_NOTIFICATION_ARRIVED_EVENT), object: userInfo)
83+
print("收到远程通知:\(userInfo)")
84+
} else {
85+
NotificationCenter.default.post(name: NSNotification.Name(J_LOCAL_NOTIFICATION_ARRIVED_EVENT), object: userInfo)
86+
print("收到本地通知:\(userInfo)")
87+
}
88+
89+
completionHandler(Int(UNNotificationPresentationOptions.badge.rawValue | UNNotificationPresentationOptions.sound.rawValue | UNNotificationPresentationOptions.alert.rawValue))
90+
}
91+
92+
@available(iOS 10.0, *)
93+
func jpushNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: (() -> Void)) {
94+
95+
let userInfo = response.notification.request.content.userInfo
96+
if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true) {
97+
// 注意调用
98+
JPUSHService.handleRemoteNotification(userInfo)
99+
NotificationCenter.default.post(name: NSNotification.Name(J_APNS_NOTIFICATION_OPENED_EVENT), object: userInfo)
100+
print("点击远程通知:\(userInfo)")
101+
102+
} else {
103+
print("点击本地通知:\(userInfo)")
104+
NotificationCenter.default.post(name: NSNotification.Name(J_LOCAL_NOTIFICATION_OPENED_EVENT), object: userInfo)
105+
}
106+
107+
completionHandler()
108+
109+
}
110+
111+
func jpushNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification) {
112+
113+
}
114+
115+
func jpushNotificationAuthorization(_ status: JPAuthorizationStatus, withInfo info: [AnyHashable : Any]?) {
116+
print("receive notification authorization status:\(status), info:\(String(describing: info))")
117+
}
118+
119+
120+
// //MARK - 自定义消息
121+
func networkDidReceiveMessage(_ notification: NSNotification) {
122+
let userInfo = notification.userInfo!
123+
NotificationCenter.default.post(name: NSNotification.Name(J_CUSTOM_NOTIFICATION_EVENT), object: userInfo)
124+
}
125+
126+
127+
128+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// HelloWord-Bridging-Header.h
3+
// HelloWord
4+
//
5+
// Created by Shuni Huang on 2025/9/23.
6+
//
7+
8+
#ifndef HelloWord_Bridging_Header_h
9+
#define HelloWord_Bridging_Header_h
10+
11+
//JPush Need
12+
#import "JPUSHService.h"
13+
#import "RCTJPushModule.h"
14+
15+
#endif /* HelloWord_Bridging_Header_h */

example/ios-swift/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
如果iOS项目是Swift代码。
2+
3+
可以根据以下操作来完成 AppDelegate.swift 文件里的配置。
4+
5+
1. 新建Swift调用OC代码的桥接文件。
6+
7+
XCode - File from Template - Header File, 键入桥接文件名, 一般为 iOS项目名-Bridging-Header.h ,创建桥接文件。并在Xcode 的 Build Settings 中找到 Swift Compiler - General>> -> Objective-C Bridging Header 选项,将新建的桥接头文件路径填入其中。并将 "JPUSHService.h" 和 "RCTJPushModule.h" 文件通过 #import 方式引入。
8+
9+
桥文件的代码示例:也可以参考ios-swift/HelloWord-Bridging-Header.h 文件。
10+
11+
```
12+
#ifndef HelloWord_Bridging_Header_h
13+
#define HelloWord_Bridging_Header_h
14+
15+
//JPush Need
16+
#import "JPUSHService.h"
17+
#import "RCTJPushModule.h"
18+
19+
#endif /* HelloWord_Bridging_Header_h */
20+
```
21+
22+
23+
2. AppDelegate.h 文件的配置可以参考ios-swift/AppDelegate.swift中的配置,请关注标注 JPush Need 的代码。
24+

0 commit comments

Comments
 (0)