Sunday, February 16, 2014

Using a preference bundle in cydget

Recently I found this Reddit post from looking at Saurik's reddit It further explained how to use preferenceloader to load plist entries. After fiddling around a bit, I was able to add this to miWeather Cydget



It implemented very easily, a lot easier than you would think. Now I don't see how we went so long without adding this to Cydget Lockscreens. So I have recorded me creating one, to maybe help some understand the procedure. 





Included in the description is the complete files.


Links:
http://iphonedevwiki.net/index.php/PreferenceLoader
http://modmyi.com/forums/file-mods/22453-how-make-custom-menus-preferences-app-custom-preferences.html


Written instuctions

PreferenceLoader includes a few files.

The plist. This can be named however you see fit.
The icon. Can also be named however you like.

The Plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
</dict>
</plist>

This is your plist in its lowest form. To add options to the plist we will enter them in between <dict></dict>

For example we want to call our icon.
With this entered in between <dict></dict> it will tell our plist its icon.

<key>entry</key>
<dict>
<key>cell</key>
<string>PSLinkCell</string>
<key>icon</key>
<string>Demo@2x.png</string>
<key>label</key>
<string>Demo</string>
</dict>


Give it a title
<key>title</key>
<string>Demo</string>

Make items in your plist
<key>items</key>
<array>
       </array>

Inside the <arrary></array> we can enter separate <dict></dict> tags to contain certain items. Toggles, Text Entry and more.

<dict>
<key>cell</key>
<string>PSGroupCell</string>
<key>label</key>
<string>Demo</string>
</dict>

Above will give you a cell name.
Then under it we can apply different cell items.

              <dict>
<key>cell</key> //What it is
<string>PSEditTextCell</string> //What type of cell
<key>default</key>
<string></string>
<key>defaults</key>
<string>com.JunesiPhone.miWeather</string> //plist it will save to
<key>key</key>   //depicts a key
<string>sCityCodes</string>  //name of our key
<key>label</key> // label for this key
<string>Zip Code/Weather Code</string>
<key>placeholder</key>  //what will show when cell is blank
<string>Enter Zip Code/Weather Code</string>
</dict>

There are multiple cell types.

  • PSEditTextCell //shown above
  • PSLinkListCell //lets you choose from a list of items
  • PSSwitchCell // on/off switch (shows true or false)
  • More i'm unaware of at this point


Complete Plist for miWeather
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>entry</key>
<dict>
<key>cell</key>
<string>PSLinkCell</string>
<key>icon</key>
<string>miWeatherPrefs@2x.png</string>
<key>label</key>
<string>miWeather LockScreen</string>
</dict>
<key>items</key>
<array>
<dict>
<key>cell</key>
<string>PSGroupCell</string>
<key>label</key>
<string>Weather</string>
</dict>

        <dict>
<key>cell</key>
<string>PSEditTextCell</string>
<key>default</key>
<string></string>
<key>defaults</key>
<string>com.JunesiPhone.miWeather</string>
<key>key</key>
<string>sCityCodes</string>
<key>label</key>
<string>Zip Code/Weather Code</string>
<key>placeholder</key>
<string>Enter Zip Code/Weather Code</string>
</dict>
    <dict>
<key>cell</key>
<string>PSGroupCell</string>
</dict>
<dict>
<key>cell</key>
<string>PSLinkListCell</string>
<key>defaults</key>
<string>com.JunesiPhone.miWeather</string>
<key>detail</key>
<string>PSListItemsController</string>
<key>key</key>
<string>options</string>
<key>label</key>
<string>Options</string>
<key>validTitles</key>
<array>
<string>0</string>
<string>1</string>
<string>2</string>
</array>
<key>validValues</key>
<array>
<string>0</string>
<string>1</string>
<string>2</string>
</array>
</dict>
<dict>
<key>cell</key>
<string>PSSwitchCell</string>
<key>default</key>
<false/>
<key>defaults</key>
<string>com.JunesiPhone.miWeather</string>
<key>key</key>
<string>sUnit</string>
<key>label</key>
<string>Turn on for celsius</string>
</dict>
<dict>
<key>cell</key>
<string>PSSwitchCell</string>
<key>default</key>
<false/>
<key>defaults</key>
<string>com.JunesiPhone.miWeather</string>
<key>key</key>
<string>TwentyFourHourClock</string>
<key>label</key>
<string>Turn on for 24hr</string>
</dict>

<dict>
<key>cell</key>
<string>PSGroupCell</string>
<key>label</key>
<string>System/Library/LockCydget/miWeather</string>
</dict>

<dict>
<key>cell</key>
<string>PSSwitchCell</string>
<key>default</key>
<false/>
<key>defaults</key>
<string>com.JunesiPhone.miWeather</string>
<key>key</key>
<string>userwall</string>
<key>label</key>
<string>User Wall</string>
</dict>

<dict>
<key>cell</key>
<string>PSGroupCell</string>
<key>label</key>
<string>Radar</string>
</dict>

<dict>
<key>cell</key>
<string>PSSwitchCell</string>
<key>default</key>
<false/>
<key>defaults</key>
<string>com.JunesiPhone.miWeather</string>
<key>key</key>
<string>world</string>
<key>label</key>
<string>Turn on for outside US</string>
</dict>

<dict>
<key>cell</key>
<string>PSGroupCell</string>
<key>footerText</key>
<string>miWeather by JunesiPhone
http://JunesiPhone.com</string>
</dict>

</array>
<key>title</key>
<string>miWeather</string>
</dict>
</plist>


Complete Plist for the demo
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>entry</key>
<dict>
<key>cell</key>
<string>PSLinkCell</string>
<key>icon</key>
<string>Demo@2x.png</string>
<key>label</key>
<string>Demo</string>
</dict>
<key>items</key>
<array>

 <dict>
<key>cell</key>
<string>PSGroupCell</string>
</dict>
<dict>
<key>cell</key>
<string>PSLinkListCell</string>
<key>defaults</key>
<string>com.JunesiPhone.demo</string>
<key>detail</key>
<string>PSListItemsController</string>
<key>key</key>
<string>options</string>
<key>label</key>
<string>Wallpaper Choices</string>
<key>validTitles</key>
<array>
<string>0</string>
<string>1</string>
<string>2</string>
</array>
<key>validValues</key>
<array>
<string>0</string>
<string>1</string>
<string>2</string>
</array>
</dict>


<dict>
<key>cell</key>
<string>PSGroupCell</string>
<key>footerText</key>
<string>Demo by JunesiPhone
http://JunesiPhone.com</string>
</dict>

</array>
<key>title</key>
<string>Demo</string>
</dict>
</plist>


It looks like a lot, but it is very easy once you get the hang of it.

Once you have these items you place them into Library/PreferenceLoader/Preferences
Close your settings app and reopen it. You will see your preferences.

Now to cydget.

<script type="text/cycript">
   try{
        var settings = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.JunesiPhone.demo.plist"];
       if(settings!=null){
image=settings.options;
       }
    }catch(err){
        alert(err);
    }
</script>

Thats pretty much it for cydget. This script will pull the plist listed. This plist is created by the preferenceloader, and you set the name for it under your defaults key. So once this toggle, switch etc is pressed it updates this plist.

If you see above there is a part of the plist that is bold. It is pertaining to this script I just wrote. You can see our key is called options. Also take note that I gave our cycript a var of settings.

Therefore if you alert(settings); it will show our plist entries. If you alert(settings.options); then it will show the value of the key options. You can now use this key in your javascript.

For the example I used image=settings.options. I passed this value to image. now if you alert(image); it will alert this value.

In the demo code I wrote, I used image to decide on which image to show as the background. Here is the complete html

<!DOCTYPE html>
<html>
<head>
<title>Demo Cydget</title>
</head>
<body>

 <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>

<style type="text/css">#wallpaper{position: absolute;top:0px;left:0pc;}</style>
<div id="wallpaper"><img src="1.jpg" width="320"></div>
<script type="text/javascript">
image="1";
</script>

<script type="text/cycript">
   try{
        var settings = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.JunesiPhone.demo.plist"];
       if(settings!=null){
image=settings.options;
       }
    }catch(err){
        alert(err);
    }
</script>

<script>
width="320"
imageset=image+".jpg"
document.getElementById("wallpaper").innerHTML='<img width='+width+' src='+imageset+'>'
</script>
</body>
</html>

Also note I set the var at the top image="1"; if it doesn't detect a plist, or doesn't read it. Then it will default to that value. If it does, then it changes it accordingly.

The complete code is located in the Video description. Enjoy

1 comment:

  1. hey, I tryied but nothing appears in the setting.app.
    Are you using the plist in a bundle or in a simple approach?
    And how do I set up the com.(yourname).(yourpackage)?

    ReplyDelete