MVVMCROSS Changing Language in iOS - ios

I'm trying to implement a multilanguage support with mvvmcross in iOS. The "languages" are stored in a .resx file in the core (Portable Library Class).
So on the android side it works great......I have just to call the recreathe()-Method to rebuild the views.
Is there some similiar method I can call in iOS or in the MVVMCROSS-Library? I experimented a lot with several methods, but without any success.
I know how to make it manually:
Remove all Subviews
Create new instances for each removed subview
Do the bindings again
Add the views back to subviews
But honestly this just can't be the way it goes.
Has anyone a much more sleek suggestion?

if your views use binding (like I described here ) to display the localized version of the string values, the only thing you need to do is to call
RaisePropertyChanged(string.empty) or RaiseAllPropertiesChanged() on the view-models
When user changes the language, you could broadcast a message (using MvmCross messenger) which all the language aware view-models listen to, and on handling this message have the view-models can then call RaisePropertyChanged(string.empty) or RaiseAllPropertiesChanged() on the view-models

Related

Keeping a WKWebView and it's UIViewController in the background running and accessible from multiple ViewControllers

Background: In order to make web requests to an API endpoint, I need to scrape a website and retrieve a token every 25-30 seconds. I'm doing this with a WKWebView and injecting some custom JavaScript using WKUserScript to retrieve AJAX response headers containing the token. Please focus on the question specifically and not on this background information - I'm attempting this entirely for my own educational purposes.
Goal
I will have different 'model' classes, or even just other UIViewControllers, that may need to call the shared UIViewController to retrieve this token to make an authenticated request.
Maybe I might abstract this into one "Sdk" class. Regardless, this 'model' SDK class could be instantiated and used by any other ViewController.
More info
I would like to be able to call the UIViewController of the WKWebView and retrieve some data. Unless I re-create it every 25 seconds, I need to run it in the background or share it. I would like to be able to run a UIViewController 'in the background' and receive some information from it once WKWebView has done it's thing.
I know there are multiple ways of communicating with another ViewController including delegation and segueing. However, I'm not sure that these help me keep the view containing the WKWebView existing in the background so I can call it's ViewController and have it re-perform the scrape. Delegation may work for normal code, but what about one that must have the view existing? Would I have to re-create this WKWebView dynamically each time a different model, or view controller, were to try and get this token?
One post suggests utilising ContainerViewControllers. From this, I gather that in the 'master' ViewController (the one containing the other ones), I could place the hidden WKWebView to do it's thing and communicate to the child view controllers that way via delegation.
Another post suggests using AppDelegate and making it a shared service. I'm completely against using a Singleton as it is widely considered an anti-pattern. There must be another way, even if a little more complex, that helps me do what I want without resorting to this 'cheat'.
This post talks about communicating between multiple ViewControllers, but I can't figure out how this would be useful when something needs to stay running and executing things.
How about any other ways to do this? Run something in a background thread with a strong pointer so it doesn't get discarded? I'm using Xcode 9.2, Swift 4, and iOS 11. As I'm very new to iOS programming, any small code examples on this would be appreciated.
Unfortunately, WKWebView must be in the view hierarchy to use it. You must have added it as a sub view of an on-screen view controller.
This was fine for me. I added this off-screen so it was not visible. Hidden attribute might have worked as well. Either way you must call addSubview with it to make it work.
There are some other questions and answers here which verify this.
Here is a way if you don't wish to use a singleton.
1- In the DidFinishlaunchingWithOptions, Make a timer that runs in the background and call a method inside the app delegate Called FetchNewToken.
2- In FetchNewToken, make the call needed and retrieve the new token (you can use alamofire or any 3rd library to make the call easier for you).
Up on successfully retrieving the token, save it in NSUserDefaults under the name upToDateToken
You can access this token anywhere from the application using NSUserDefaults and it will always be up to date.

iOS Interface Builder: Login into two separate user types

I am wanting to utilize Xcode 5's interface builder using storyboards.
Being that Xcode has been moving faster than most of the online tutorials, I have struggled to find anything remotely close to details on utilizing storyboards with Logins prior to the users profile interface. Here's a briefing on what I am working on and what I have done (with no luck)
It starts out with UINavigationViewController that acts as a "login" initial view controller. Once this initial viewController's requirements are filled, the users' input will determine which one, of the 2, interfaces they will be accessing.
Each interface (1 & 2) will be a UITabBarViewController with separate functionality (only by the user types). With this being said, the user interaction and use of one interface will be required to populate the the other user type's views.
Should I separate the login and interfaces into individual storyboards and then reconnect them programmatically with an empty ViewController that acts as a translator and connector to the type? I came up with this theory here: http://robsprogramknowledge.blogspot.com/search/label/UIStoryboard
Would setting up one large storyboard that holds all the views be the best bet?
Has anyone come across anything that helps explain how to implement these type of new interface builder "solutions"?
I have managed to separate the login storyboard from the user type interfaces, but was only successful with accessing and "logging into" one of the user types, not both, when using the recommendations from the storyboard tutorial link above.
I suggest to build your storeyboard module wise. and use followings Link1 and Link2 to merge them. It will be easy for you if multiple people are working on same project for merging purpose. Else there is a mess if multiple people working on same project with single storeyboard .
Regarding different views for multiple type user you can specify identifier to invoke view progrmatically or use seague to different view

