简体   繁体   English

使用openurl时,应用程序设置捆绑包未显示在设置应用程序中

[英]App settings bundle not showing up in settings app when using openurl

When my app launches, a modal view displays to the user and includes an option to adjust a setting in the Settings app. 启动我的应用程序时,模式视图会显示给用户,并包含一个用于在“设置”应用程序中调整设置的选项。 If this option is selected, I use openURL as follows: 如果选择此选项,我将使用openURL,如下所示:

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

This successfully redirects the user to my app's settings in the Settings app. 这样可以成功将用户重定向到“设置”应用中我应用的设置。 The problem is that the only setting that appears to the user is the "Use Cellular Data" radio button. 问题在于,向用户显示的唯一设置是“使用蜂窝数据”单选按钮。 My settings from the root.plist are nowhere to be found. 我在root.plist中的设置无处可寻。 In all subsequent visits to this view in the Settings app, the root.plist settings all load correctly. 在“设置”应用程序中对该视图的所有后续访问中,所有root.plist设置均正确加载。

My theory is that this is a timing issue and that my app's root.plist has not yet been loaded in the Settings app for some reason. 我的理论是这是一个计时问题,由于某种原因,我的应用程序的root.plist尚未加载到“设置”应用程序中。 Does anyone know if this is the case? 有谁知道是这样吗? Can I force it to load somehow? 我可以强迫它以某种方式加载吗? It's awkward and confusing to direct the user to non-existent settings. 将用户定向到不存在的设置很尴尬且令人困惑。

At the following link: 在以下链接中:

NSUserDafaults reading Root.plist got a nil value NSUserDafaults读取Root.plist的值为零

Someone stated that "The values from settings.bundle do not actually load to NSUserDefaults until the user opens the settings for the first time. By default, they are off. Once the user opens the settings bundle, they will fill in for you." 有人说“在用户第一次打开设置之前,settings.bundle中的值实际上不会加载到NSUserDefaults。默认情况下,它们是关闭的。用户打开设置包后,它们将为您填充。”

and I believe some solutions were proposed here: 我相信这里提出了一些解决方案:

Can you make the settings in Settings.bundle default even if you don't open the Settings App 即使不打开“设置”应用程序,也可以将Settings.bundle中的设置设为默认设置吗

Such as this one from @Lawrence Johnston 例如@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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM