diff --git a/Bark.xcodeproj/project.pbxproj b/Bark.xcodeproj/project.pbxproj index e3d64cc..c0c478e 100644 --- a/Bark.xcodeproj/project.pbxproj +++ b/Bark.xcodeproj/project.pbxproj @@ -35,6 +35,7 @@ 06802E5320ECC40C00767047 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0661A549204FDA4100965E4E /* Assets.xcassets */; }; 06B1158D247BA6D5006D91FB /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 06B1158C247BA6D5006D91FB /* CloudKit.framework */; }; 06B1158F247BB1FB006D91FB /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06B1158E247BB1FB006D91FB /* Message.swift */; }; + 06B11591247BC132006D91FB /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06B1158E247BB1FB006D91FB /* Message.swift */; }; 06CF784721C7A50300A052D7 /* NotificationServiceExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 06CF784021C7A50300A052D7 /* NotificationServiceExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 06CF784C21C7A51200A052D7 /* NotificationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06CF784B21C7A51200A052D7 /* NotificationService.swift */; }; 6AF9CC9B5F6CD2BF86509897 /* libPods-Bark.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E4EA0C04469BF7B418FF3CB7 /* libPods-Bark.a */; }; @@ -105,6 +106,7 @@ 0683487220510FB20024B6DA /* UserNotificationsUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotificationsUI.framework; path = System/Library/Frameworks/UserNotificationsUI.framework; sourceTree = SDKROOT; }; 06B1158C247BA6D5006D91FB /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; }; 06B1158E247BB1FB006D91FB /* Message.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Message.swift; sourceTree = ""; }; + 06B11590247BBC15006D91FB /* NotificationServiceExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = NotificationServiceExtension.entitlements; sourceTree = ""; }; 06CF784021C7A50300A052D7 /* NotificationServiceExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = NotificationServiceExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 06CF784421C7A50300A052D7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 06CF784B21C7A51200A052D7 /* NotificationService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationService.swift; sourceTree = ""; }; @@ -235,6 +237,7 @@ 06CF784121C7A50300A052D7 /* NotificationServiceExtension */ = { isa = PBXGroup; children = ( + 06B11590247BBC15006D91FB /* NotificationServiceExtension.entitlements */, 06CF784B21C7A51200A052D7 /* NotificationService.swift */, 06CF784421C7A50300A052D7 /* Info.plist */, ); @@ -508,6 +511,7 @@ buildActionMask = 2147483647; files = ( 06CF784C21C7A51200A052D7 /* NotificationService.swift in Sources */, + 06B11591247BC132006D91FB /* Message.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -745,6 +749,7 @@ baseConfigurationReference = 08AEA046D1FD0D7950208796 /* Pods-NotificationServiceExtension.debug.xcconfig */; buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; + CODE_SIGN_ENTITLEMENTS = NotificationServiceExtension/NotificationServiceExtension.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 5U8LBRXG3A; INFOPLIST_FILE = NotificationServiceExtension/Info.plist; @@ -765,6 +770,7 @@ baseConfigurationReference = 68049930BAF79AFBD8B109B8 /* Pods-NotificationServiceExtension.release.xcconfig */; buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; + CODE_SIGN_ENTITLEMENTS = NotificationServiceExtension/NotificationServiceExtension.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 5U8LBRXG3A; INFOPLIST_FILE = NotificationServiceExtension/Info.plist; diff --git a/Bark.xcodeproj/xcshareddata/xcschemes/Bark.xcscheme b/Bark.xcodeproj/xcshareddata/xcschemes/Bark.xcscheme new file mode 100644 index 0000000..7824f79 --- /dev/null +++ b/Bark.xcodeproj/xcshareddata/xcschemes/Bark.xcscheme @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Bark.xcodeproj/xcshareddata/xcschemes/NotificationContentExtension.xcscheme b/Bark.xcodeproj/xcshareddata/xcschemes/NotificationContentExtension.xcscheme new file mode 100644 index 0000000..78b4bd9 --- /dev/null +++ b/Bark.xcodeproj/xcshareddata/xcschemes/NotificationContentExtension.xcscheme @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Bark.xcodeproj/xcshareddata/xcschemes/NotificationServiceExtension.xcscheme b/Bark.xcodeproj/xcshareddata/xcschemes/NotificationServiceExtension.xcscheme new file mode 100644 index 0000000..96b0311 --- /dev/null +++ b/Bark.xcodeproj/xcshareddata/xcschemes/NotificationServiceExtension.xcscheme @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Bark/AppDelegate.swift b/Bark/AppDelegate.swift index 768c8c7..8aa9c22 100644 --- a/Bark/AppDelegate.swift +++ b/Bark/AppDelegate.swift @@ -13,7 +13,7 @@ import RealmSwift import IceCream @UIApplicationMain -class AppDelegate: UIResponder, UIApplicationDelegate { +class AppDelegate: UIResponder, UIApplicationDelegate{ var window: UIWindow? var syncEngine: SyncEngine? @@ -38,14 +38,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } } - + let groupUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark") + let fileUrl = groupUrl?.appendingPathComponent("bark.realm") let config = Realm.Configuration( - // Set the new schema version. This must be greater than the previously used - // version (if you've never set a schema version before, the version is 0). + fileURL: fileUrl, schemaVersion: 12, - - // Set the block which will be called automatically when opening a Realm with - // a schema version lower than the one set above migrationBlock: { migration, oldSchemaVersion in // We haven’t migrated anything yet, so oldSchemaVersion == 0 if (oldSchemaVersion < 1) { @@ -54,14 +51,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // And will update the schema on disk automatically } }) - // Tell Realm to use this new configuration object for the default Realm Realm.Configuration.defaultConfiguration = config - + //iCloud 同步 syncEngine = SyncEngine(objects: [ SyncObject() ], databaseScope: .private) + + + let realm = try? Realm() + print("message count: \(realm?.objects(Message.self).count ?? 0)") return true } diff --git a/Bark/Bark.entitlements b/Bark/Bark.entitlements index f3e0806..2421988 100644 --- a/Bark/Bark.entitlements +++ b/Bark/Bark.entitlements @@ -6,11 +6,15 @@ development com.apple.developer.icloud-container-identifiers - iCloud.me.fin.barkContainer + iCloud.me.fin.bark com.apple.developer.icloud-services CloudKit + com.apple.security.application-groups + + group.bark + diff --git a/Model/BarkTargetType.swift b/Model/BarkTargetType.swift index fc033d4..e8aaba5 100644 --- a/Model/BarkTargetType.swift +++ b/Model/BarkTargetType.swift @@ -59,9 +59,13 @@ extension BarkTargetType { return NetworkActivityPlugin { (change, type) in switch change { case .began: - UIApplication.shared.isNetworkActivityIndicatorVisible = true + dispatch_sync_safely_main_queue { + UIApplication.shared.isNetworkActivityIndicatorVisible = true + } case .ended: - UIApplication.shared.isNetworkActivityIndicatorVisible = false + dispatch_sync_safely_main_queue { + UIApplication.shared.isNetworkActivityIndicatorVisible = false + } } } } diff --git a/Model/Message.swift b/Model/Message.swift index 66fe629..9f9f7c6 100644 --- a/Model/Message.swift +++ b/Model/Message.swift @@ -17,6 +17,10 @@ class Message: Object { // 设置为 true 后,将被IceCream自动清理 @objc dynamic var isDeleted = false + + override class func primaryKey() -> String? { + return "id" + } } extension Message: CKRecordConvertible {} diff --git a/NotificationServiceExtension/NotificationService.swift b/NotificationServiceExtension/NotificationService.swift index 689d6b2..46ee480 100644 --- a/NotificationServiceExtension/NotificationService.swift +++ b/NotificationServiceExtension/NotificationService.swift @@ -14,7 +14,30 @@ class NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? + lazy var realm:Realm? = { + let groupUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark") + let fileUrl = groupUrl?.appendingPathComponent("bark.realm") + let config = Realm.Configuration( + fileURL: fileUrl, + schemaVersion: 12, + migrationBlock: { migration, oldSchemaVersion in + // We haven’t migrated anything yet, so oldSchemaVersion == 0 + if (oldSchemaVersion < 1) { + // Nothing to do! + // Realm will automatically detect new properties and removed properties + // And will update the schema on disk automatically + } + }) + + // Tell Realm to use this new configuration object for the default Realm + Realm.Configuration.defaultConfiguration = config + + + return try? Realm() + }() + override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { + self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) @@ -28,8 +51,19 @@ class NotificationService: UNNotificationServiceExtension { UIPasteboard.general.string = bestAttemptContent.body } } + + try? realm?.write{ + let message = Message() + message.title = userInfo["title"] as? String + message.body = userInfo["body"] as? String + message.createDate = Date() + realm?.add(message) + } + contentHandler(bestAttemptContent) } + + } } diff --git a/NotificationServiceExtension/NotificationServiceExtension.entitlements b/NotificationServiceExtension/NotificationServiceExtension.entitlements new file mode 100644 index 0000000..59eb47c --- /dev/null +++ b/NotificationServiceExtension/NotificationServiceExtension.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.application-groups + + group.bark + + +