Posts

Showing posts with the label swift

Vapor3 JSON encode & Decode

Image
Clash Royale CLAN TAG #URR8PPP Vapor3 JSON encode & Decode I'm having to use a FoundationDB database for a project. It stores data as a key:value pair stored as Bytes. I want to store a JSON object mapped to a struct I have. I want to be able save the data as an encoded JSON object and then be able to recreate the JSON object by reading the value Bytes from the DB. In my CreateRecord function I pass a JSON in a request and use it to create my Country object. I need to convert the type Data into Bytes to store it. So far I have come up with this. let data: Bytes = try JSONEncoder().encode(country).base64URLEncodedString() Then, when I read the record from the database I need to be able to reverse the process to create my Country object from the Bytes that store the JSON. let mycountry:Country = try decoder.decode(Country.self, from: Data(bytes: DBrecord.value) ) The struct is: struct struct Country: Content { var country_name: String var timezone: String var default...

Force the navigation bar to add space for the status bar

Image
Clash Royale CLAN TAG #URR8PPP Force the navigation bar to add space for the status bar I have a UIPageViewController subclass that allows the user to swipe upward from the initial view controller to a second view controller. The initial view controller does not have a navigation bar (it's not connected to a UINavigationController ), but the second one does. Currently, when I swipe up to present the second view controller, there is no space in the navigation bar for the status bar. When the transition is complete, the entire view controller layout updates instantly to add the status bar space. I want to make the second view controller anticipate that the status bar will be there and add the space for it so the view doesn't jolt after the transition. UIPageViewController UINavigationController Short video clip showing the jolt Similar, but unanswered question EDIT: After closely looking at the way the view controller behaves during the transition, I was able to get a good l...

How to encrypt/decrypt a data using Elliptic Curve Cryptography(ECC) using the Bouncy Castle Open Source Library for iOS Swift

Image
Clash Royale CLAN TAG #URR8PPP How to encrypt/decrypt a data using Elliptic Curve Cryptography(ECC) using the Bouncy Castle Open Source Library for iOS Swift How can I encrypt and decrypt a data using Elliptic Curve Cryptography(ECC) using the Bouncy Castle or Spongy Castle Open Source Library for iOS Swift. You can try cryptoSwift library for encryption/decryption. github.com/krzyzanowskim/CryptoSwift – Smita Dec 27 '17 at 10:28 CryptoSwift not supporting Elliptical Curve Encryptions – Ajithkumar M Dec 27 '17 at 10:48 Bouncycastle is for Java and c#, not for iOS – pedrofb ...

Image Encryption in Swift

Image
Clash Royale CLAN TAG #URR8PPP Image Encryption in Swift I am using IDZSwiftCommonCrypto for image encryption using StreamCryptor described as an example at its GitHub page: https://github.com/iosdevzone/IDZSwiftCommonCrypto IDZSwiftCommonCrypto I am not able to successfully decrypt. Here is my code for encryption and decryption (imageData comes from UIImageView ). The output is different from input after encryption (imageData is different from xx). UIImageView Encryption: func performImageEncryption(imageData: Data) -> Void { var inputStream = InputStream(data: imageData) let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c") var sc = StreamCryptor(operation:.encrypt, algorithm:.aes, options:.PKCS7Padding, key:key, iv:Array<UInt8>()) var inputBuffer = Array<UInt8>(repeating:0, count:1024) var outputBuffer = Array<UInt8>(repeating:0, count:1024) inputStream.open() var cryptedBytes = 0 ...

How to disable isEnable and isUserInteraction attributes but keep cursor show in UITextField?

Image
Clash Royale CLAN TAG #URR8PPP How to disable isEnable and isUserInteraction attributes but keep cursor show in UITextField? I have a custom framework for UITextField . For now, I want to disable two attribute as the question above but still keep the cursor show in my custom textfield. I already disable the keyboard with set the inputView = UIView() , and that successful for hide the keyboard even show the cursor inside textField. UITextField inputView = UIView() My goal is disable both attributes like above or another for can not Select All action from my textField, but still keep the cursor available anytime. Select All Hope to hear the ideas from you guys. Thank a lot! Put a dummy view above textview with clear color, and show and hide that view. – Bista 31 secs ago By ...

