Using AttributedString in Swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let html = """

                    <html>
                    <body>
                    <p style="color: blue;
                              font-size: 20px;
                    ">
                    Drag the blue handles to choose the section of your clip that will speed up or slow down.</p>

                    <p style="display:block; line-height: 50px; background-color: #05ffb0;"><br></p>
                    <p style="display:block; line-height: 1px; background-color: #05ffb0;"><br></p>

                    <p style="color: blue;
                              font-size: 20px;
                    ">
                    You can also move the blue handles on the clip in the timeline.
                    </p>
                    </body>
                    </html>

                    """
        let data = Data(html.utf8)
        let rect = CGRect(x: 10, y: 100, width: (self.view.frame.size .width) - 100, height: 300)
        let label = UILabel(frame: rect)
        label.backgroundColor = UIColor.red
        label.numberOfLines = 0
        if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
            label.attributedText = attributedString
        }
        self.view .addSubview(label)
    }
}

Detecting interlaced videos using FFMpeg

/Users/darshan/Downloads/ffmpeg -filter:v idet -frames:v 100 -an -f rawvideo -y /dev/null -i /Users/darshan/Dropbox/Public/1080i/interlaced/Mug\ Drop_Interlaced_4ss.mp4

video:303750kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
[Parsed_idet_0 @ 0x7facaf200000] Repeated Fields: Neither: 63 Top: 20 Bottom: 18
[Parsed_idet_0 @ 0x7facaf200000] Single frame detection: TFF: 81 BFF: 0 Progressive: 20 Undetermined: 0
[Parsed_idet_0 @ 0x7facaf200000] Multi frame detection: TFF: 101 BFF: 0 Progressive: 0 Undetermined: 0

If you observe the output above, you will see that the progressive, repeated fields , single frame and multiple frame have values in them. This should give you an idea that its a interlaced video. Also most interlaced videos will have an ‘i’ in them. e.g. 1080i

Exporting an AVAsset in iOS

// First thing is you need a AVAsset.
// Once you have an asset, you need to determine the type of preset you need to use.
// You can consider a preset as a set of properties, which is provided to the exporter.
// I do this for helping me with my export.
-(NSString*)determineCompatibleExportPresetForAsset:(AVAsset*)asset
{
NSArray<NSString*>* presetsRankedByPriority = @[AVAssetExportPresetHighestQuality, AVAssetExportPresetMediumQuality, AVAssetExportPresetLowQuality];
NSArray* compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
for (NSString* preset in presetsRankedByPriority)
{
if([compatiblePresets containsObject:preset])
{
return preset;
}
}
return nil;
}
// after getting the preset, use this call
AVAssetExportSession* exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:preset];
exportSession.outputFileType = UTI;
exportSession.outputURL = whereToSaveTheFile;
if (!exportSession)
{
callback([NSError errorWithDomain:kManagerErrorDomain
code:kDownloadFailed
userInfo:nil]);
}
else
{
[exportSession exportAsynchronouslyWithCompletionHandler: ^{
// handle error here
}];
view raw gistfile1.txt hosted with ❤ by GitHub

Detecting exception crashes in xcode

Many times you might have observed your app crashing from xcode without any stack trace in the console. This might be due to exceptions.
The easiest way to catch this is through xcode itself.
a. go to your breakpoints/exceptions tab.
b. 'Add Exception Breakpoint' using the + at the bottom left corner.
c. run your code. when the exception hits, you will see the debugger stop.
d. to dig deeper, go to the crashed thread.
e. type 'register read'
f. type 'po $rax'
g. most times, you will see the details here.
view raw gistfile1.txt hosted with ❤ by GitHub

String comparison in Swift and Objective C


// Lets start by comparing the same string in Swift.
// Unicode defines U+00E9, Latin small letter e with acute, as a single value.
// But you can also write it as the plain letter e, followed by U+0301, combining acute accent.
// In both cases, what’s displayed is é, and a user probably has a reasonable expectation
// that two strings displayed as “résumé” would not only be equal to each other but also
// have a “length” of six characters, no matter which technique was used to produce the é in either one.
// They would be what the Unicode specification describes as canonically equivalent.
let single = "Pok\u{00E9}mon"
let double = "Poke\u{0301}mon"
if(single == double)
{
print("They are equal")
}
// Lets try the same thing in Objective-C
let nssingle = single as NSString
nssingle.length // → 7
let nsdouble = double as NSString
nsdouble.length // → 8
if(nssingle == nsdouble)
{
print("They are equal NS")
}
else
{
print("they are not equal NS") // This will be printed.
// The strings will not be printed, because of the == operator implementation in objective-c
// Both the strings will be compared literally. So they will fail to match.
}
// This is how we fix it. Use compares to check the values of the strings.
if(nssingle.compare(String(nsdouble)) == ComparisonResult.orderedSame)
{
print("They are equal")
}

view raw

gistfile1.swift

hosted with ❤ by GitHub

How to send a NSNotification in React Native to Javascript?

Updated code here: https://redflowerinc.com/how-to-send-a-nsnotification-in-react-native/

Send a notification from Objective C to React Native.
Follow the link below to see how you can send a notification from Javascript to Objective-C and then back again to JS front end.


//
// EventManager.h
//
// Created by md on 6/14/17.
//
//
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface EventManager : RCTEventEmitter <RCTBridgeModule>
@end

view raw

EventManager.h

hosted with ❤ by GitHub


//
// EventManager.m
//
// Created by md on 6/14/17.
//
//
#import "EventManager.h"
@interface EventManager ()
@end
@implementation EventManager
RCT_EXPORT_MODULE();
– (NSArray<NSString *> *)supportedEvents {
// this is the list of supported events.
return @[@"scrollToTopHomeFeed"];
}
– (void)startObserving {
for (NSString *event in [self supportedEvents]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotification:)
name:event
object:nil];
}
}
– (void)stopObserving {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// Use this method to handle notifications from the javascript side
– (void)handleNotification:(NSNotification *)notification {
if([notification.name isEqualToString:@"scrollToTopHomeFeed"])
{
[self sendEventWithName:notification.name body:@"scrollToTopHomeFeed"];
}
else
{
[self sendEventWithName:notification.name body:notification.userInfo];
}
}
@end

view raw

EventManager.m

hosted with ❤ by GitHub


// This is your javascript code
import { NativeModules, NativeEventEmitter } from 'react-native';
// In this method add a listener to your notifications.
componentDidMount() {
const { EventManager } = NativeModules;
this.eventEmitter = new NativeEventEmitter(EventManager);
this.eventEmitter.addListener('scrollToTopHomeFeed', (data) => this.scrollToTop(data))
}
scrollToTop(data) {
this.flatList.getScrollResponder().scrollTo({x:0, y:0, animated: true})
}

view raw

frontend.js

hosted with ❤ by GitHub


//
// NotificationManager.h
//
// Created by md on 6/14/17.
//
//
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface NotificationManager : NSObject <RCTBridgeModule>
@end


//
// NotificationManager.m
//
// Created by md on 6/14/17.
//
//
#import "NotificationManager.h"
#import "EventManager.h"
#import <React/RCTEventEmitter.h>
@implementation NotificationManager
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(postNotification:(NSString *)name) {
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:nil];
}
@end


