使用openurl时,应用设置包未显示在设置应用中

时间:2014-12-07 04:42:00

标签: ios objective-c iphone settings settings.bundle

当我的应用程序启动时,会向用户显示模式视图,并包含一个用于调整“设置”应用中的设置的选项。如果选择此选项,我将按如下方式使用openURL:

if (&UIApplicationOpenSettingsURLString != NULL) {
        NSURL *appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:appSettings];
}

这会成功将用户重定向到我的应用在“设置”应用中的设置。问题是用户看到的唯一设置是"使用蜂窝数据"单选按钮。我无法找到root.plist中的设置。在“设置”应用中随后访问此视图时,root.plist设置都会正确加载。

我的理论是,这是一个时间问题,我的应用程序的root.plist由于某种原因尚未加载到“设置”应用中。有谁知道是否是这种情况?我能以某种方式强行加载吗?将用户引导到不存在的设置会很尴尬和困惑。

1 个答案:

答案 0 :(得分:1)

在以下链接中:

NSUserDafaults reading Root.plist got a nil value

有人说“来自settings.bundle的值实际上并没有加载到NSUserDefaults,直到用户第一次打开设置。默认情况下,它们是关闭的。一旦用户打开设置包,他们将填写您。”

我相信这里提出了一些解决方案:

Can you make the settings in Settings.bundle default even if you don't open the Settings App

比如来自@Lawrence Johnston的这个

- (void)registerDefaultsFromSettingsBundle {
    [[NSUserDefaults standardUserDefaults] registerDefaults:[self defaultsFromPlistNamed:@"Root"]];
}

- (NSDictionary *)defaultsFromPlistNamed:(NSString *)plistName {
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    NSAssert(settingsBundle, @"Could not find Settings.bundle while loading defaults.");

    NSString *plistFullName = [NSString stringWithFormat:@"%@.plist", plistName];

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:plistFullName]];
    NSAssert1(settings, @"Could not load plist '%@' while loading defaults.", plistFullName);

    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
    NSAssert1(preferences, @"Could not find preferences entry in plist '%@' while loading defaults.", plistFullName);

    NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
    for(NSDictionary *prefSpecification in preferences) {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        id value = [prefSpecification objectForKey:@"DefaultValue"];
        if(key && value) {
            [defaults setObject:value forKey:key];
        } 

        NSString *type = [prefSpecification objectForKey:@"Type"];
        if ([type isEqualToString:@"PSChildPaneSpecifier"]) {
            NSString *file = [prefSpecification objectForKey:@"File"];
            NSAssert1(file, @"Unable to get child plist name from plist '%@'", plistFullName);
            [defaults addEntriesFromDictionary:[self defaultsFromPlistNamed:file]];
        }        
    }

    return defaults;
}