bash

Git Tracking Remote Branches

For some reason, I’ve always failed to remember what the command to track a remote branch is in git. I look it up on this page everytime I want to do it: https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches

Well no more. I figured if I write a post about it, I will remember it so here we go:

git fetch origin
git checkout -b somename origin/somename

That’s it… the branch will be set up to track the remote and you are switched over automatically.

xcode

Using the Facebook iOS SDK in Swift

Just a quick video to show you how to use the facebook native ios sdk in swift.

follow the directions to get your plist file updated here: https://developers.facebook.com/quickstarts/?platform=ios

Once that’s done follow the instructions to add your build identity to facebooks records. Then…

  1. drag the frameworks into your project
  2. update your appdelegate file
  3. update your view controller
  4. make your view controller a delegate for the login button
  5. test
AppDelegate.swift:

replace your applicationdidlaunchwithoptions:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }

Then add the following:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
    }

Then in your “applicationDidBecomeActive“:

FBSDKAppEvents.activateApp()

NOTE: make sure to import:

import FBSDKCoreKit
ViewController.swift:
//
//  ViewController.swift
//  FacebookTutorial
//
//  Created by Mike Newell on 8/13/15.
//  Copyright (c) 2015 Mike Newell. All rights reserved.
//

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        if FBSDKAccessToken.currentAccessToken() != nil {
            // user already has access token
            self.logUserData()
        } else {
            let loginButton = FBSDKLoginButton()
            loginButton.center = self.view.center
            loginButton.readPermissions = ["email"]
            self.view.addSubview(loginButton)
            loginButton.delegate = self
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(animated: Bool) {
        self.logUserData()
    }
    
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        println("logged in")
    }
    
    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        println("logged out")
    }
    
    func logUserData() {
        let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
        graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in
            if error != nil {
                println(error)
            } else {
                println(result)
            }
        }
    }


}

That’s it! here’s a video explaining more:

There’s also a great tutorial for anyone looking to do the older “bridging” method of importing the sdk: http://www.brianjcoleman.com/tutorial-how-to-use-login-in-facebook-sdk-4-0-for-swift/

xcode

MPMoviePlayerController Black Screen When Fading

Let’s say you want to animate a new view controller in and immediately start playing a video. You might things it’s as easy as taking a static image of the first frame of the video as the background and then crossfading the video overtop when it’s ready to play…right?

Kinda.

This was a 24 hour bug for me. I kept seeing this black screen before the video loads. I tried adjusting alphas, delays before playing the video, even changing the background color of the view of the video itself! Nothing worked.

Normally many of these solutions would seem right! It turns out you need to look far back in time to find the solution. In IOS 6, MPMoviePlayerController was given a new event to catch. For those of us using “MPMoviePlayerLoadStateDidChangeNotification” you’re doing it wrong… You see this load event is telling you when the video content is loaded and can play all the way through, however, it does NOT tell you when your movie is ready for the display to handle it.

There’s a new-ish notification to watch out for, it’s called: “MPMoviePlayerReadyForDisplayDidChangeNotification” and it’s your silver bullet. This thing only fires after the video has loaded…and is ready for the display!

Set everything up like you normally would, ad your observer:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieReadyForDisplayChanged:) name:MPMoviePlayerReadyForDisplayDidChangeNotification object:nil];

Then add your handler:

- (void) movieReadyForDisplayChanged: (NSNotification *) sentNotification
{
    if (player.loadState & (MPMovieLoadStatePlaythroughOK)) {
        [UIView animateWithDuration:0.1 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
            // animation
            [[player view] setAlpha:1.0];
        } completion:^(BOOL finished) {
            // done
            [player play];
        }];
    }
}

Now your video should only fade in when it’s ready for display. After fading, you actually call “play” on the controller.

I’ll make a video and post it later…

 

xcode

Local Website in an IOS App

So you’re a master at html5 and you want to build a native app on ios, but you don’t know swift or objective-c.

No worries, there are a ton of platforms out there to help with this. But with some basic knowledge of ios you can build an using mostly web technologies!

What we will do is a build a locally hosted website, put it inside a native ios app and run it all together to give the appearance of a native app but built using html5.

You can download the project on my github: https://github.com/newshorts/LocalWebsiteInIOSApp

 

webgl

Timelapse Movie of Your Desktop While You’re Working

i want to start doing timelapses of coding while I’m working, so here goes:

First off set up a terminal command to take a screenshot every 25 seconds:

i=1;while [ 1 ];do screencapture -t jpg -x ~/Desktop/TrickyWays/$i.jpg; let i++;sleep 25; done

Next stitch together the screens with ffmpeg:

ffmpeg -framerate 1 -pattern_type glob -i '~/Desktop/screens/*.jpg' -c:v libx264 out.mp4

thats all!