- Create a nativescript project, add the latest master build of the ios-runtime and prepare it for the ios platform
- Open your xcode project located under platforms/ios/[project_name].xcodeproj in Xcode
- Add a new target to your Xcode app project through File > New > Target and choose the iMessage template.
- Delete MessagesViewController.h, MessagesViewController.m and MainInterface.storyboard files from the extension folder
- Click on the project on the upper left in the file browser, click on the project again in the second-to-left panel, and click on the Info tab at the top of the inner panel. Set tne platforms/ios/[project_name]/build.xcconfig as your extension target Based on Configuration File
- Click on the project on the upper left in the file browser, click on the extension target in the second-to-left panel, and click the Build Phases pane. Then create new run script phase with the following script content
"$SRCROOT/internal/nativescript-pre-build"
and make sure this is at the top of the list directly underTarget Dependencies
- Then click
Copy Bundle Resources
and add your host applicationsapp
folder. - Right-click the [extension name folder] >> SupportingFiles and add a new Objective-C file naming it main.m with the following content
#include <Foundation/Foundation.h>
#include <JavaScriptCore/JavaScriptCore.h>
#include <NativeScript/NativeScript.h>
#include <TNSExceptionHandler.h>
TNSRuntime* runtime;
__attribute__((constructor))
void initialize() {
extern char startOfMetadataSection __asm(
"section$start$__DATA$__TNSMetadata");
[TNSRuntime initializeMetadata:&startOfMetadataSection];
runtime = [[TNSRuntime alloc] initWithApplicationPath:[NSBundle mainBundle].bundlePath];
TNSRuntimeInspector.logsToSystemConsole = YES;
[runtime executeModule:@"./MessagesMainController"];
}
- Right-click the app folder of your application and add a new JavaScript file named MessagesMainController.js with the following content
MSMessagesAppViewController.extend({
viewDidLoad: function() {
this.super.viewDidLoad();
let button = UIButton.buttonWithType(UIButtonTypeSystem);
button.setTitleForState("Click here!", UIControlStateNormal);
button.frame = CGRectMake(0, 0, 160.0, 40.0);
button.addTargetActionForControlEvents(this, "tap", UIControlEvents.UIControlEventTouchUpInside)
this.view.addSubview(button);
},
tap() {
let layout = MSMessageTemplateLayout.alloc().init();
layout.caption = "NativeScript rocks !";
// create a message and tell it the content and layout
let message = MSMessage.alloc().init();
message.layout = layout;
this.activeConversation.insertMessageCompletionHandler(message, null);
},
didBecomeActiveWithConversation(conversation) {
// Called when the extension is about to move from the inactive to active state.
// This will happen when the extension is about to present UI.
// Use this method to configure the extension and restore previously stored state.
},
didReceiveMessageConversation(message, conversatio) {
// Called when a message arrives that was generated by another instance of this
// extension on a remote device.
// Use this method to trigger UI updates in response to the message.
},
didStartSendingMessageConversation(message, conversation) {
// Called when the user taps the send button.
}
}, {
name: "MessagesMainController",
exposedMethods: {
"tap": {
returns: interop.types.void
}
}
});
- In the [extension folder]/Info.plist, replace the NSExtensionMainStoryboard property set to MainInterface, with NSExtensionPrincipalClass property set to MessagesMainController
To run the application open platforms/ios/[project_name].xcodeproj in Xcode, select the extension target and press Run