Skip to content

Instantly share code, notes, and snippets.

View jazzedge's full-sized avatar

Rocco Labellarte jazzedge

View GitHub Profile
class LaunchViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
Thread.sleep(forTimeInterval: 0.5) // Pause for half a second
instantiateHomeViewContoller() // Instantiate HomeViewController
See: https://stackoverflow.com/questions/26943808/ios-how-to-run-a-function-after-device-has-rotated-swift
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
print("Landscape")
} else {
print("Portrait")
}
//Code
See: https://freakycoder.com/ios-notes-31-how-to-hide-keyboard-by-touching-anywhere-cdf4f0c5151c
01. Create an extension
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
See: https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar
If you build a stack of view controllers with a navigation controller, but without embedding a VC in the NC then you have to add the navigation bar and buttons programmatically to every VC.
01. Example with a title, left and right button
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title = "Profile Settings"
See:https://makeapppie.com/2016/07/11/programmatic-navigation-view-controllers-in-swift-3-0/
Go to ViewController class and add the following method:
@IBAction func fourFiveToggleButton(_ sender: UIButton){
performSegue(withIdentifier: "mySegueIdentifier", sender: self)
}
To unwind see: https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/
In plist add
Application does not run in background - > YES
Now your app will reopen each time from the rootViewController, not where it left off.
See: https://chariotsolutions.com/blog/post/importing-data-via-custom-file-types-in/
01. Define the File Type
First we need to define a file type extension that our application will support. This is also known as a Uniform Type Identifier (UTI). For our purpose, we will use a filetype of 'csm' (for Chariot Solutions Mobile). The contents of this file will be some string. It could really be almost anything, but for simplicity we will use a string.
For a list of Apple defined UTIs, look here: http://developer.apple.com/library/ios/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1.
Once we know what our custom file type extension is, we can add support for it to the project by modifying the Info.plist file for the project. We are going to be the owner/exporter of the new file type, so we add the "Exported Type UTIs" key to the Info.plist file.
See:https://github.com/maximbilan/ios_airdrop_custom_data
var excludedActivities: [Any] = [.postToTwitter, .postToFacebook, .postToWeibo, .message, .mail, .print, .copyToPasteboard, .assignToContact, .saveToCameraRoll, .addToReadingList, .postToFlickr, .postToVimeo, .postToTencentWeibo] as? [Any]
See:https://stackoverflow.com/questions/39309338/how-to-send-and-receive-custom-data-with-airdrop
See:https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html
See:https://stackoverflow.com/questions/45154373/creating-custom-uti-for-use-with-airdrop-ios
See:https://stackoverflow.com/questions/43195134/how-do-i-import-and-read-a-file-in-my-ios-app
01. How to send and receive custom data with AirDrop
In my app I have an array of custom data that I want to send to another user with the same app, via AirDrop.
See:https://stackoverflow.com/questions/43823430/using-struct-to-pass-a-variable-swift
The right way to pass data would be to:
eliminate static variables;
give your struct a name that starts with uppercase letter;
create instance of your struct; and
pass this instance to the destination view controller in prepare(for:sender:).
01. In the first VC: