Correct way to display today view widget content - ios

I'm working on a today view extension with some custom view elements which I don't set up in interface builder.
Now I'm wondering where the right point in the lifecycle is to init the widget content.
I read about updating the content in widgetPerformUpdateWithCompletionHandler so I implemented a check for new updates which should about the view.
But my observations showed me that the method is called before viewDidAppearand so there is no view to update.
Also I tought the widget stays in memory for a while so that I can have a certain object in widgetPerformUpdateWithCompletionHandler whose content I can update and use in viewDidAppear but this isn't the case (it loads every time)
So what is the correct way: write content to disk in the widgetPerformUpdateWithCompletionHandler, can I depend on something that is in memory or just ignore and refresh on every load?

In TodayViewController.m -viewDidLoad() method is called each time you open notification. So when ever you check Today widget it will call -viewDidLoad(). So you do your customisation in -viewDidLoad(). Unless you don't want to check previous state.
Straight from a blog which helps a create Today widget with real time data:
Creating today widget
Caching
We can take advantage of NSUserDefaults to save the calculated used space between launches. The lifecycle of a widget is short so if we cache this value, we can set up the user interface with an initial value and then calculate the actual value.
Edit:
From Apple documentation it state that
To help your widget look up to date, the system occasionally captures snapshots of your widget’s view. When the widget becomes visible again, the most recent snapshot is displayed until the system replaces it with a live version of the view.
It is also state that from Notification center, We can get widget details of state and snapshot.
Not sure with this, but it state that below method is used by both widget and containing app to check its content. There is no much details about how to get widget snapshot.
func setHasContent(_ flag: Bool,
forWidgetWithBundleIdentifier bundleID: String!)
NCWidgetController setHasContent
Edit 2: Use of NSUserDefaults in extension:
You have two .entitlements files, one for host app and second for the extension app, add below key in both files
<key>com.apple.security.application-groups</key>
<array>
<string>group.YourExtension</string>
</array>
You see the value for key is group.YourExtension it is shared
common key for both host app and extension app
Save your data using group.YourExtension in NSUserDefaults

Related

SwiftUI TextField with non-default keyboard does not update the bound value

I am using a TextField to update a numeric value in my app model. The page editing the values is triggered from a NavigationView. Everything works fine when I use a default keyboard, ie:
TextField("Enter a number", value: $userData.chosenNumber, formatter: NumberFormatter())
.keyboardType(.default)
will update the value of userData.chosenNumber when hitting the Return key and this change is reflected on the main navigation screen (userData is an EnvironmentObject).
However, the same code using .keyboardType(.numberPad) instead won't show a Return key, and userData.chosenNumber won't be updated when navigating back to the main view. This happens both in the simulator and live on device (but things work as expected using SwiftUI live preview, or in the simulator when hitting the physical keyboard Return key).
Is there a way to get the new value picked up please without having to use a default keyboard?
--Edit--
Based on pawello2222's suggestion in the comments, I tried the solution in this other SO's question but that didn't fully work in my case.
What the other question suggests: introducing a Binding<String> (via a computed property) that acts as intermediate between the value I want to modify and the string displayed in the UI.
This works to modify the value inside my view.
However the runtime fails to detect that the EnvironmentObject injected by the parent view is modified, so after leaving the view the app UI is not refreshed and the other views do not get updated.
(For some context, my app computes a diver's bottom time based on an air tank site and the diver's air consumption. The detail view is used to edit the diver's air consumption value while another view displays the resulting bottom time. So in my case the diver's consumption gets updated, but the bottom time doesn't receive an update, unlike what happens with a standard keyboard.)

UNNotificationContentExtension not displaying

I'm trying to make a custom notification alert. I added my extension target and changed the UNNotificationExtensionCategory value to 'drawing'.
In my push notification I added a "category" : "drawing" element to the aps block. The didReceive(_:) method from that extension is never getting called though. What did I forget to do?
You seem to have done whats needed. I too had similar problem of not seeing the custom notification screen.
However, whats not mentioned in the documents of UNNotificationContentExtension is that the custom screen appears only when you do a 3D touch on the notification alert. I tried to do a 3D touch and the custom screen appeared. Please check.
First check that the extension category is set appropriately in the extension info.plist file to match what is being sent in the aps block of your push notification
Second check that the deployment target for the extension is <= to the iOS version of the device you're trying to run on.
Have u added this in your target's info.plist file instead of ACTIONABLE add drawing
I had similar issue, it was just that in the plist I made my UNNotificationExtensionCategory to be an Array and I wasn't adding them correctly.
Incorrect:
It shouldn't come next to UNNotificationExtensionCategory ie it shouldn't be a field of NSExtensionAttributes
Correct:
It should be a field of UNNotificationExtensionCategory
Apparently to learn how to use the plist you need at least 25 years of experience :)
This method is only called once you force touch into the notification.
If you are monitoring this through LLDB, it runs as a separate process. Make sure you're building your extension target in order to access these methods and the logs from LLDB.
Lastly, if you are like me, and you have discarded the storyboard, then you need to specify the principal class in your extensions Info.plist. This class must be your main view controller including the target namespace. For example, if your extension is MyNotificationExtension then you would set NSExtension > NSPrincipalClass to MyNotificationExtension.MyNotificationExtensionViewController.