how to call a swift function from javascript

Image
Clash Royale CLAN TAG #URR8PPP how to call a swift function from javascript I am injecting a script in UIWebView using Swift. When a certain tap action happens, the script takes care of the handling. While this happens, the JavaScript is supposed to make a call to one of the functions in Swift, which saves the data sent from JavaScript in native variable. Please help me in making this call. UIWebView People won't be able to help without showing what you have done so far. Bits of code snippets would do. – Jay Mayu 27 mins ago 1 Answer 1 For this you need to make a callback from your JavaScript file, I used one JavaScript editor in that,This was the code :- window.location = "callback://0/"+ pass your value here And in your swift file, use t...

not able to understand that how to store value in userdefaults

Image
Clash Royale CLAN TAG #URR8PPP not able to understand that how to store value in userdefaults i am new to swift and i want to know that from my response i want to store user id in userdefaults please any one help data = { "access_token" = b332236bb2c9a2a5e35452bff62f76ac; "contact_no" = 847334868; email = "jhdsjhs@gmail.com"; "fk_user_id" = 125; "is_active" = 1; photo = "usr_jbdcm1533033074.png"; "user_name" = schsjcsacksjah; "user_role" = 2; "user_type" = 0; }; message = "Customer registered successfully!"; success = 1; } i want to store fk_user_id in user defaults please can anyone help Are you able to fetch fk_user_id from your response ? Saving it is just one search and one click on google. – Sharad Chauhan 9 mins ago ...

Swift Custom NavBar Back Button Image and Text

Image
Clash Royale CLAN TAG #URR8PPP Swift Custom NavBar Back Button Image and Text I need to customise the look of a back button in a Swift project. Here's what I have: Here's what I want: I've tried creating my own UIBarButtonItem but I can't figure out how to get the image to be beside the text, rather than as a background or a replacement for the text. let backButton = UIBarButtonItem(title: "Custom", style: .Plain, target: self, action: nil ) //backButton.image = UIImage(named: "imageName") //Replaces title backButton.setBackgroundImage(UIImage(named: "imageName"), forState: .Normal, barMetrics: .Default) // Stretches image navigationItem.setLeftBarButtonItem(backButton, animated: false) @Randy You mean make a UIView and add a Label and ImageView and set that? – Conor May 31 '16 at 18:25 ...

Class implemented in app target and test target

Image
Clash Royale CLAN TAG #URR8PPP Class implemented in app target and test target I'm using CoreData and have extended the automatic (didn't generate custom class files) model classes by custom variables that return objects of classes coming from an CocoaPods dependency. import Foundation import ACocoaPodsFramework import CoreData extension MyClass { var customVar: AClassFromCocoaPods? { return AClassFromCocoaPods() } } I've added a target for unit tests to the Xcode project and added MyClass to the test target. A test class looks like the following. import XCTest import ACocoaPodsFramework import CoreData @testable import MyAppTarget class MyClassTests: XCTestCase { } I have imported ACocoaPodsFramework in both the app and the unit test target as a framework. When i run the unit tests i receive the following notice objc[21178]: Class MyClass is implemented in both /.../MyAppTarget.app/MyAppTarget (0x1017703f0) and /... MyAppTarget.app/PlugIns/MyClassTests.xcte...

2 UiDatepickerView in one ViewController

Image
Clash Royale CLAN TAG #URR8PPP 2 UiDatepickerView in one ViewController I want to use 2 UiDatePickerView in 1 One View Controller. I used 2 textfields fromeDate to toDate but when i selected fromdate textfield it automatically selected other toDate. I want to know who can i use 2 or more Uidatepickerview in 1 view controller This is my code: @IBOutlet weak var fromDateTxtField: UITextField! @IBOutlet weak var toDateTxtField: UITextField! private var datePickerFrom: UIDatePicker? private var datepickerTo : UIDatePicker? override func viewDidLoad() { super.viewDidLoad() datePickerFrom = UIDatePicker() datepickerTo = UIDatePicker() datePickerFrom?.datePickerMode = .date datepickerTo?.datePickerMode = .date fromDateTxtField.inputView = datePickerFrom toDateTxtField.inputView = datepickerTo showDatePicker() } func showDatePicker(){ datePickerFrom?.datePickerMode = .date let toolBar = UIToolbar() toolBar.sizeToFit() let doneButton = UIBarButto...

