How do I save a simple NSString to Firebase in Obj C? - ios

I'm new to Obj-C and having a hard time following the instructions available from Firebase. How do I save a simple NSString to the Firebase database?
I'm trying to save a unique user identifier and then be able to tie a zip code to it. I've made it to this step in the instructions, but my code (below) won't save to Firebase: https://firebase.google.com/docs/database/ios/save-data
#import "ZipCodeVC.h"
#import <FirebaseStorage/FirebaseStorage.h>
#import <FirebaseDatabase/FirebaseDatabase.h>
#interface ZipCodeVC ()
#property FIRDatabaseReference *ref;
#end
#implementation ZipCodeVC
- (void)viewDidLoad {
[super viewDidLoad];
[self.ref setValue:#"Test" forKey:#"Testing 1"];
}
#end
On a similar note, does anyone know of a good Firebase guide for Objective-C apart from what's available from Firebase? Most of the guides I've found are for swift.

First of all, you are creating a variable to a Firebase reference, but never initializing it.
Step 1: Initialize your ref:
self.ref=[[FIRDatabase database]reference];
Step 2: Save to your desired path (Testing 1 in this case)
[[self.ref child:#"Testing 1"] setValue:#"Test"];
You also may need to add Firebase to your App Transport Security Settings (ever since Apple increased their security on HTTP links. To do this, go to your info.plist file, then add a new key called "App Transport Security Settings". Then, add a sub-key called "Allow Arbitrary Loads" and make it a boolean with value YES. Then add a dictionary sub-key named "Exception Domains" and add a string key under that with value: "https://console.firebase.google.com". Now, your app is allowed to load the firebase console. It should look something like this:
Last step:
Modify your read/write rules by going to Database->Rules in your Firebase console. Then, create 2 lines:
".read":true,
".write":true
This allows you to write data to the Firebase console from any client. You should now be all set up to save data to Firebase from iOS.

Related

Accessing the same persistent store from Swift and React Native

We're considering using React Native for one of our Screens - for fun, to check it and to get a better grasp of the possibilities, but even before w start I need some answers, so we're sure React Native and it's environments can meet our requirements.
In this question I'd like to specifically ask about persisting data and accessing it.
In our app we focus on offline experience, as it's one of the most crucial points for us. On iOS we have a couple of possibilities how to achieve this eg. Core Data, Realm... But the thing is if we decide to implement some part of our app as React Native we'd also need these parts to access stored data and even modify it and save to persistent store.
Can this be achieved? Having one persistent store (SQLite, Realm, something else?) and access it from both Swift code and React Native.
This is extremely easy with React Native in both iOS and Android with Native Modules.
You would build a simple native module in Swift/Obj-C and expose your persistent store:
//RCTMyDataStore.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#interface RCTMyDataStore : RCTEventEmitter <RCTBridgeModule>
#end
and
// RCTMyDataStore.m
#import "RCTMyDataStore.h"
#implementation RCTMyDataStore {
}
RCT_EXPORT_MODULE(MyDataStore);
RCT_REMAP_METHOD(getMyData,
getMyData_resolver:(RCTPromiseResolveBlock)resolve
getMyData_rejecter:(RCTPromiseRejectBlock)reject)
{
// get data from my persistent store
if (success) {
// convert to RN passable format (NSDictionary or NSArray)
resolve(myData);
} else {
reject(#"404", #"No data", nil);
}
}
#end
In your React Native code:
import {
NativeModules
} from 'react-native';
const NativeDataStore = NativeModules.MyDataStore; // must match the RCT_EXPORT_MODULE name
NativeDataStore.getMyData()
.then(data => {
// do some stuff
})
For more info on Native Modules and Swift support, you can check out the docs here: https://facebook.github.io/react-native/docs/native-modules-ios.html#exporting-swift

Cannot Invoke an Argument List type

I am trying to configure the iOS SDK from Zopim into my code. My iOS code is written in Swift and I've imported the framework of the SDK which is written in Obj C. I have created a bridging header and was able to import from the framework.
In order to be able to configure and connect with Zopim I need to include the following code in my Appdelegate.swift file.
Object C Code:
[ZDCChat configure:^(ZDCConfig *defaults) { defaults.accountKey =
#"you account key here"; }];
I have tried to convert the above code into Swift (code below) however I'm getting the following error - Cannot Invoke Configure with an argument list of type ZDCConfig
Swift code:
ZDCChat.configure({(defaults: ZDCConfig) in defaults.accountKey =
"you account key here"})
The ZDCConfig code include the following:
#interface ZDCConfig : ZDCSessionConfig
/** * The Zopim account key (must be set before a chat can be
started). */
The ZDCChat code include the following:
** * The core of the Chat SDK, all objects related to chat can be accessed * from here either directly or indirectly. */ #interface
ZDCChat : NSObject
/** * The chat session. */
Can anyone guide me as to what is causing the problem ?
I had to add a bridging header that contained these two lines-
#import <ZDCChat/ZDCChat.h>
#import <MobileCoreServices/MobileCoreServices.h>
and then I was able to make that call like this-
ZDCChat.configure {
defaults in
defaults.accountKey = ""
}
Hope this helps.

How to Implement native module in react-native?

I am following this guide
http://facebook.github.io/react-native/docs/nativemodulesios.html#content
And also this website:
http://colinramsay.co.uk/2015/03/27/react-native-simple-native-module.html
But does not matter where i add the .h and .m files i always get the error:
Class ClassName was not exported Did you forget to use RTC_EXPORT_MODULE()?
Even if it the same code from the examples from react-native documentation, can anyone guide me where to add the .h and .m files and properly link them to the project?
Thanks.
There has been a change in the native module API and it seems the docs haven't been updated accordingly. From the example in my article, SomeString.m should look like this:
// SomeString.m
#import "SomeString.h"
#implementation SomeString
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(get:(RCTResponseSenderBlock)callback)
{
// Change this depending on what you want to retrieve:
NSString* someString = #"something";
callback(#[someString]);
}
#end
This ends up with the desired result and you can call it from JS in the same way as before. It looks like this only just happened:
https://github.com/facebook/react-native/commit/0686b0147c8c8084e4a226b7ea04585362eccea8
You can also just add a plain RCT_EXPORT(); to any method you want to export. Works like a charm.

How to send emails in Swift using Mailgun

I have the need to send automatic emails in a Swift project I'm working on. So far my best lead seems to be using Mailgun. (I'm open to better options if anyone has liked something else)
Swift is not listed in the Mailgun API Reference in their documentation, and I didn't see objective-c either. The only article speaking at all about his I've found is this one.
Update
I've been trying to piece together everything and this is where I've gotten so far.
I was able to get Mailgun installed via cocoapods. Using it in Swift has been kinda tricky.
I setup cocoapods with the following pod file:
target 'TestApp' do
pod 'mailgun', '~> 1.0.3'
end
target 'TestAppTests' do
end
With this podfile I was able to run pod install and set up the dependencies. Then I setup an Objective-C-Bridging Header in build settings. I used the following objective-C bridging header.
#ifndef Promises_Promises_Bridging_Header_h
#define Promises_Promises_Bridging_Header_h
#import <mailgun/Mailgun.h>
#import "testMail.h"
#endif
I was getting a linking error for awhile, but I needed to have the project opened via the workspace and I had to go to Product -> Schemes -> Edit Schemes and add the Pods-mailgun to the top of the list and then it would let me build.
Now I want to take advantage of the MailGun API. The docs say to do the following.
Mailgun *mailgun = [Mailgun clientWithDomain:#"samples.mailgun.org" apiKey:#"key-3ax6xnjp29jd6fds4gc373sgvjxteol0"];
[mailgun sendMessageTo:#"Jay Baird <jay.baird#rackspace.com>"
from:#"Excited User <someone#sample.org>"
subject:#"Mailgun is awesome!"
body:#"A unicode snowman for you! ☃"];
I think i am facing exactly the same problem with you. But I am a little confused on how many .h and .m files you have used and what each one contains. I am clueless on obj C so I try to follow you blindly.
Correct me if I am wrong.
• You have one .h bridging header file that contains:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "AFNetworking.h"
#import <mailgun/Mailgun.h>
• You have one .m file that contains:
#import <Foundation/Foundation.h>
#import <mailgun/Mailgun.h>
#interface mailTest: NSObject
- (void) sendMail: (NSString*)email;
#end
• And in your Viewcontroller.swift you have (lets say in a button) this:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func myButton(sender: UIButton) {
var test: mailTest = mailTest()
test.sendMail("testemail#testemail.com")
}
}
Is that correct?
There's another option that you could try if you wanted as much as possible to be on the Swift side:
If you don't have a bridging header yet, create a dummy objective-c file (add new file to project). Xcode will ask if you will like to add a header file (this will sort out the header for you automatically)
Add this to your bridging header (providing you already imported mail gun using cocoa pods):
#import "mailgun/Mailgun.h"
Import maligun in your swift class:
import mailgun
Simply use mailgun's API in swift as you would in Objective-C:
#IBAction func sendEmail(_ sender: Any) {
let mailgun = Mailgun.client(withDomain: "samples.mailgun.org", apiKey: "key-3ax6xnjp29jd6fds4gc373sgvjxteol0")
mailgun?.sendMessage(to: "Jay Baird <jay.baird#rackspace.com>", from: "Excited User <someone#sample.org>", subject: "Mailgun is awesome!", body: "A unicode snowman for you! ☃")
}
At this point I've answered my own question. The process isn't too terrible. Install mailgun using cocoapods.
Link the objective-c code that is needed using an objective-c bridging header.
Create an objective c file to house your method that will call the mailgun operation, and use it.
#import <Foundation/Foundation.h>
#import <mailgun/Mailgun.h>
#interface mailTest: NSObject
- (void) sendMail: (NSString*)email;
#end
Then in my swift code I just call:
var test: mailTest = mailTest()
test.sendMail("testemail#testemail.com")
Here is the mailTest header file. I created a .m file that implements the sendMail method and calls the code from the API and it works great. The only other thing that could be challenging is setting up the mailgun account and configuring your domain so that emails can be sent, but that isn't code related.

iTunes Store Operation failed - the app references non-public [duplicate]

I am getting this warning while submitting app to the Apps store through organizer.
The app references non-public selectors in Payload/.app/: decoder
i know we get this warning if we use any Third Party API in our application. I have used SOCKETIO-ObjC library for chat functionality in application. Also used facebook iOS sdk for fb implementation.So i am not getting exactly what causes this warning.! Please find attached ScreenShot for better understanding
You may get this warning just for using a selector in your own code or third party code that has the same name as some selector that is marked as non-public. Happens to me all the time. Never got rejected for it.
By "same name" i mean just something as simple as you having an object with this selector:
-(id) XYZKMyClass doSomethingFancy:(id) toThis
...and there being a selector like this for an internal Apple functionality
-(id) ApplesClass doSomethingFancy:(id) toSomething
So: What it seems they are looking for is the signature -(id) doSomethingFancy:(id). You can see how it's very easy to accidentally bump up against this.
Presumably they perform a deeper check at the App Store Police HQ, and determine that the flagged selector is in your code, and hence OK.
This can help you:
Before:
#import "SocketIOJSONSerialization.h"
extern NSString * const SocketIOException;
// covers the methods in SBJson and JSONKit
#interface NSObject (SocketIOJSONSerialization)
// used by both JSONKit and SBJson
- (id) objectWithData:(NSData *)data;
// Use by JSONKit serialization
- (NSString *) JSONString;
**- (id) decoder;**
// Used by SBJsonWriter
- (NSString *) stringWithObject:(id)object;
#end
After:
#import "SocketIOJSONSerialization.h"
extern NSString * const SocketIOException;
// covers the methods in SBJson and JSONKit
#interface NSObject (SocketIOJSONSerialization)
// used by both JSONKit and SBJson
- (id) objectWithData:(NSData *)data;
// Use by JSONKit serialization
- (NSString *) JSONString;
**- (id) jsonDecoder;**
// Used by SBJsonWriter
- (NSString *) stringWithObject:(id)object;
#end
I get in this link: http://blog.csdn.net/erica_sadun/article/details/12188083
Check your Target Membership for all classes used in project. In some cases when you create or copy your target the warning may appears without link error.

Resources