WatchKit reloadRootControllersWithNames causing error, with pageController or after push/pop

I have a basic watchkit app that loads a page based navigation of 3 interface controllers. This works well, but I'd then like to trigger an action to remove the page-control and essentially revert back to the original InterfaceController that was present when the app first loads.
// load page based control, with 3 views. this works ok
[WKInterfaceController reloadRootControllersWithNames:#[#"pageController1",#"pageController2",#"pageController3"]
contexts:#[#"data1",#"data2",#"data3"]];
// attempt to reload original interface controller, identified by storyboard id
[WKInterfaceController reloadRootControllersWithNames:#[#"myInterfaceController"] contexts:#[#{}]];
The page based navigation remove, the original navigation loads after a short spinner. However it fails to function correctly and original Actions result in this error.
Extension[6766:123665] *********** ERROR
-[SPRemoteInterface _interfaceControllerClientIDForControllerID:] clientIdentifier for interfaceControllerID:(null) not found
Is there a better way to cleanly reload the original InterfaceController?
EDIT, 2/19
It seems there are some other actions that are causing this error too. For instance, if segue to a second InterfaceController and then popController to get back, the error often appears. It is always related to a secondary call to this function.
[WKInterfaceController reloadRootControllersWithNames: contexts:]
EDIT2, 3/18
As previously mentioned, this is reproducible 100% of the time by doing the seguePush, the popController, then attempting to reloadRootControllersWithNames.
If the seguePush/popController is not done beforehand, then the reloadRootControllersWithNames will work fine.
This situation seems to be in addition to the multi->single-multi instance of this bug.
This is actually not a bug because according to Apple:
You cannot combine hierarchical and page-based interface styles. At design time, you must choose the style that best suits your app’s content and design for that style.
So unfortunately, we can't mix Hierarchical and Page-based navigation patterns within the same Watch app.
Just one of many limitations we have to deal with when developing apps for  Watch
This is a bug in WatchKit in Xcode 6.2 Beta 5. Please dupe the following radar on Apple's Bug Reporting System to help raise the priority to get this fixed.
In the meantime, a workaround that I've found can be found on the dev forums. What you can do is add a dummy interface controller to any single interface controller page set so you always have two. This will fix the error until Apple get's the bug fixed (hopefully in Beta 6). Please dupe!
I was able to solve my instance of this problem by not using popController on a pushed view controller. Instead I use a reloadRootControllersWithNames in place of the popController.
How this allows both push and paging, via an example:
Push a view controller
reloadRootControllersWithNames to return to the original controller. (The transition is not quite as animated, but is sufficient)
Create page based view controller.
reloadRootControllersWithNames to return to the original controller
Repeat 1 or 3 as needed.
This eliminates the error at the cost of non-animated popControllers, and allows partial pushing and paging. It would not allow more complex push navigation though.
There may be a better method of navigating to a sub interface controller without a push call, but I'm not aware of it on the watch yet.
None or the answers above worked for me. This problem began when I changed the icon names for the app and the watch app name. I solved it like this:
1) Click on your Watch app Target > Capabilities > make sure app Group
is in ON.
2) Make sure the App Group is selected.
3) Clic on the circled arrow Refresh icon (this will apparently just
refresh this thing if you already had it)
4-Repeat steps 1-3, but for your Watch App EXTENSION target too.
5-Click on the Scheme button (on the right side of the STOP button),
and clic on Edit Schemes.
6-Click Run > Info 7-In executable select your target (Actually it
should already be selecting but opening this window seems to
refresh the option, and wipe the error)
Apparently all these things above are not updated automatically when you change the icon name (Target names) and you have to go to those menus and open them to refresh them manually. Shame on Apple perhaps?

