Wednesday, May 20, 2015

Swift: URL response caching using NSURLCache.

Download XCode project

//Create request with caching policy
let request = NSURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 60)
//Get cache response using request object
let cacheResponse = NSURLCache.sharedURLCache().cachedResponseForRequest(request)
//check if cached response is available if nil then hit url for data
if cacheResponse == nil {

            //default configuration
            let config = NSURLSessionConfiguration.defaultSessionConfiguration()

            //Enable url cache in session configuration and assign capacity
            config.URLCache = NSURLCache.sharedURLCache()
            config.URLCache = NSURLCache(memoryCapacity: 51200, diskCapacity: 10000, diskPath: "urlCache")
            //create session with configration
            let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
            //create data task to download data and having completion handler
            let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
                //below two lines will cache the data, request object as key
                let cacheResponse = NSCachedURLResponse(response: response, data: data)
                NSURLCache.sharedURLCache().storeCachedResponse(cacheResponse, forRequest: request)
        } else {
            //if cache response is not nil then print
            let string = NSString(data: cacheResponse!.data, encoding: NSUTF8StringEncoding)


Saturday, April 25, 2015

Android device screen cast or screen recording using command prompt in windows machine.

Follow below steps:

1. Open command prompt (cmd) in windows (click window+R, type cmd in it and press enter)

2. Go to adb (android debug bridge) platform tools, it will be in your android sdk folder:
     Example - C:\Users\abc\AppData\Local\Android\sdk\platform-tools

3. Connect your android phone

4. Run command to record: adb shell screenrecord --verbose ./sdcard/record-video.mp4

5. To stop recording, press: Ctrl+C