在NativeScript App中注入纯Java/Obj-C代码 [英] Inject pure Java / Obj-C code in NativeScript App

查看:135
本文介绍了在NativeScript App中注入纯Java/Obj-C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发适用于Android/iOS的应用程序,现在我处于让用户与Facebook互动,尤其是向其朋友发送私人消息的阶段.

I'm currently developing an App for Android / iOS, and now i'm at the stage to let my users interact with Facebook, and especially send private message to their friends.

Facebook为此针对iOS实现了一个SDK:

Facebook implement a SDK for that, for iOS:

FBSDKSendButton *button = [[FBSDKSendButton alloc] init];
button.shareContent = content; 
[self.view addSubview:button];

对于Android:

SendButton sendButton = (SendButton)findViewById(R.id.fb_send_button);
sendButton.setShareContent(shareContent);
sendButton.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() { ... });

但是我不确定我是否可以在NativeScript中真正使用这些代码. 是否有人在使用Facebook发送按钮和NativeScript方面有任何经验,或者至少可以弄清是否可行.

But i'm unsure if i can actually use those pieces of code with NativeScript. Does anyone have any experience with facebook send button and NativeScript or at least could shed a light on whether its possible or not.

非常感谢

推荐答案

上周我问了一个类似的问题,并且能够使用addSubview调用来像这样工作:

I asked a similar question last week and was able to get use the addSubview call to work like this:

var mainView = args.object.getViewById('main-view');
mainView.ios.addSubview(publisher.view);

我的XML如下:

<Page loaded="pageLoaded">
 <StackLayout id="main-view">
 </StackLayout>
</Page>

在XML中创建的视图上需要一个ID,然后在函数的代码隐藏文件中使用args.object.getViewById('yourViewID');创建变量,然后调用viewVariable.ios.addSubview('view-you-want-to-add');.

You'll need an ID on the view you created in your XML, then in the code-behind file in your function, use the args.object.getViewById('yourViewID'); to create a variable and then call viewVariable.ios.addSubview('view-you-want-to-add');.

在我的情况下,我框架的publisher对象是我传递给.ios.addSubview()调用的对象.

In my case my framework's publisher object is what I passed to the .ios.addSubview() call.

还有完整的代码隐藏文件:

Full code-behind file as well:

var vmModule = require("./main-view-model");

// moving OpenTok out of the plugin and into code base for dev.
// Will move into a plugin after
var OpenTok = {
    createSession: function(options) {
        var apiKey = 'private';
        var sessionID = 'private';
        var delegate = this;

        // Framework Method
        var session = OTSession.alloc().init();
        return session.initWithApiKeySessionIdDelegate(apiKey, sessionID, delegate);
      },
      createPublisher: function() {
        // Framework Method
        var publisher = OTPublisher.alloc().init();

        return publisher;
      }
};

function pageLoaded(args) {
    var page = args.object;
    var mainView = args.object.getViewById('main-view');
    var token = "private";
    var error = new interop.Reference();
    page.bindingContext = vmModule.mainViewModel;

    var session = OpenTok.createSession();
    // Framework Method
    session.connectWithTokenError(token, error);

    var publisher = OpenTok.createPublisher();
    // Framework Method
    publisher.initWithDelegate(publisher);
    // Native iOS Method
    mainView.ios.addSubview(publisher.view);

    var checkConnectionStatus = setInterval(function(){
      if ( session.sessionConnectionStatus == 1 ) {
          console.log('clear interval');
          clearInterval(checkConnectionStatus);
          // Framework Method
          session.publishError(publisher, null);
      }
    }, 1000);

}

exports.pageLoaded = pageLoaded;

这是我的问题,如果您仍然遇到问题,还可能会提供更多见解: Nativescript addSubview

This is my question that might also provide more insight if you're still stuck: Nativescript addSubview

这篇关于在NativeScript App中注入纯Java/Obj-C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