Change the view controller app launches with when button is clicked - Xcode

I'm attempting to create an introduction to my iOS app. So I've created a few view controllers that are shown in the beginning of the app. On the last introduction view controller there is a button which brings you to "the actual app".
How do I make it so when that button is clicked the app launches with a different view controller.
If someone could walk me through the steps and be very detailed I would appreciate it as I'm a beginner iOS developer.
One option is to store whether "view intro" is set to true/false & set this value in NSUserDefaults. Check that value when the app starts & put an if/else to load the appropriate VC. You might want to make the button "skip" intro entirely & set this value. People generally get annoyed w/long intros & want to use your app. See this answer about NSUserDefaults.
I am assuming you are creating an "app tutorial" type of introduction. Also assuming that you only want this to occur the first time the user uses the app, then go directly to the app every other time.
I would store a didWatchTutorial BOOL value in the NSUserDefaults.
This would also need to include an if/else statement in the opening view controller of your app, which controls (based on this BOOL value) if the user should be shown the tutorial:
if (didWatchTutorial) {
//GO STRAIGHT TO APP
} else {
//SHOW TUTORIAL
}

MonoTouch - Refresh TableView

I am writing a news application for iOS using MonoTouch. The data comes via an OData web sevice.
I have a singleton class called NewsFactory which is responsible for querying the OData feed. Now in the ViewDidLoad method I am retrieving the list of news articles and binding it to the table view.
However - I am not sure how to refresh this every time the app is launched. I don't care much for "pull to refresh". All I want to make sure is that every time the app is launched (from scratch or activated again) that the call to the web service is made to make sure I show the latest info.
In addition, I want to cover for the scenario where the network connectivity is lost when the app is launched and I notify the user. The use closes the app (suspend) and let's say the network connectivity returns - If the user opens the app again, I would like to make sure that I show the latest news articles.
I am building the view hierarchy programmatically. I have navigation controllers and a tab bar controller with 5 tabs.I am targeting iOS 5.
I tried playing with OnActivated and ViewWillAppear and they don't do exactly what I need to do. This is a very common need for any iOS app so there must be a solution - I am new to this so any help is appreciated.
Well you have to think, in general, if you call a method that loads from one source (let's say a text file that has data in it), then you change up the source (change text data), but call the same method when the class is loaded, then the new data will appear when the new class is loaded.
Like for example:
- (void) loadView {
NSString *testString = [NSString stringWithContentsOfFile:filePath];
textView.text = testString;
}
This example shows the setting of text in a file to a UITextView instance. If you load it the first time and the file contained the contents of "Hi", then the textView will say hi. If you change it, then call the same method, then it will show your changed text the second time you load the app.
The point is that if you change data, then call the same method, then you'll see a difference. Unless you're working with a database.
I'd give you a better example if you provide sample code.
EDIT: Not sure if your custom class comes with this function, but it is included in the regular UITableView class:
[tableView reloadData];
You can call that after your array changes internally.

Resources