How to ignore App Transport Security in iOS

Issue #221 Ignore a host <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>example.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSExceptionRequiresForwardSecrecy</key> <true/> <key>NSExceptionMinimumTLSVersion</key> <string>TLSv1.2</string> <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> <false/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <true/> <key>NSThirdPartyExceptionMinimumTLSVersion</key> <string>TLSv1.2</string> <key>NSRequiresCertificateTransparency</key> <false/> </dict> </dict> </dict> Ignore all <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>

April 30, 2019 · 1 min · 37 words · Khoa

How to use Stripe and Apple Pay in iOS

Issue #219 iOS Integration Setting Up Apple Pay Requirements Apple Pay Show basic add card in iOS import UIKit import Stripe final class MainController: UIViewController { func showPayment() { let addCardViewController = STPAddCardViewController() addCardViewController.delegate = self let navigationController = UINavigationController(rootViewController: addCardViewController) present(navigationController, animated: true, completion: nil) } } extension MainController: STPAddCardViewControllerDelegate { func addCardViewControllerDidCancel(_ addCardViewController: STPAddCardViewController) { dismiss(animated: true, completion: nil) } func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: @escaping STPErrorBlock) { _ = token....

April 30, 2019 · 9 min · 1870 words · Khoa

Understanding AVFoundation and MediaPlayer frameworks in iOS

Issue #210 Depending on what features we want to achieve, we need to go with either AVFoundation or MediaPlayer framework. As someone who worked with many apps that involve media playback, here are some of my observations MPMoviePlayerController vs AVPlayer At first, I use MPMoviePlayerController because it is very simple, in fact, it is a wrapper around AVPlayer. It offers a lot of useful notifications and properties. But when my requirements change, a lot more features are needed, I need to change to AVPlayer to have more control....

April 18, 2019 · 4 min · 675 words · Khoa

How to handle reachability in iOS

Issue #209 Here are what I learn about reachability handling in iOS, aka checking for internet connection. Hope you will find it useful, too. This post starts with techniques from Objective age, but many of the concepts still hold true The naive way Some API you already know in UIKit can be used for checking internet connection. Most of them are synchronous code, so you ’d better call them in a background thread...

April 17, 2019 · 5 min · 880 words · Khoa

How to cancel DispatchWorkItem and NSOperation

Issue #194 DispatchWorkItem https://stackoverflow.com/questions/48844169/swift-ios-dispatchworkitem-is-still-running-even-though-its-getting-cancelled https://stackoverflow.com/questions/29492707/how-to-stop-cancel-suspend-resume-tasks-on-gcd-queue NSOperation https://developer.apple.com/documentation/foundation/nsoperation/1411672-cancel?language=objc Grand Central Dispatch vs NSOperation https://stackoverflow.com/questions/43226434/2017-swift-3-1-gcd-vs-nsoperation https://medium.com/@johnsundell/a-deep-dive-into-grand-central-dispatch-in-swift-dead7f6e1ca7

April 3, 2019 · 1 min · 14 words · Khoa

How to zoom in double in MapKit

Issue #183 func zoomInDouble(coordinate: CLLocationCoordinate2D) { let region = mapView.region let zoomInRegion = MKCoordinateRegion( center: coordinate, span: MKCoordinateSpan( latitudeDelta: region.span.latitudeDelta * 0.5, longitudeDelta: region.span.longitudeDelta * 0.5 ) ) mapView.setRegion(zoomInRegion, animated: true) }

March 22, 2019 · 1 min · 32 words · Khoa

How to select cluster annotation in MapKit

Issue #182 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { guard let coordinate = view.annotation?.coordinate else { return } if (view.annotation is MKClusterAnnotation) { zoomInDouble(coordinate: coordinate) } }

March 22, 2019 · 1 min · 28 words · Khoa

How to cluster annotations in MapKit in iOS 11

Issue #181 https://developer.apple.com/documentation/mapkit/mkannotationview/decluttering_a_map_with_mapkit_annotation_clustering final class AnnotationView: MKMarkerAnnotationView { override init(annotation: MKAnnotation?, reuseIdentifier: String?) { super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) clusteringIdentifier = String(describing: ClusterView.self) } required init?(coder aDecoder: NSCoder) { fatalError() } } final class ClusterView: MKAnnotationView { override init(annotation: MKAnnotation?, reuseIdentifier: String?) { super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) displayPriority = .defaultHigh } required init?(coder aDecoder: NSCoder) { fatalError() } override func prepareForDisplay() { super.prepareForDisplay() guard let annotation = annotation as? MKClusterAnnotation else { return } let count = annotation....

March 22, 2019 · 1 min · 169 words · Khoa

How to get properties of JSValue in JavascriptCore

Issue #179 let rough = context.objectForKeyedSubscript("myObject") myObject.toDictionary()

March 19, 2019 · 1 min · 7 words · Khoa

Using CircleCI 2.0

Issue #158 We ’ve been using CircleCI for many of our open source projects. Since the end of last year 2017, version 2.0 began to come out, and we think it’s good time to try it now together with Swift 4.1 and Xcode 9.3 The problem with version 2.0 is it’s so powerful and has lots of cool new features like jobs and workflows, but that requires going to documentation for how to migrate configuration file, especially Search and Replace Deprecated 2....

