iOS  Tree

"All of us do not have equal talent. But , all of us have an equal opportunity to develop our talents.” – A.P.J Abdul Kalam

launch safari from iOS app — July 29, 2017

launch safari from iOS app

Objective C:
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"link://"]]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"link://"]];
}

openURL Deprecated in iOS10:
openURL:
A way to open external links
canOpenURL:
Got some privacy controls in iOS 9 to stop you from querying devices for installed apps.

Now with iOS 10 openURL:options:completionHandler: is used instead of openURL

Objective C:
Handle completionHandler:
- (void)openScheme:(NSString *)scheme {
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:scheme];
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
[application openURL:URL options:@{}
completionHandler:^(BOOL success) {
NSLog(@"Open %@: %d",scheme,success);
}];
} else {
BOOL success = [application openURL:URL];
NSLog(@"Open %@: %d",scheme,success);
}
}

Without handling completionHandler:
[application openURL:URL options:@{} completionHandler:nil];

Swift 3.0:
func open(scheme: String) {
if let url = URL(string: scheme) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in print("Open \(scheme): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(scheme): \(success)")
}
}
}

Options: UIApplicationOpenURLOptionUniversalLinksOnly

  • Use a boolean value set to true (YES) to only open the URL if it is a valid universal link with an application configured to open it.
  • If there is no application configured or the user disabled using it to open the link the completion handler is called with false (NO).
  • Objective C:
    NSDictionary *options = @{UIApplicationOpenURLOptionUniversalLinksOnly : @YES};
    [application openURL:URL options:options completionHandler:nil];

    Swift:
    let options = [UIApplicationOpenURLOptionUniversalLinksOnly : true]
    UIApplication.shared.open(url, options: options, completionHandler: nil)

    isKindOfClass — July 17, 2017

    isKindOfClass

  • In Cocoa, almost all classes inherit from NSObject.
  • That means that NSObject is ‘the superclass’, and (almost) the rest of the classes are subclasses of NSObject.
  • uikit_classes

    isKindOfClass:
    Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

    Objective C:
    if ([element isKindOfClass:[UIImageView class]]){
    }

    or

    NSArray *viewControllers = [self.navigationController viewControllers];
    for (id aViewController in viewControllers) {
    if([aViewController isKindOfClass:[TestViewController class]]){
    print(aViewController)
    return;//return for stop the loop
    }
    }

    Swift:

    if (element is UIImageView) {
    }

    or

    let viewControllers: [UIViewController] = (self.navigationController?.viewControllers)! as [UIViewController];
    for aViewController:UIViewController in viewControllers {
    if aViewController.isKind(of: TestViewController.self) {
    print(aViewController)
    return //return for stop the loop
    }
    }

    REMOVE ALL SUBVIEWS — July 10, 2017

    REMOVE ALL SUBVIEWS

    Objective C:
    To remove all subViews

    [self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];

    # pragma mark - Removing all subviews
    - (void) clearAction{
    NSLog(@"clear all subViews");
    [self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
    }

    or
    # pragma mark - Removing all subviews
    - (void) clearAction{
    for (UIView *subview in self.view.subviews){
    [subview removeFromSuperview];
    }
    }

    To remove particular subview
    [subview removeFromSuperview];

    Swift 3.0:
    To remove all subViews

    //MARK: Removing all subviews
    func clearAction() {
    self.view.subviews.forEach { subview in
    subview.removeFromSuperview()
    }
    }

    or
    //MARK: Removing all subviews
    func clearAction() {
    for subView in self.view.subviews {
    subView.removeFromSuperview()
    }
    }

    To remove particular subview
    subView.removeFromSuperview()

    Parse JSON data from local file — July 4, 2017

    Parse JSON data from local file

  • JSON – JavaScript Object Notation.
  • JSON is a text-based, lightweight, human-readable, easy way for storing and exchanging data.
  • Objective c:


    # pragma mark - JSON Serial from local file
    -(void) jsonSerial{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSArray *jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    for (id jsonObject in jsonData){
    NSLog(@"\nName-->%@", [jsonObject valueForKey:@"key"]);
    }
    }

    Swift 2.0:


    // MARK: JSON Serial from local file
    func jsonSerial(){
    do{
    let filePath = NSBundle.mainBundle().pathForResource("fileName", ofType: "json")
    let data = NSData(contentsOfFile: filePath!)
    let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! [[String : String]]
    for item in json{
    print(item["key"]!)
    }
    } catch let error {
    print("json error: \(error)")
    }
    }

    Swift 3.0:


    // MARK: JSON Serial from local file
    func jsonSerial() {
    let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: "fileName", ofType: "json")!))
    do {
    let parsedObject = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)
    for item in parsedObject as! [[String : String]] { //[String : String] type of json content
    print(item["key"]!)
    }
    }catch{
    print("not able to parse")
    }
    }