I'd like to authenticate an iOS device to use AppSync/S3 services via Cognito user pools. The AWSMobileClient provides some nice conveniences but the initialization requires that you're bundle have an awsconfiguration.json file -- which our application will define dynamically. Is there a way to configure that manually?
The current solution is to use the multi-environment workflow from the CLI.
https://aws-amplify.github.io/docs/cli/multienv?sdk=ios
Edit
If the multi-environment workflow from the Amplify team doesn't work for you, what you can do is create debug and prod versions of your config, and then create a build phase that copies the correct one based on your build settings (debug vs release, etc). This is working extremely well for one of my projects.
#export; #Prints list of all xcode variables with values
printf "$CONFIGURATION\n";
if [ "$CONFIGURATION" = "Debug" ]; then
printf "creating debug configuration";
cp -r "$PROJECT_DIR/awsconfiguration-debug.json" "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/awsconfiguration.json"
else
printf "creating production configuration";
cp -r "$PROJECT_DIR/awsconfiguration-prod.json" "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/awsconfiguration.json"
fi
As of AWS iOS SDK 2.11.0 (9th September 2019) it is now possible to configure without an awsconfiguration.json file.
It's even documented in the amplify documentation here
See also my answer to a related question
Here's a specific solution:
extension AWSMobileClient {
convenience init?(configuration url: URL) {
guard let data = try? Data(contentsOf: url) else { return nil }
guard let dict = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] else { return nil }
self.init(configuration: dict)
}
convenience init?(configuration name: String) {
guard let url = Bundle.main.url(forResource: name, withExtension: "json") else {
return nil
}
print("INITIALIZING AWSMobileClient [\(name)]")
self.init(configuration: url)
}
}
To use it, you can have as many different awsconfiguration-XXX.json files as you need, and at runtime you initialize with the one you want by:
let mobileClient = AWSMobileClient(configuration: "awsconfiguration-XXX")
mobileClient.initialize { (userState, error) in ... }
Related
I'm working on an iOS application that uses a duktape javascript based micro service and will deploy it using edgeSDK's iOS wrapper. In order to do that I am asked to specify an imagePath and apiRootUrl values. What are they and how do I set them?
This is my deployment function:
func deployMicroService() -> Void {
let deployConfig = MMKMicroserviceDeploymentConfig.init(name: "micro-service", apiRootUrl: "???", imagePath: "???", envVariables: [:])
let edgeOpsWrapper = edgeSDK_iOS_app_ops()
edgeOpsWrapper.deployMicroservice(edgeAccessToken: "MY-DEPLOYMENT-TOKEN", config: deployConfig) { (result) in
if let checkedError = result.error {
print("micro service deployment ended with error: \(checkedError.localizedDescription)")
}
else {
print("micro service deployment result: \(result.status.debugDescription)")
}
}
}
The answers are:
apiRootUrl is an arbitrary value that your micro service will be deployed under. For example you can use something like:
"/micro-service/v1"
and this would result for your micro service to be accessible at the following location:
http://localhost:8087/client-id-value/micro-service/v1
imagePath is a file system path to the micro service image tar file. Usually located in the application's bundle. For example you can use something like:
class func microServiceBundleStoragePath(serviceName: String) -> String? {
let microServiceFileName = "\(serviceName)-v1"
let microServiceBundlePath = Bundle.main.path(forResource: microServiceFileName, ofType: ".tar")
return microServiceBundlePath
}
resulting in a file system path of:
"/private/var/containers/Bundle/Application/DF2F6617-9153-4AEC-999D-BC7699A80FA0/example.app/micro-service-v1.tar"
I am trying to build an app where I am able to access(read/write) windows/mac shared folders in my local network with swift.
Is there any possible way to do that with swift?
There is an App in the App Store called "FileExplorer" https://apps.apple.com/de/app/fe-file-explorer-file-manager/id510282524 where you can access these shared folders, but I do not know how they programmed this and with which language.
I also tried to access my shared folders via this App and yes it worked I can see my shared folders on my Phone.
But there needs to be a way to do it with swift...
I already tried different things(code bellow).
In the code bellow I tried to access the shared folder of my second mac and write the Text "Write this text to the fileURL as text in iOS using Swift" into the file named "Test.txt" and after that I want to read the same file again.
#IBAction func Button(_ sender: UIButton)
{
var uc = URLComponents()
uc.scheme = "smb"
uc.user = "user"
uc.password = "password"
uc.host = "ip-adress"
uc.path = "document-directory"
// Save data to file
let fileName = "Test"
let url = uc.url
//let DocumentDirURL = URL(fileURLWithPath: "/Users/f/d/t/App/Assets/Apps/TestApp")
let DocumentDirURL = try! URL(resolvingAliasFileAt: url!)
let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
print("FilePath: \(fileURL.path)")
let writeString = "Write this text to the fileURL as text in iOS using Swift"
do {
// Write to the file
try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
var fullString: String = "" // Used to store the file contents
do {
// Read the file contents
fullString = try String(contentsOf: fileURL, encoding: .utf8)
} catch let error as NSError {
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
print("File Text: \(readString)")
}
If I run the code as shown, he always gives me the error
"smb scheme is not supported" and then some additional errors that he can not write/read the file because he can not access it.
When I change the code and only search on the device I am programming on and run the simulator to search for this file everything works fine. So I have problems with "smb".
Thank you for every helpful answer.
you can use amsmb2 library to do this
you can extend the template class provided to connect to download files, write files, list directories -> on an smb share
everything is asynchronous from memory, with the librarys calls including hooks for progress updates on the ui main thread etc
i believe the amsmb2 library function your after might be uploadItem
iOS 13 includes SMB (server message block protocol) support
https://9to5mac.com/2019/06/17/ios-13-beta-2-enables-smb-server-connectivity-in-the-files-app/
I am new to Realm and I want to ship a pre bundled Realm file my app, however the realm documentation is unclear to me on how to actually create a .realm file and also upload the desired pre bundled data to it. I have not been able to find any solution via SO, Google, or Youtube. Step-by-step instructions would be very helpful.
We're still looking at ways to officially make generating pre-populated Realms more easy. It's definitely a desired feature for the Realm Browser, but due to the way that Realm files require a migration when changing the schema, it's somewhat tricky to incorporate into a UI. It's definitely something we want to improve down the line.
At the moment, the Browser has a converter in it that can perform minimal data import like CSV files.
The Realm documentation is saying that the easiest way for you to produce a pre-populated Realm specific for your apps needs is to build a separate macOS app whose sole role is to generate the Realm file, pre-populate the data and then expose the resulting Realm file so you can copy it to your app.
The operation to do this is just like any normal Realm interaction (try! Realm() is what causes the file to actually be initially created), except you manually interact with the Realm file on disk upon completion.
I'm working on an app for an iOS conference, and the schedule data for the event is going to be stored in a Realm that is pre-bundled with the app when it ships.
Since I thought creating an extra macOS app would be overkill for an iOS app, I instead used iOS unit tests that would generate the pre-bundled Realm from scratch every time it was executed.
class Tests: XCTestCase {
func testGenerateNewDefaultRealm() {
let sources = [conferences, sponsors, conferenceDays] as [Any]
XCTAssert(generateDefaultRealm(named: "MyConferenceRealm.realm", sources: sources))
}
}
extension Tests {
public func generateDefaultRealm(named name: String, sources: [Any]) -> Bool {
// Create and configure the Realm file we'll be writing to
let realm = generateRealm(named: name)
// Open a Realm write transaction
realm.beginWrite()
// Loop through each source and add it to Realm
for source in sources {
if let objectArray = source as? [Object] {
for object in objectArray {
realm.add(object)
}
}
else if let objectDictionary = source as? [String : Object] {
for (_, object) in objectDictionary {
realm.add(object)
}
}
}
// Commit the write transaction
do {
try realm.commitWrite()
}
catch let error {
print(error.localizedDescription)
return false
}
// Print the file location of the generated Realm
print("=====================================================================")
print(" ")
print("Successfully generated at")
print(realm.configuration.fileURL!.path)
print(" ")
print("=====================================================================")
return true
}
public func generateRealm(named name: String) -> Realm {
let exportPath = NSTemporaryDirectory()
let realmPath = exportPath.appending(name)
// Delete previous Realm file
if FileManager.default.fileExists(atPath: realmPath) {
try! FileManager.default.removeItem(atPath: realmPath)
}
// Create new Realm file at path
let objectTypes: [Object.Type] = [Conference.self, ConferenceDay.self, SessionBlock.self, Event.self, Presentation.self,
Session.self, Location.self, Speaker.self, Sponsor.self, Venue.self]
let configuration = Realm.Configuration(fileURL: URL(string: realmPath), objectTypes: objectTypes)
let realm = try! Realm(configuration: configuration)
return realm
}
}
Running this unit test will generate a new Realm file in the NSTemporaryDirectory() directory of the Mac, and then feed in a set of Array and Dictionary objects of un-persisted Realm Object instances that are created each time the test is run. The location of the final Realm is then printed in the console so I can grab it and move it to the app bundle.
I would like to backup a realm database file to an iCloud drive, like WhatsApp, I have some questions:
What is the best practice to do this?
I have a database located in a shared group folder to access it from extensions, how can I back it up? How can I show the progress bar of upload? Like WhatsApp for example?
If I put a realm file in a document folder it will be synced for each modify.
Are there some samples code that we can see?
Thanks for the help, have any ideas? links?
Just to clarify, this is a question about backing up a discrete Realm file itself to iCloud Drive, so that it would be visible in the iCloud Drive app. Not synchronizing the contents of the file to a CloudKit store.
If you leave the Realm file in the Documents directory, then if the user performs an iCloud or iTunes backup, the file will be backed up. All this means though is that if the user decides to upgrade to a new device and perform a restore using the old device's backup image, the Realm file will be restored then. If the user deletes the app from your old device before then, the iCloud backup will also be deleted.
If you want to export your Realm file so it can be permanently saved and accessed in iCloud Drive, you can export a copy of the Realm file to your app's iCloud ubiquity container. This is basically just another folder like the shared group's folder, but it's managed by iCloud. This folder sort of behaves like Dropbox in that anything you put in there is automatically synchronized.
The code would look something like this:
let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)
let realmArchiveURL = containerURL.appendPathComponent("MyArchivedRealm.realm")
let realm = try! Realm()
try! realm.writeCopy(toFile: realmArchiveURL)
This is a really basic example. The Apple documentation recommends you do this on a background thread since setting up the iCloud folder for the first time can create some time.
Updating this wouldn't happen automatically. You'll need to export a new copy of the Realm each time the user wants to perform a backup.
I have recently had the same requirements and I am able to achieve from below steps
Swift: 4+
Step:1
1.Setup Your cloudKit for your app with a Developer account
2. You can take reference: https://www.raywenderlich.com/1000-cloudkit-tutorial-getting-started
Step 2
- Add CloudKit Capabilities in your App
- Please check out the screenshot: https://prnt.sc/pdpda5
Step 3
- Check for cloud Enabled options for your iphone
// Return true if iCloud is enabled
func isCloudEnabled() -> Bool {
if DocumentsDirectory.iCloudDocumentsURL != nil { return true }
else { return false }
}
Step 4
- Setup the below variables for Local or iCloud Document directories
struct DocumentsDirectory {
static let localDocumentsURL = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: .userDomainMask).last!
static let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
}
Step:5
Below function is used for copyRealmFileToIcloudContainer
func uploadDatabaseToCloudDrive()
{
if(isCloudEnabled() == false)
{
self.iCloudSetupNotAvailable()
return
}
let fileManager = FileManager.default
self.checkForExistingDir()
let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents", isDirectory: true)
let iCloudDocumentToCheckURL = iCloudDocumentsURL?.appendingPathComponent("\(memberId)_default.realm", isDirectory: false)
let realmArchiveURL = iCloudDocumentToCheckURL//containerURL?.appendingPathComponent("MyArchivedRealm.realm")
if(fileManager.fileExists(atPath: realmArchiveURL?.path ?? ""))
{
do
{
try fileManager.removeItem(at: realmArchiveURL!)
print("REPLACE")
let realm = try! Realm()
try! realm.writeCopy(toFile: realmArchiveURL!)
}catch
{
print("ERR")
}
}
else
{
print("Need to store ")
let realm = try! Realm()
try! realm.writeCopy(toFile: realmArchiveURL!)
}
}
Step:6
- Once your realm file uploaded on the server , you can check this in your iPhone
- Steps
- 1.Go To Setting
- 2.Go To iCloud
- 3.Go To ManageStorage
- 4.You will see your application there
- 5.Tap on Application, you will able to see your realm file over there
Step:7
- Make Sure you have added the below lines in info.plist
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.example.app</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>iCloudDemoApp</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
#yonlau as per your request sharing answer for backup realm file , This is tested once and the realm data only have when they backup on iCloud.
func DownloadDatabaseFromICloud()
{
let fileManager = FileManager.default
// Browse your icloud container to find the file you want
if let icloudFolderURL = DocumentsDirectory.iCloudDocumentsURL,
let urls = try? fileManager.contentsOfDirectory(at: icloudFolderURL, includingPropertiesForKeys: nil, options: []) {
// Here select the file url you are interested in (for the exemple we take the first)
if let myURL = urls.first {
// We have our url
var lastPathComponent = myURL.lastPathComponent
if lastPathComponent.contains(".icloud") {
// Delete the "." which is at the beginning of the file name
lastPathComponent.removeFirst()
let folderPath = myURL.deletingLastPathComponent().path
let downloadedFilePath = folderPath + "/" + lastPathComponent.replacingOccurrences(of: ".icloud", with: "")
var isDownloaded = false
while !isDownloaded {
if fileManager.fileExists(atPath: downloadedFilePath) {
isDownloaded = true
print("REALM FILE SUCCESSFULLY DOWNLOADED")
self.copyFileToLocal()
}
else
{
// This simple code launch the download
do {
try fileManager.startDownloadingUbiquitousItem(at: myURL )
} catch {
print("Unexpected error: \(error).")
}
}
}
// Do what you want with your downloaded file at path contains in variable "downloadedFilePath"
}
}
}
}
2.Copy realm file from iCloud to Document directory
func copyFileToLocal() {
if isCloudEnabled() {
deleteFilesInDirectory(url: DocumentsDirectory.localDocumentsURL)
let fileManager = FileManager.default
let enumerator = fileManager.enumerator(atPath: DocumentsDirectory.iCloudDocumentsURL!.path)
while let file = enumerator?.nextObject() as? String {
do {
try fileManager.copyItem(at: DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(file), to: DocumentsDirectory.localDocumentsURL.appendingPathComponent(file))
print("Moved to local dir")
//HERE ACCESSING DATA AVAILABLE IN REALM GET FROM ICLOUD
let realm = RealmManager()
let array = realm.FetchObjects(type: Mood.self)
print(array?.count)
} catch let error as NSError {
print("Failed to move file to local dir : \(error)")
}
}
}
}
You could take a look at this Github project by mikemac8888.
Basically you make your model objects conform to RealmCloudObject:
class Note: Object, RealmCloudObject {
...
}
You have to implement a mapping function :
func toRecord() -> CKRecord {
...
record["text"] = self.text
record["dateModified"] = self.dateModified
}
... and the reverse function used to create Realm records out of CloudKit records:
public func changeLocalRecord(...) throws {
...
realm.create(objectClass as! Object.Type,
value: ["id": id,
"text": text,
"dateModified": NSDate(),
"ckSystemFields": recordToLocalData(record)],
update: true)
...
}
The full documentation could be read at the link I provided, obviously.
When distribution app on app store should I also set Build Configuration in Xcode from edit scheme like this
I would recommend to set up two schemas.
First one: Development -> setup with Debug build configuration.
You can use this while you are developing your app. This will give you logging, easy debugging, etc..
Second one: Distribution -> setup with Release build configuration.
Logging will not happen on this schema, also debugging will be unavailable, because the build is not optimizaed for that.
When you are preparing your submittal to the App Store, archive the Distribution schema using the Release build configuration.
You can find some more detailed description here about the difference between Debug and Release build configurations.
This will cover your question in depth. I have used build configuration environment so that if you make a build in release Configuration. your automatic values will be se according to release version. you can also download the sample code from the link below to see actually what happens when you change the scheme.
First step.
Add the variable "Configuration" in your info.plist and add value "$(CONFIGURATION)" there.
Make a Config.swift file and copy paste the below code there.
`
import Foundation
private let configManagerSharedInstance = ConfigManager()
class Config {
class var sharedInstance: ConfigManager {
return configManagerSharedInstance
}
}
// You can put as much Environment as you need but you make sure you also put these environment in the config.plist file.
enum Environment: String {
case Debug = "Debug"
case Production = "Release"
}
class ConfigManager: NSObject {
private var environment: Environment?
var config: NSDictionary?
override init() {
super.init()
// Retrieve the current evironment from the main bundle
if let currentEnvironment = Bundle.main.infoDictionary?["Configuration"] as? String {
// Store the current environment for later use
environment = Environment(rawValue: currentEnvironment)
if let projectConfigPath = Bundle.main.path(forResource: "Config", ofType: "plist") {
if let projectConfigContents = NSDictionary(contentsOfFile: projectConfigPath) as? Dictionary<String, AnyObject> {
config = projectConfigContents[currentEnvironment] as? Dictionary<String, AnyObject> as NSDictionary?
}
} else {
print("config file not found")
}
}
}
func getCurrentEnvironment() -> Environment? {
return self.environment
}
func configForKey(key: String) -> String {
return config?[key] as! String
}
//It will use to get sub dictionary and their values.
func configForCategory(category: String, andKey: String) -> String {
let configuration = config?.value(forKeyPath: category) as! NSDictionary
return configuration.value(forKeyPath: andKey) as! String
}
}
`
I have also made a file Constants.swift in which i have set the varibles using the above code.
`
//
// Constants.swift
// BuildConfiguration
//
// Created by Ourangzaib khan on 4/6/17.
// Copyright © 2017 Ourangzaib khan. All rights reserved.
//
let kBASE_URL : String = {
print(Config.sharedInstance.configForKey(key: "kBASE_URL"));
return Config.sharedInstance.configForKey(key: "kBASE_URL")
}()
let STRIPEKEY : String = {
return Config.sharedInstance.configForCategory(category: "Stripe", andKey: "Publishable Key")
}()
let PUBNUBKEYSUBSCRIBE : String = {
return Config.sharedInstance.configForCategory(category: "PubNub", andKey: "Publish Key")
}()
let PUBNUBKEYPUBLISH : String = {
return Config.sharedInstance.configForCategory(category: "PubNub", andKey: "Subscribe Key")
}()
let WOWZAKEY : String = {
return Config.sharedInstance.configForKey(key: "Wowza");
}()
`
Now you just have to select the environment using edit sceme go into the edit scheme and chose Build Configuration Now when you will run the project you will see this output WRT build configuration in below images.
https://github.com/ourangzeb/Build-Configuration-for-IOS