我创建了.plist,其中我放置了用于谷歌分析的字符串,问题是该字符串无法传递到appdelegate.m中的代码,其中是谷歌分析代码。
configuration.plist看起来像这样: GAnalytics,字符串,“XXXXXXXX”
我已经创建了.h和.m文件供它阅读
configuration.h
#import <Foundation/Foundation.h>
@interface Configuration : NSObject
+(instancetype)sharedConfig;
-(NSString *)GoogleAnalyticsID;
@end
configuration.m
#import "Configuration.h"
@interface Configuration ()
@property (strong, nonatomic) NSDictionary *config;
@end
@implementation Configuration
+(instancetype)sharedConfig
{
static Configuration *instance = nil;
if (!instance) {
instance = [[Configuration alloc] init];
}
return instance;
}
-(instancetype)init
{
self = [super init];
if (self) {
NSBundle *bundle = [NSBundle mainBundle];
NSString *configPath = [bundle pathForResource:@"Configuration" ofType:@"plist"];
self.config = [NSDictionary dictionaryWithContentsOfFile:configPath];
}
return self;
}
-(NSString *)GoogleAnalyticsID
{
return self.config[@"GAnalytics"];
}
@end
appdelegate.m
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 20;
[[GAI sharedInstance] trackerWithTrackingId: [[Configuration sharedConfig] GoogleAnalyticsID]];