2014-12-07 130 views
0

當我的應用程序啓動時,模式視圖向用戶顯示,幷包含一個選項以調整「設置」應用程序中的設置。如果選擇了這個選項,我使用的OpenURL如下:使用openurl時,設置應用程序捆綁軟件未顯示在設置應用程序中

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

這成功地將用戶重定向到在設置應用我的應用程序的設置。問題在於,用戶看到的唯一設置是「使用蜂窩數據」單選按鈕。我無法找到root.plist中的設置。在隨後對設置應用中的此視圖進行的所有訪問中,root.plist設置都會正確加載。

我的理論是,這是一個計時問題,我的應用程序的root.plist由於某種原因尚未加載到設置應用程序中。有誰知道這是否是這種情況?我可以強迫它加載嗎?將用戶指向不存在的設置很尷尬和令人困惑。

回答

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約翰斯頓

- (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; 
} 
+1

雖然此鏈接可以回答這個問題,最好是包括這裏的答案的基本部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – 2014-12-07 06:24:36

+0

謝謝@BobGilmore你的建議使我輸入了更好的信息。從現在開始我會這樣做。 – iCMS 2014-12-08 05:04:23