Wednesday, October 10, 2018

Swift 3.0

Take a screenshot of current screen and make it blur.

//
// Helper.swift
// Blur
//
// Created by Narender Kumar on 09/10/18.
// Copyright © 2018 Appster. All rights reserved.
//

import Foundation
import UIKit


extension UIApplication {
var screenShot: UIImage? {
if let layer = keyWindow?.layer {
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
if let context = UIGraphicsGetCurrentContext() {
layer.render(in: context)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshot
}
}
return nil
}
}



func blurEffect(_ img: UIImage)-> UIImage {
let context = CIContext(options: nil)
let currentFilter = CIFilter(name: "CIGaussianBlur")
let beginImage = CIImage(image: img)
currentFilter!.setValue(beginImage, forKey: kCIInputImageKey)
currentFilter!.setValue(10, forKey: kCIInputRadiusKey)
let cropFilter = CIFilter(name: "CICrop")
cropFilter!.setValue(currentFilter!.outputImage, forKey: kCIInputImageKey)
cropFilter!.setValue(CIVector(cgRect: beginImage!.extent), forKey: "inputRectangle")
let output = cropFilter!.outputImage
let cgimg = context.createCGImage(output!, from: output!.extent)
let processedImage = UIImage(cgImage: cgimg!)
return processedImage
}















Thursday, September 13, 2018

 Keyboard correct height

If you are not getting keyboard correct height and you are using below code.

Objective c
Problem : 
- (void)keyboardWillChange:(NSNotification *)notification {
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
}

The problem is since iOS 8 this will not give the correct value, if the keyboard suggestions is up or if I push them down I get different (not correct) values.


Replace you NSValue code 
Solution 
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];


Replace occurrences of space in URL

Some time developer face issue to validate or perfect in URL below code help you create a correct URL.

I want to replace the spaces with '%20'.


The correct format for replacing space from url is :
Objective C
NSString *urlString;//your url string.

urlString = [originalUrl stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
or
urlString = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
iOS 9 and later
urlString = [originalUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
Swift :
var originalUrl = "https://google.co.in"
var urlString :String = originalUrl.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
ios 9 and later
var urlString :String = originalUrl.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
Swift 4
var urlString = originalString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)

Friday, August 10, 2018

Swift 3.0  Post Local Notification

Notification name should be same for both sender and receiver

// Some where in class A [Post (Broadcast)]

let notificationObjName = Notification.Name("NotificationName")
NotificationCenter.default.post(name: notificationObjName, object: nil)


// And in other class B register for notification

let notificationName = Notification.Name("NotificationName")
NotificationCenter.default.addObserver(self, selector: #selector(ClassB.methodName), name: notificationName, object: nil)

// This method is call when post notification in Class B
func methodName(notification: NSNotification) {
        // Do, what you want
    }



Pass data using notification

let tempDict:[String: UIImage] = ["image": image]

// post a notification with data to class A
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: tempDict)
        
// Register to receive notification in your class B
NotificationCenter.default.addObserver(self, selector: #selector(classB.methodName(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

// recive notification
func methodName(_ notify: NSNotification) {        
    if let image = notify.userInfo?["image"] as? UIImage {
       // Do, what you want
     }
}










Swift 3.0 - Check if array contains element with property 

if array.contains(where: { $0.someProperty == "matchProperty" }) {
     // Property found
} else {
    // Property not found
}


    let elements = [1, 2, 3, 4, 5]
    if elements.contains(5) { 
        // Found
      } else {
         // Not Found
      }

Saturday, June 30, 2018


If no Simulator showing in xcode try this



I could not find any solution that would fix my issue. All simulators were there for all projects but the one that I needed them.
Solution:
Build Settings -> Architectures -> Supported Platforms:
changed from iphoneos to iOS

xcode : 9.3