import { NativeModules, NativeEventEmitter } from 'react-native';
var NotificationManager = require('react-native').NativeModules.NotificationManager;
// This is how you will post a notification to objective-c code.
NotificationManager.postNotification("scrollToTopHomeFeed")

All the new features in iOS 11

Drag and drop
In iOS 11 https://en.wikipedia.org/wiki/Apple_File_System
app store multiple days / user reviews
iOS 11 – 64bit only. 32 bit apps not supported anymore
High sierra last release to support 32bit release
Jan 2018 – 64bit only app store
Xcode 9 is much faster
Semantic highlight – useful for functions opening and closing braces
Brand new refactoring system (swift, c, c++, objective c)
Xcode 9 has swift 4
Swift – String easier to use, unicode correctness
Codable prototype
40% faster Swift/Objective C projects
Searching and indexing faster
Github integration (not sure if bitbucket is supported)
Undefined Behavior sanitizer
Main Thread API Checker
Xcode server Built-in
Xcodebuild multiple destinations at the same time e.g. iPhone 7, iPhone 5s
Simulator multiple sessions e.g. watch, iPhone booted at the same time.
Xcode 9, wireless development (USB, WIFI or ethernet)
Visual Debugging on ViewControllers
Indexing while building (background)
wireless with other apps, quicktime, console and instruments
Drag and drop
Dynamic Type
NSFileCoordinator
Password autofill
DynamicType
Simulator – New bezels (easier to swipe from side)
Files App (to access files)
Document browser
AV route picker
App strip (live content can be rendered)
Send API – app extensions
Camera detects QR codes
HEVC compression
HEIF for images
Depth map in images. Depth is stored in images.
AVCapture/AVDepthData
Vision APIs
Face and landmark detection
Text detection
Object detection
CoreML and image recognition
Machines learning modules will be given access [Caffee]
CoreML convertor tools
1.7 million apps using Metals
Metal 2 [GPU driven rendering / Convolution ML networks]
VR development on the mac
ARKit
Visual Inertial Odometry
Scene Understanding