Skip to content

Instantly share code, notes, and snippets.

@miguel12345
miguel12345 / 0_reuse_code.js
Created November 27, 2013 13:39
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
// Here's a method that takes a block
- (void)doMathWithBlock:(int (^)(int, int))mathBlock {
self.label.text = [NSString stringWithFormat:@"%d", mathBlock(3, 5)];
}
 
// Calling that method with a block
- (IBAction)buttonTapped:(id)sender {
[self doMathWithBlock:^(int a, int b) {
return a + b;
}];
@miguel12345
miguel12345 / new_gist_file.html
Created November 28, 2013 20:02
Videos and fallbacks
<video id="news" width=640 height=480 controls preload> <!-- WebM format for Firefox and Chrome --> <source src="news.webm" type='video/webm; codecs="vp8, vorbis"'> <!-- H.264 format for IE and Safari --> <source src="news.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'> <!-- Fall back on the Flash plugin --> <object width=640 height=480 type="application/x-shockwave-flash" data="flash_movie_player.swf"> <!-- Param elements here configure the Flash movie player you're using --> <!-- Text is the ultimate fallback content --> <div>video element not supported and Flash plugin not installed.</div> </object> </video>
@miguel12345
miguel12345 / new_gist_file.html
Created November 29, 2013 08:05
Canvas basic example
<body> This is a red square: <canvas id="square" width=10 height=10></canvas>. This is a blue circle: <canvas id="circle" width=10 height=10></canvas>. <script> var canvas = document.getElementById("square"); var context = canvas.getContext("2d"); context.fillStyle = "#f00"; context.fillRect(0,0,10,10); canvas = document.getElementById("circle"); context = canvas.getContext("2d"); context.beginPath(); context.arc(5, 5, 5, 0, 2*Math.PI, true); context.fillStyle = "#00f"; context.fill(); </script> </body>
@miguel12345
miguel12345 / new_gist_file.js
Created November 29, 2013 08:49
Users geolocation img element
// Return a newly created <img> element that will (once geolocation succeeds) // be set to display a Google map of the current location. Note that the caller // must insert the returned element into the document in order to make it // visible. Throws an error if geolocation is not supported in the browser function getmap() { // Check for geolocation support if (!navigator.geolocation) throw "Geolocation not supported"; // Create a new <img> element, start a geolocation request to make the img // display a map of where we are, and then return the image. var image = document.createElement("img"); navigator.geolocation.getCurrentPosition(setMapURL); return image; // This function will be invoked after we return the image object, when // (and if) the geolocation request succeeds. function setMapURL(pos) { // Get our position information from the argument object var latitude = pos.coords.latitude; // Degrees N of equator var longitude = pos.coords.longitude; // Degrees E of Greenwich var accuracy = pos.coords.accu
@miguel12345
miguel12345 / new_gist_file.js
Created November 29, 2013 08:51
Geolocation options, and speed/altitude etc
// Determine my location asynchronously and display it in the specified element.
function whereami(elt) {
// Pass this object as the 3rd argument to getCurrentPosition()
var options = {
// Set to true to get a higher accuracy reading (from GPS, for example) // if available. Note, however that this can affect battery life.
enableHighAccuracy: false, // Approximate is okay: this is the default
// Set this property if a cached location is good enough.
// The default is 0, which forces location to be checked anew.
maximumAge: 300000, // A fix from the last 5 minutes is okay
// How long are you willing to wait to get the location?
@miguel12345
miguel12345 / new_gist_file
Created November 29, 2013 09:22
Singleton
+ (MyClass *)sharedInstance {
static MyClass *sharedInstance;
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[MyClass alloc] init];
}
}
return sharedInstance;
}
@miguel12345
miguel12345 / new_gist_file
Created November 29, 2013 09:54
Load view from nib
+(Class*) loadFromNib
{
NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"Class" owner:nil options:nil];
return nibContents[0];
}
@miguel12345
miguel12345 / new_gist_file
Created November 29, 2013 10:06
Verify email
NSString* emailString = @"[email protected]";
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
[emailTest evaluateWithObject:emailString]; //returns BOOL
@miguel12345
miguel12345 / new_gist_file
Created November 29, 2013 10:55
Facebook flow
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
//open!
}
break;
case FBSessionStateClosed: