Last active
November 25, 2016 12:57
-
-
Save hsylife/8baed1fb58fb38949abe9ed92293eb86 to your computer and use it in GitHub Desktop.
How to exchange Realm data between watchOS and iOS ref: http://qiita.com/hsylife/items/26629e65bf66d79cd311
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RealmSwift | |
public class Field: Object { | |
public dynamic var text: String? | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
target 'SampleRealmOnWatchOS3' do | |
dynamic frameworks | |
use_frameworks! | |
pod 'RealmSwift' | |
end | |
target 'SampleWatchApp_Extension' do | |
use_frameworks! | |
platform :watchos, '2.0' | |
pod 'RealmSwift' | |
end | |
post_install do |installer| | |
installer.pods_project.targets.each do |target| | |
target.build_configurations.each do |config| | |
config.build_settings['SWIFT_VERSION'] = '3.0' | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController, WCSessionDelegate { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) { | |
if let text = message["text"] as! String?{ | |
messageLabel.setText(text) | |
} | |
replyHandler(["reply" : "OK"]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sendMessage(){ | |
if WCSession.default().isReachable { | |
let applicationDict = ["text": messageTextField.text ?? ""] | |
WCSession.default().sendMessage(applicationDict, | |
replyHandler: { replyDict in print(replyDict) }, | |
errorHandler: { error in print(error.localizedDescription)}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment