Codename One - 获取Android和iOS

时间:2018-05-21 23:30:18

标签: codenameone

读者更新:此处讨论的代码现已在此Codename One库中提供(并且它也在CN1扩展管理器中...):https://github.com/jsfan3/CN1Libs-NativeLogsReader

简短问题:

我编写了一个有效的原生Android代码,用于在执行我的Codename One应用程序时获取设备的本机日志。我使用了native interface functionality provided by Codename One。我还需要帮助来实现iOS界面。

长问题

首先,我试图获取原生日志(例如Android Studio和XCode提供的那些),以便在我的应用程序中出现奇怪的行为时请求帮助...因为标准的Codename在一些情况下,一个日志不足(例如,我遇到了使用Google Maps CN1Lib等原生组件的问题。)

我是Android Studio的新用户,而且我遇到了我的Codename One应用程序的Logcat,我也很难使用构建服务器提供的本机资源。而且,我没有Mac,所以我不能使用XCode。

我的语言技能仅限于Codename One Java 8,我不会说话#34; Android原生Java,我觉得iOS原生Objective-C是不可读的......

这就是为什么,为了帮助自己提供准确的日志,当我需要在Codename One的Github存储库中提交一些问题时,我尝试编写本机代码来获取本机登录String (我可以通过几种简单的方式管理,例如我可以在表单中显示它,我可以通过电子邮件发送给自己)

所以......我能够实现Android的代码:它完美无缺。我在几个Android版本中测试过。

CallNativeCode.java

package ...;

import com.codename1.system.NativeInterface;

/**
 * Native Code interface
 */
public interface CallNativeCode extends NativeInterface {

    public void clearAndRestartLog();
    public String readLog();
    public String getFilePath();

}

CallNativeCodeImpl.java

package ...;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CallNativeCodeImpl {

    public String getFilePath() {
        return null;
    }

    public void clearAndRestartLog() {
        // https://developer.android.com/studio/command-line/logcat
        try {
            Runtime.getRuntime().exec("logcat -b all -c");
            Runtime.getRuntime().exec("logcat");
        } catch (IOException ex) {
            // logcat non available?
        }
    }

    // original code: https://stackoverflow.com/q/12692103
    public String readLog() {
        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
                log.append("\n");
            }
            return log.toString();
        } catch (IOException e) {
            return "Log is not available.";
        }
    }

    public boolean isSupported() {
        return true;
    }

}

它是这样的:在Codename One应用程序的主类的init()中我添加了:

// Test of Native code
        CallNativeCode callNativeCode = NativeLookup.create(CallNativeCode.class);
        if (callNativeCode != null && callNativeCode.isSupported()) {
            Log.p("Native code can be executed");
            callNativeCode.clearAndRestartLog();
            Log.p("Native LOG cleared and restarted");
        } else {
            Log.p("Native code cannot be executed");
        }

然后,当我想获得应用程序的本机日志时,我可以执行:

String nativeLog = callNativeCode.readLog();

通过这种方式,我可以获得Android Studio Logcat的相同输出,而无需使用Android Studio,也无需连接到计算机的设备。

我尝试为iOS复制此功能...但我遇到了麻烦,因为我不了解Objective-C。我试图将本机输出日志重定向到一个文件,然后读取该文件(调整我找到的一些代码并试图猜测它是如何工作的)......但我不知道该怎么做以及我的代码不在iOS构建服务器上编译。

以下代码是我尝试做的。怎么修好?感谢

myPackageName_CallNativeCodeImpl.h

#import <Foundation/Foundation.h>

@interface cool_teammate_registration_CallNativeCodeImpl : NSObject {
}

-(NSString*)readLog;
-(NSString*)getFilePath;
-(void)clearAndRestartLog;
-(BOOL)isSupported;
@end

myPackageName_CallNativeCodeImpl.m

#import "myPackageName_CallNativeCodeImpl.h"

@implementation myPackageName_CallNativeCodeImpl

-(NSString*)getFilePath{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString* fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
    NSString* logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    return logFilePath;
}

-(NSString)readLog{
    NSString* logFilePath = [self getFilePath];
    NSString* content = [NSString stringWithContentsOfFile:logFilePath encoding:NSUTF8StringEncoding error:nil];
    return content;
}

-(void)clearAndRestartLog{
    // https://stackoverflow.com/questions/5179108/iphone-how-to-read-application-logs-from-device
    NSString* logFilePath = [self getFilePath];
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}

-(BOOL)isSupported{
    return YES;
}

@end

1 个答案:

答案 0 :(得分:1)

我的iOS代码中存在拼写错误(const styles = StyleSheet.create({ navigationContainer:{ flex: 1, width: '100%', backgroundColor: 'white', }, navigationItem: { flex: 1, } }); 而不是NSString)。

我在这篇文章中找到了有用的信息:Log iOS Errors to file。我使用该信息对代码进行了一些小优化。

最后,这是我的工作代码(在iOS 8,iOS 9,iOS 10,iOS 11上成功测试)。通过这种方式,我可以获得控制台日志,而无需将设备连接到安装了XCode的Mac:)

<强> myPackageName_CallNativeCodeImpl.m

NSStrings*