April 3, 2018 · 3 min · 454 words · Khoa

UITableViewCell and Model

Issue #154 The most common UI element in iOS is UITableView, and the most common task is to display the UITableViewCell using the model. Although the title specifies UITableViewCell, but the problem involves other views (UICollectionView, custom view, …) as well There are many debates about this, so here I want to make a summary, all in my opinion It started with UITableViewCell Is Not a Controller This article follows strict MVC and states we should not pass model object to cell and let cell manipulate directly on the model....

February 27, 2018 · 3 min · 571 words · Khoa

Learning from Open Source Generic Factory

Issue #148 From https://github.com/devxoul/Pure/blob/master/Sources/Pure/FactoryModule.swift public protocol FactoryModule: Module { /// A factory for `Self`. associatedtype Factory = Pure.Factory<Self> /// Creates an instance of a module with a dependency and a payload. init(dependency: Dependency, payload: Payload) } From https://github.com/devxoul/Pure/blob/master/Sources/Pure/Factory.swift open class Factory<Module: FactoryModule> { private let dependencyClosure: () -> Module.Dependency /// A static dependency of a module. open var dependency: Module.Dependency { return self.dependencyClosure() } /// Creates an instance of `Factory`. /// /// - parameter dependency: A static dependency which should be resolved in a composition root....

January 26, 2018 · 1 min · 163 words · Khoa

How to use standalone UINavigationBar in iOS

Issue #144 There are times we want the same UIViewController to look good when it’s presented modally or pushed from UINavigationController stack. Take a look at BarcodeScanner and the PR https://github.com/hyperoslo/BarcodeScanner/pull/82 When it is presented, we need a header view so that we can show a title and a close button. We can create a custom HeaderView that inherits from UIView or either embed it in a UINavigationController before presenting....

January 10, 2018 · 3 min · 449 words · Khoa

How to deal with animation in UITests in iOS

Issue #143 Today I was writing tests and get this error related to app idle t = 23.06s Assertion Failure: <unknown>:0: Failed to scroll to visible (by AX action) Button, 0x6000003827d0, traits: 8858370049, label: 'cart', error: Error -25204 performing AXAction 2003 on element <XCAccessibilityElement: 0x7fc391a2bd60> pid: 91461, elementOrHash.elementID: 140658975676048.128 It turns out that the project uses a HUD that is performing some progress animation. Even it was being called HUD.hide(), the problem still exists....

January 8, 2018 · 1 min · 161 words · Khoa

How to use R.swift in UITest in iOS

Issue #138 Here is how to use R.swift in UITest target Add Localizable.strings to UITest target Declare pod target 'MyAppUITests' do pod 'R.swift', '~> 4.0' end In UITest target settings, add $(FRAMEWORK_SEARCH_PATHS) to Runpath Search Path Add R.generated.swift to UITest target

January 5, 2018 · 1 min · 41 words · Khoa

Hiding back button in navigation bar in iOS

Issue #137 Use a custom NavigationController import UIKit class NavigationController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() navigationBar.tintColor = .white navigationBar.barStyle = .black navigationBar.isTranslucent = false } override func pushViewController(_ viewController: UIViewController, animated: Bool) { viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) super.pushViewController(viewController, animated: animated) } }

January 4, 2018 · 1 min · 50 words · Khoa

Using dlopen in iOS

Issue #133 With dlopen we can make uses of some private frameworks. It will be fun From iPhone X home button #import <dlfcn.h> // somewhere in viewDidLoad dlopen([binaryPath cStringUsingEncoding:NSUTF8StringEncoding], RTLD_NOW); UIView *const view = [[NSClassFromString(@"SBHomeGrabberView") alloc] init]; [view sizeToFit]; [self.view addSubview:view];

January 2, 2018 · 1 min · 41 words · Khoa

How to prevent UIVisualEffectView crash

Issue #124 We all know that there’s a potential crash with UIVisualEffectView on iOS 11. The fix is to not add sub views directly to UIVisualEffectView, but to its contentView. So we should change effectView.addSubview(button) to effectView.contentView.addubView(button) Here we don’t need to perform iOS version check, because effectView.contentView works for any iOS versions. Potential cases for crashes Here are some cases you can potentially cause the crashes Strange namings Normally we name our UIVisualEffectView as blurView, effectView....

December 20, 2017 · 3 min · 439 words · Khoa

Collection Update

Issue #119 This is about collection update, how to provide correct IndexPath and a simple diffing algorithm Excerpt from my talk at CocoaHeads, Oslo https://www.meetup.com/CocoaHeads-Oslo/events/244892347/ Slide https://speakerdeck.com/onmyway133/collection-update CollectionView It’s hard to imagine of any apps that don’t use Table View or CollectionView. And by CollectionView, I actually mean UICollectionView 😉 . Most of the time, we show something with response from backend, then potentially update and insert new items as data changed....

December 7, 2017 · 8 min · 1666 words · Khoa

How to learn iOS

Issue #116 Objcio Swift Talk Hacking With Swift AppCoda Swift by Sundell Swift with Majid Nil Coalescing

December 7, 2017 · 1 min · 17 words · Khoa