WindowsPhone not refresh/update listbox

i m writing an app where i has created an web service in local network.I m able to grab resources provided by service in android, wp7.1 and ios5.
But i faces a problem, in wp7.1 and ios5 in refreshing the list box and the uitableview controller respectively.
Actually the data source get update in both of this platform but it didn't refresh/update the view which is very important.
if any expertize knows about my problem in depth please help me out from this.if any body has sample code related to updating the view according to the web service kindly help.
i have a solution for this problem in wp7.
may be your web service call fetches the data from catch for the same service call.
try to add current time to show your web service request as new one.
like ".....?&currenttime=...."
try this.
You mention both view and controller so I'm guessing you are using some form of MVVM pattern.
For the controller to update the view a number of things must happen:
The controller must be the DataContext for the view. Most MVVM frameworks handle this plumbing for you.
The controller property that the listbox in the view is bound against must have NotifyPropertyChanged called or must be an ObservableCollection.

Xcode: How to create a multi-langual iOS application with language settings within the application (NOT using localization)

Hi I want to create an application for the iPhone/ iPad, and on the initial page I want the user to have the option to change the language of the application (default language is English). Depending on the language the user chooses, for example Spanish, the initial page text and buttons will all be in Spanish. Then, after a button is pressed, the next view is also in Spanish. (And the back button is also in Spanish)
How could I do this? Remember, I DO NOT want to use Localization. Instead, I want the user to have to possibility to change the language of the app, WITHIN the same app, and NOT the Settings app.
Help with maybe a few examples would be very much appreciated.
Thank you very much in advance!
I've done this a few times. iOS is not designed for this and the first thing you should do is reconsider your approach or go back to the client and make absolutely sure it is necessary. There are only a few situations where this approach is the right one, it's mostly a mistake.
Your first issue is with localising string resources. NSBundle offers methods to load localised strings. Have a singleton class to manage which language you've currently got selected. When you change the language, it can load the en.lproj directory (or whichever language is selected) as a bundle. From that point, instead of using the usual macros to get localised string resources, use a method on your singleton to fetch them from the current bundle.
The same applies to images and other resources - your singleton knows which localisation bundle is in play, so ask it for the resources you need.
When you change language, tell your singleton, and your singleton can load the relevant bundle and post a notification so that your view controllers know to update themselves.
If you are lucky, your nibs layouts work independently of language without modification. This means you can set up a base view controller that others inherit from that has an outlet collection for localised interface elements. It should listen for the notification and walk the collection to update the necessary items.
If you are unlucky and different localisations need different layouts, you'll probably have to save state, get the relevant nib from your singleton, then reload everything and restore state.
You also have a problem that can't be entirely fixed - iOS will still think it's in the main system language. Where there is iOS-supplied content that is out of your control, it will be localised to the main system language. You can mitigate that a little by setting the user preference AppleLanguages to an array with a string containing the locale before the application is fully initialised (i.e. in main.c). However this can only change languages when the application is launched.
A possible implementation:
Use CoreData to create a table of localized strings
Store a Name/Key, language, and value.
You'll then want to build out a manager class to help you read the values. Using a singleton here would probably be a good approach. You could then do things like this:
[[LanguageManager] instance] getLocalizedString:#"mainView_title" forLang:#"en"];
Have you tried to checkout this article?
I see you are asking to NOT to use localization, but the only reason I figured out is because you'd like to change language at runtime.
That post then offers (and explains how to use) a custom utility class to meet your requirements leveraging part of the already existing localization functionalities of the SDK.
Hope that's going to be useful.

BlackBerry - Handling Custom Protocols

I'm currently developing a BlackBerry application where I need to be able to open the application by clicking a link in an email or web page. The link would contain a string of text that would also need to be made available to the application at runtime.
The iPhone OS allows you to do this through custom protocols (ex. appname://some-other-text) quite easily. Is there similar functionality available in the BlackBerry SDK, or is this going to turn into a pipe dream?
I have done something like this by registering a custom BrowserContentProvider (using a unique, custom MIME type). You then use a URL that returns an web page with the custom MIME type, which will trigger your BrowserContentProvider implementation. Part of this implementation can consist of code that launches your application (or bring it to the foreground if already running).
There's another class called ContentHandler that you may want to look into as well. I haven't used it, but it appears to be able to spawn custom handlers based on certain filename-matching conditions.

Resources