Can a custom keyboard extension communicate with the companion app at runtime? - objective-c

Can a custom keyboard communicate with the companion app at runtime? Can a custom keyboard dynamically display content based on states set in the companion app?
If so, what API’s/documentation is available for this communication between the extension and app?

Here is a nice paragraph from the documentation that could get you started in sharing data with your companion app: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW6
I have this working with a today extension and the companion app. They both share the same NSUserDefaults so they can communicate in real time each other. If NSUserDefaults is what you're looking for, here there is a nice tutorial: http://www.glimsoft.com/06/28/ios-8-today-extension-tutorial/

Related

How my react js code can interact with existing Obj-C logic?

I have existing Obj-C project with rich business logic. I wanna try using React Native on the particular screen (I mean View Controller in terms of Cocoa), but every example I see in repo contains logic in javascript. How can I treat React Native as rendering, but pass user actions to my Objective-C code?
EDIT 31 march 2015:
Native view module does not seem to be a good solution, because native modules get instanciated from React code. Thus if I want to use already created view model for that view controller, I need to have some singleton, which will be like a some shared state on the side. I think this is bad.
It's not possible for React views to call native methods directly without going through JavaScript, unless you create custom native view plugins for literally everything onscreen.
Your best bet is probably to create a custom native module that exports all the native methods you want to call, then write a minimal React JavaScript application that does nothing except forward touch events from the views to your module by calling those methods.
If you need to communicate back to the JS application, your module can either use callbacks passed to your exported methods, or broadcast events which the JS code can observe.
To get the most out of React Native though, I'd recommend that you try to keep all the view and controller logic in the JS part, and only expose the business logic from the native side. Otherwise you lose all the benefits of rapid reloading to test changes, etc.
I have the same problem and what I did to solve it:
Create Native Module wrapper around NSNotificationCenter, so javascript code could publish iOS events.
Subscribe for that event within ReactController-wrapper around React-Native code.
Raise event when user clicked button (when we need to give control back to objective-c) with needed data to pass as dictionary.
Catch event from ReactController-wrapper (objective-c), process data, open other controllers/etc.
Using React Native within existing iOS app for some views only
Have a look at the documentation:
http://facebook.github.io/react-native/docs/nativemodulesios.html#content
React Native provides "Native Modules" as a way of bridging between Obj-C and JavaScript. There's also a little bit more about this on the React Native homepage:
http://facebook.github.io/react-native/
Under the "Extensibility" heading, it shows how to expose custom iOS views.
IF you implement RCTRootView in your view controller you can use it to access RTCBridge and then from this gain access to your module.
This will allow you to keep a reference of your view controller inside your module, making you able to pass calls to method directly instead of relying on NSNotifications.
example :
In your view controller
let reactView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "MyReactApp", initialProperties: nil, launchOptions: nil)
self.reactBridge = reactView.bridge
let myModule = self.reactBridge.module(for: MYModuleClass.self) as! MYModuleClass
myModule.viewController = self
And in your module keep the reference :
#objc(MYModuleClass)
class MYModuleClass: NSObject {
weak var viewController: MyViewController!
}

How should a Model be implemented in iOS using Objective-C?

EDIT: Although the answers are teaching me things, I still have no
idea how I might "implement" a model. Can anyone give me an example as
to how I would create a simple model class that has a few basic
functions that make calls to NSUserDefaults or JSON web calls and how
I would access this from ANY of my view controllers?
I am just beginning iOS development and have come to the point where my app will require a Model to interact with the overall data flow between controllers, but am unsure as to how they should be implemented properly.
I understand that the View's are in the storyboard, and the Controllers are the classes associated with those views.
What is the proper way to implement a central Model for an app? Do i create a class (ie "MyModel.h/.m") and then import it from all of my view controllers?
I also see people using a UINavigationController->RootViewController as their model, is this viable?
I have googled this question and searched stack overflow for a few hours, but am now resorting to a new question.
EDIT: Although the answers are teaching me things, I still have no
idea how I might "implement" a model. Can anyone give me an example as
to how I would create a simple model class that has a few basic
functions that make calls to NSUserDefaults or JSON web calls and how
I would access this from ANY of my view controllers?
In iOS, a model (MyModel class) is usually a subclass of NSObject or in the case of Core Data (an iOS framework that helps save data to a database locally on the device) NSManagedObject. As with any model object it contains instance variables and getter / setter methods. Most object-oriented languages have a mechanism to provide encapsulation, in iOS a property provides encapsulation and the keyword synthesize automatically generates the getter and setter methods.
View is subclass from *UIView* which provides the capability for handling touch events and drawing.
The UIKit framework contains classes to draw typical interface elements such as tables (lists), buttons, textfields, sliders and more.
Controller is generally a subclass of **UIViewController** that manages a view, it is also responsible for responding to delegation messages and target-action messages.you can have a UITableViewController which is a subclass of UIViewController that manages a UITableView
TabBar and Navigation View Controllers manages an array of view controllers, but Navigation VC manages VC as a “stack” data structure and yes it is a viable usage
please have a look at Design Patterns in ios apple library resource for further reference and here is a apple sample code to understand how to create a network application using the Model-View-Controller design pattern
this tutorial teaches you how to get started with JSON , try integrating FB in your app for understanding JSON as its fun and easy
start coding NSUserDefault in your app for example
// create a standardUserDefaults variable
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
// saving an NSString
[standardUserDefaults setObject:#"mystring" forKey:#"string"];
here is a good tutorial i started with..
happy coding :)

Way to modify Contact's ringtone via ABPerson class iOS?

So I've been toying around with the AddressBook framework on iOS and have noticed in the ABPerson class, pretty much every attribute of a contact is listed as a constant (ref: apple documentation). However, one attribute I notice is missing is the ability to make a custom ringtone. Is there any way to modify the ringtone of a contact via the ABPerson class? If not, is there any way at all to modify the custom ringtone using the AddressBook framework? iOS 5.1.1.

Application Delegate Usage

I'm fairly new to Objective-C and cocoa programming, so I don't really understand the concept of App Delegates.
When we create a cocoa application, do we store our code (Methods, actions, outlets) in the App Delegate files or do we create a new file that will act as a controller and code from there. Right now, I put all of my code in those two files, but from what I read, your goal is to try to make your App Delegate files as slim as possible.
My question is: What's the usage of the app delegate files?
Talking about applicationDidFinishLaunching::
It's just your application entry point. Normally you only create the window and your first ViewController, or your Tabbar - your main starting interface class - here.
All the other delegate methods of the NSApplicationDelegate have other functions of course. Most of them are the point, where you react on the state of the app. Opened / Closed / Backgrounded / Reopened etc.
But you should probably have a look at the programming tutorials in the iPhone documentation. There is a lot of information on how to structure your objc projects. E.g. look here: Start Developing iOS Apps Today
Or if your looking for OSX Apps, look here:
1) Your First Mac App
2) Mac App Programming Guide
There is also a bunch of Sample code.
The App Delegate is a handler location to handle events that occur on the application. Things like open and close. It also hangs around the whole time the application is executing and you can grab the singleton instance at any point by doing [[NSApplication sharedApplication] delegate].
This comes in handy for handing objects between controllers and serving as a router for events. You can also store some data on the delegate if you need to modify/have access to it in different parts of the code.
This all works well for simple applications, but as things become more complex, you need to have some division of responsibilities. The AppDelegate should really only be responsible for actions that occur on the application itself, not on another view or a controller. Putting all/most of your code in the AppDeligate is certainly bad practice and will lead to horrible code as things get more complex and need to be maintained.

How to structure the code for a Cocoa Application

I'm writing a Cocoa application where I have an auto generated app delegate:(MyAppDelegate.h/MyAppDelegate.m)
So I am not sure of the best way to structure the class files for this Cocoa application. I understand MVC thoroughly (on the iPhone) but I'm having a block as to try organising the source properly in a Cocoa app.
I need to spawn two separate fullscreen OpenGL views. The problem is that I could simply create classes for "OpenGLView" then instantiate and call all this code into the app delegate, but it seems messy and it's aparently not the place to do it.
How would I best achieve the code structure?
Instantiate your application delegate in the MainMenu.xib file and hook it up. When I've done Cocoa fullscreen stuff, I've instantiated the views in the -applicationDidFinishLaunching method of the application delegate. It *is* messy, because for fullscreen views it doesn't really make sense to use interface builder. This is the same way that other folks do fullscreen apps in Cocoa.