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

A few cycripts

These are a few things you can 1. Run from terminal while in cycript. 2. Add to your lockscreens to use.

Turn on the lockscreen
[[SBBacklightController sharedInstance] turnOnScreenFullyWithBacklightSource:1]
Unlock iPhone (Terminal only)
[[SBLockScreenManager sharedInstance] _finishUIUnlockFromSource:1 withOptions:1]
Stop dimming of the lockscreen
[[SBBacklightController sharedInstance] cancelLockScreenIdleTimer]
Pull info from settings set plist
 var settings = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.JunesiPhone.miWeather.plist"];
Pull info from settings code
 

script type="text/cycript"
   try{
        var settings = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.JunesiPhone.miWeather.plist"];
       if(settings!=null){
       }
    }catch(err){
        alert(err);
    }
/script
SB switcher stuff
 
[[SBUIController sharedInstanceIfExists]_toggleSwitcher]

[[SBUIController sharedInstanceIfExists]dismissSwitcherAnimated:YES]

[[SBUIController sharedInstanceIfExists]getRidOfAppSwitcher]
Unlock with passcode
 
[[SBLockScreenManager sharedInstance] attemptUnlockWithPasscode:@"1234"]
Small status on SB
 
SBLockScreenViewController.messages['statusBarStyle']=function(){return 0}
Allow folders to be placed in folders.
 
SBIconView.messages['canReceiveGrabbedIcon:'] = function (icon){ return YES; }
See if notifications are showing on the Lockscreen
 
[[[SBLockScreenManager sharedInstance] lockScreenViewController] lockScreenIsShowingBulletins];
Use location services
 
manager = [[[CLLocationManager alloc] initWithEffectiveBundle: [NSBundle bundleForClass: CLLocationManager.class]]autorelease]
    manager.delegate = CLLocationManagerDelegate;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
    [manager startUpdatingLocation];
    var GPS = manager.location.coordinate;
    [manager stopUpdatingLocation];
    var Latitude = JSON.stringify(GPS[0]);
    var Longitude = JSON.stringify(GPS[1]);
See if location services is enabled
 
[[CLLocationManager sharedManager] locationServicesEnabled];
Detect if passcode is on
 
[[SBDeviceLockController sharedController]isPasscodeLocked];
Open url in safari
 
mySafari = [UIApplication sharedApplication];
    myURL = [[NSURL alloc]initWithString:@""+url];
    [mySafari openURL:myURL]; //myUrl is your link
    [myURL release];
Disable lock bounce iOS7.1
 
SBLockScreenView.messages['hintDisplacement']=function(){return 0}
Get app notification badge
 
SBIconController.sharedInstance.model.leafIconsByIdentifier["com.apple.MobileSMS"].badgeValue;
Device stuff
 
[[UIDevice currentDevice] name];
[[NSFileManager defaultManager] attributesOfFileSystemForPath:@"/var/mobile" error:NULL];
[NSProcessInfo processInfo].physicalMemory;
[NSProcessInfo processInfo].processorCount;
[NSProcessInfo processInfo].operatingSystemVersionString;
[NSProcessInfo processInfo].systemUptime;
Battery Percent
 
[[SBUIController sharedInstance]batteryCapacityAsPercentage];
Unlock device on iOS7. Thanks to Saurik for his help on this one.
 
   [^void(){[[UIApplication sharedApplication] launchApplicationWithIdentifier:@""+bundle suspended:NO];}
  performSelectorOnMainThread: @selector(invoke) withObject: nil waitUntilDone: NO];
Hide LS items
 
 [[[[SBLockScreenManager sharedInstance]lockScreenViewController]lockScreenView]setSlideToUnlockHidden:1 forRequester:1];
 [[SBAppStatusBarManager sharedInstance] hideStatusBar];
 [[[[SBLockScreenManager sharedInstance]lockScreenViewController]lockScreenView]setTopGrabberHidden:1 forRequester:1];
 [[[[SBLockScreenManager sharedInstance]lockScreenViewController]lockScreenView]setBottomGrabberHidden:1 forRequester:1];
Get last touched icon
 
 [[SBIconController sharedInstance]lastTouchedIcon];
Home button tap by code
 
 [[SBIconController sharedInstance]handleHomeButtonTap];
Display icons or bundles
 
 [[SBApplicationController sharedInstance]allDisplayIdentifiers]
[[SBApplicationController sharedInstance]allApplications]
Hope this helps, has taken me quite awhile to get most of these. Sometimes weeks at a time to figure one out. If you have any let me know, and I will add them. Most all of these can be found in LockBuilder Evo