NativeScript / nativescript-ios-imessages

Simple app extension that interact with the Messages app
14 stars 1 forks source link

nativescript-ios-imessages

Simple app extension that interact with the Messages app

Alt text

  1. Create a nativescript project, add the latest master build of the ios-runtime and prepare it for the ios platform
  2. Open your xcode project located under platforms/ios/[project_name].xcodeproj in Xcode
  3. Add a new target to your Xcode app project through File > New > Target and choose the iMessage template.
  4. Delete MessagesViewController.h, MessagesViewController.m and MainInterface.storyboard files from the extension folder
  5. 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
  6. 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 under Target Dependencies
  7. Then click Copy Bundle Resources and add your host applications app folder.
  8. 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

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"];

}


1. Right-click the app folder of your application and add a new JavaScript file named MessagesMainController.js with the following content 
  ```javascript
    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
          }
      }
  });
  1. 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