【问题标题】:No NSLog output in distributed app under iOS 10iOS 10下分布式应用没有NSLog输出
【发布时间】:2016-11-06 15:23:47
【问题描述】:

iOS 10中,Apple 做出了一项更改,即不会在分布式应用程序(企业、应用商店)中发出 NSLog() 输出。

请注意,当从 Xcode 运行时,NSLog() 工作正常。

有没有办法强制调试所有应用(在 beta 测试阶段非常有用)?

这里出现了一些线索:NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?

但是,我们能否为此制定一个明确的实施方案?

【问题讨论】:

    标签: ios ios10 nslog


    【解决方案1】:

    我们的解决方案取决于两个问题:

    1. 我们是否使用 Xcode 8 及更高版本进行编译?如果是,则识别出新的 os_log。如果没有,我们必须回退到现有的 NSLog 行为。
    2. 我们是否在 iOS-10 及更高版本下运行?如果是,我们可以使用新的记录器。如果没有,我们必须回退到现有的 NSLog 行为。

    我们将找到关于编译时间的问题 [1] 的答案。对于 [2],我们必须在运行时进行测试。

    这里是实现:

    mylog.h

    //only used to force its +load() on app initialization
    @interface MyLog:NSObject
    @end
    
    #if !__has_builtin(__builtin_os_log_format)
        //pre Xcode 8. use NSLog
    #else
        //we need this include:
        #import <os/log.h>
    #endif
    
    void myLog(NSString *format, ...);
    
    #ifdef DEBUG
        #define NSLog(f, ...) myLog(f, ## __VA_ARGS__)
    #else 
        #define NSLog(f, ...) (void)0
    #endif
    

    mylog.m

    @implementation MyLog
    
    BOOL g_useNewLogger = NO;
    
    +(void)load
    {
        NSOperatingSystemVersion os_ver = [[NSProcessInfo processInfo] operatingSystemVersion];
        if (os_ver.majorVersion >= 10) {
            g_useNewLogger = YES;
        }
        NSLog(@"Use new logger: %@", g_useNewLogger? @"YES":@"NO");
    }
    @end
    
    void myLog(NSString *format, ...)
    {
        va_list args;
        va_start(args, format);
    #if !__has_builtin(__builtin_os_log_format)
        //pre Xcode 8. use NSLog
        NSLogv(format, args);
    #else
        //Xcode 8 and up
        if (g_useNewLogger) { // >= iOS 10
            NSString *nsstr = [[NSString alloc] initWithFormat:format arguments:args];
            os_log(OS_LOG_DEFAULT, "%{public}s", [nsstr cStringUsingEncoding:NSUTF8StringEncoding]);
        } else { // < iOS 10
            NSLogv(format, args);
        }
    #endif
        va_end(args);
    }
    

    【讨论】:

      【解决方案2】:

      如果您想要一个嵌入式解决方案来获取您的应用程序的日志,我建议您查看我们创建的工具。它被称为Bugfender,它的作用是将所有应用日志发送到我们的服务器,以便您可以在我们的网站上查看它们。

      在进行 Beta 测试时非常有用,因为您的用户可以测试应用程序,如果他们发现问题,您将获得他们在应用程序中所做的所有事情的信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 1970-01-01
        • 1970-01-01
        • 2015-01-02
        • 1970-01-01
        相关资源
        最近更新 更多