How to return value from a Closure in Swift?

Image
Clash Royale CLAN TAG #URR8PPP How to return value from a Closure in Swift? So I am using fabric plugin/Twitter-kit to use twitter api in my application. I want to get the image URL of profile picture of a celebrity. here is my code below. func getImageURL(celebrity :String) -> String{ var imageURL = "" let client = TWTRAPIClient() let statusesShowEndpoint = userSearch let params = ["q": celebrity, "page" : "1", "count" : "1" ] var clientError : NSError? let request = client.URLRequestWithMethod("GET", URL: statusesShowEndpoint, parameters: params, error: &clientError) client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in if connectionError != nil { print("Error: (connectionError)") } do { let json = try NSJSONSerialization.JSONObjectWithDat...

Swift: NSAttributeString incorrect HTML result

Image
Clash Royale CLAN TAG #URR8PPP Swift: NSAttributeString incorrect HTML result i tried to read HTML using NSAttributeString. but it gets incorrect result when i put table in li <ol><li>first <table><tbody><tr> <td>hello</td> <td>: </td> <td>world</td> </tr></tbody></table></li><li>second</li></ol> and it becomes like this i have used the other extension but it still doesn't work. ex: Convert HTML to NSAttributedString in iOS is it bug from swift? and if it's a bug is there any solution? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

How do I pass indexPathForSelectedRow into a UITabView via a UITabBarController?

Image
Clash Royale CLAN TAG #URR8PPP How do I pass indexPathForSelectedRow into a UITabView via a UITabBarController? I'm trying to segue from a UITableView to a specific tab within a UITabBarController . While googling, I found two sets of info that seem to indicate how to do, but neither were using a UITableView as the source. UITableView UITabBarController UITableView The first source is this wonderfully written answer I found here on StackOverflow: How to make a segue to second item of tab bar? The second source was this site: http://www.codingexplorer.com/segue-uitableviewcell-taps-swift/ I have been attempting to combine the two for my app. Below is a truncated version of my originating UIViewController (I can post the full one if needed, just most the code isn't related to this segue, I don't think): class BonusListViewController: UITableViewController { // MARK: - Table View Configuration override func numberOfSections(in tableView: UITableView) -> Int { ...

confused about optional chaining and nil coalescing operator in Swift

Image
Clash Royale CLAN TAG #URR8PPP confused about optional chaining and nil coalescing operator in Swift I'm having difficulty understanding the exercise below: Assume the following structs and enum are defined. enum Genre: String { case country, blues, folk } struct Artist { let name: String var primaryGenre: Genre? } struct Song { let title: String let released: Int var artist: Artist? } Using the enums and structures, implement the getArtistGenre() function. Given a Song as input, return the raw value of the artist's primary genre. If either the artist is nil or the primaryGenre is nil, then return an empty string. You must use optional chaining and the nil coalescing operator (??). enum Genre: String { case country, blues, folk } struct Artist { let name: String var primaryGenre: Genre? } struct Song { let title: String let released: Int var artist: Artist? } func getArtistGenre(song: Song) -> String { } ...

Image does not fit properly in collectionCell

Image
Clash Royale CLAN TAG #URR8PPP Image does not fit properly in collectionCell In Swift 3. My app has a table view and a detail view with is a view controller. Here I added a collection view. An array of images must be presented. The images adapt themselves beautifully if in portrait I check the content mode in scaler to fill. In ladscape it works in aspectFill. Vice-versa, the layout does not fit. I did the following in cellForItem At: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = myCollection.dequeueReusableCell(withReuseIdentifier: "cellCollection", for: indexPath) as! CollectionViewCell cell.cellImage.image = UIImage(named: array[indexPath.item]) if UIDevice.current.orientation.isLandscape{ cell.cellImage.contentMode = .scaleAspectFill } else if UIDevice.current.orientation.isPortrait{ cell.cellImage.contentMode = .scaleToFill } return cell } I...