How to avoid this error: "Took too long to show custom notification. Falling back to static."? - xcode

Having issues testing out the custom long look notification on the apple watch simulator. The debugger log this error:
WatchKit Extension[5230:156324] Took too long to show custom notification. Falling back to static.
How to resolve this issue?

I had exactly this problem with the default code set up by Xcode. I didn't do any operations inside didReceiveRemoteNotification, but the error still occured.
Turns out the reason it failed was that my WKUserNotificationInterfaceController subclass was not connected to the Dynamic interface Controller in my storyboard file.
Solution:
Go to the storyboard file for your watchkit app.
Click on your Dynamic Interface controller
Click the identity inspector (middle tab in the right pane)
In the Class field, select your
WKUserNotificationInterfaceController subclass (xcode had named mine
NotificationController)
Voilà!

I had the same issue before.
Inside didReceiveRemoteNotification, are you calling completionHandler(WKUserNotificationInterfaceTypeCustom); ?
Also, what are you doing inside that function? If takes too much time, it will show by default the static notification:
"Use the static notification interface to define a simple version of
your custom notification interface. The purpose of a static interface
is to provide a fallback interface in the event that your WatchKit
extension is unable to configure the dynamic interface in a timely
manner"

Try giving your custom class name as NotificationController. Xcode take it as WKUserNotificationcontroller. It worked for me.

This will happen if you take too long in your handler to setup. If it takes too long, the watch will default to the static notification.
Be sure you are calling the correct completion block:
completionHandler(WKUserNotificationInterfaceTypeCustom);
When calling the completion handler block, if you want WatchKit to display your static interface instead, specify the WKUserNotificationInterfaceTypeDefault constant.
Reference:
https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/CustomzingthePushNotificationInterface.html

In my case the problem was that the created WatchKit Extension was configured as Swift code, while my entire project was in Objective-C.
The Dynamic Interface never show up, always switching to the Static one printing the error of this post in the console. Once I changed the WatchKit Extension to Objective-C everything worked perfectly.

If you use a real watch, try turn off "Wrist Detection" in "Watch" -> "General".

Related

Class PLBuildVersion is implemented in both frameworks

iOS 10 / Xcode 8 GM build getting the below, never had it before on Xcode 7. Any ideas?
objc[25161]: Class PLBuildVersion is implemented in both
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices
(0x12049a910) and
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices
(0x1202c4210). One of the two will be used. Which one is undefined.
(NOTE: Only seems to happen in simulator, does not appear on real device).
Main Idea
Main idea is simple:
If your app (or dependencies, such as Pods) uses framework, that uses explicit (or implicit) PhotoLibraryServices.framework or AssetsLibraryServices.framework as dependency, Xcode warns you (even if you are using only one of them). It might be Photos/PhotosUI.framework or AssetsLibrary.framework, or another (I don't have full list of dependencies, but it is possible).
What is the problem?
Class with name PLBuildVersion is defined in both PhotoLibraryServices.framework and AssetsLibraryServices.framework. Class name is unique in Objective-C (you can't define 2 classes with same name), so it is undefined which one will be used in runtime.
However, I think that it will not be a problem, because both classes have same methods and fields (checked this with disassembler) and I guess that both were compiled from the same source.
Radar is already sent.
As per answer from Apple employee on Apple's Developer Forum:
You don't control either of the class sources listed, so there isn't anything you can or should do – aside from Reporting a Bug.
I was unable to find a way to get rid of the warning, but if you want to prevent the app from crashing, you need to provide a description for why you are accessing the camera, photo library, etc. This is new in iOS10.
Input the following into your Info.plist file.
Photo
Key: Privacy - Photo Library Usage Description
Value: $(PRODUCT_NAME) photo use
Camera
Key: Privacy - Camera Usage Description
Value: $(PRODUCT_NAME) camera use
More info can be found here: https://iosdevcenters.blogspot.com/2016/09/infoplist-privacy-settings-in-ios-10.html
I find you can get this error merely by using a UIWebView. My solution was to replace my use of UIWebView with WKWebView.
I had this after adding Answers on Fabric to my project.
Deleting derived data did the trick for me. (shift alt command k in XCode)
Edit a year later:
After deleting derived data, always exit XCode and start it again.
In unrelated cases I have the impression that deleting derived data does not clear XCode’s in memory caches of the derived data.
Resetting the iOS simulator fixed this for me. Simulator -> Reset Content And Settings.
In my case this warning started to appear after opening a second xcode project and running the second app on the simulator. After changing back to the first app, the warning started to appear. I just quit the Simulator and Xcode and reopened my project. The warning disappeared after that. If that doesn't solve it, proceed with the other answers. Xcode can be really picky sometimes.

Xcode shortcut to implement protocols in Swift [duplicate]

When I use a prototype table view, I always have to conform to the protocol TableViewDataSource. I always forget what methods I need to implement, so I have to look at the source of the protocol every time. This is really time consuming.
I think Xcode must have a feature that automatically implements the needed methods for you, right? Just like IntelliJ IDEA, Eclipse, and Visual Studio.
I want to know where can I find this feature. If there's isn't, is there a workaround for this? At least I don't have to open the source code of the protocol each time I conform to it.
If you don't understand what I mean, here's some code:
I have a protocol
protocol Hello {
func doStuff ()
}
When I conform to it,
class MyClass: Hello {
}
I often don't remember the names of the methods that I need to implement. If Xcode has a feature that turns the above code, into this:
class MyClass: Hello {
func doStuff () {
code
}
}
Now you understand what I mean? I just want to ask where to find such a feature.
Xcode 9 : you can add missing Protocol Requirements by add new shortcut to your Key Bindings Set
From the top Xcode menu (top left) choose Preferences.
Choose Key Binding.
Search about protocol. you will find Command called Refactor -> Add Missing Protocol Requirements
Press on Key column then add your shortcut. ex: cmd + shift + M
Now you can add missing Protocol Requirements by clicking on class name name (or his extension) then press your shortcut
Well if i understood your problem then here is a workaround:
try to define methods with protocol as prefix like here hello then you'll not have to remember the methods just start typing protocol name and XCODE will prompt you with all available methods see here:
And if you want autocomplete protocol try
Snippets
Xcode 9, takes care of implementation of mandatory methods of Swift Datasource & Delegates.
Look at these snapshots, with example of UICollectionViewDataSource:
Indicating warning to implement protocol methods:
By clicking on 'Fix' button, it has added all mandatory methods:
Very similar to Amjad's post, Xcode 9/10 brings the functionality to add missing protocol requirements right from within the code editor.
Just "right click" on the class name: "Refactor" --> "Add Missing Protocol Requirements"
Xcode won't do it for you.
If you look at the documentation for the protocol, it's clearly marked which functions you have to implement:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/

Could not connect action to NSViewController

I have a stupid problem with Xcode. Sometimes - and I don't know why, because I always follow the same procedure - if I create an IBAction from the nib file of my NSViewController to the .h-file of this NSViewController, I get the error "Could not connect the action buttonCancelClicked: to target of class NSViewController".
It's crazy, because the File's Owner is set to the .h-file and even if I click the button, the IBAction is called. Only Xcode throws this error.
Is this a bug of Xcode? Can I do something to remove this error?
An NSViewController serves to manage NSViews. Disconnect the button first. Declare the action in (maybe another) class, save, and read the header in IB again and connect it in IB the button. That's how I do it and never had a problem.
Another reason that will generate this message is when the class identifier is not pointing to the correct Project. I had copied a class from one project to another.
My initial class identifier looked as follows:
This generated the console messages as shown in the original question.
I set the class then as follows:
This solved the problem.

setValue:forUndefinedKey:]:

FinalViewWithSending *newView = [[FinalViewWithSending alloc]initWithNibName:#"FinalViewWithSending" bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:newView animated:YES]; //it crashes here with a thread 1 error, SIGABRT error..
It says:
"Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<FinalViewWithSending 0x1bef70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key loginButton.'"
There is no variable called loginButton so Im not sure why i am getting an error...
Thank you
You are getting the error because there is no property called loginButton. Check your .xib file. There is almost certainly a bad link to a FinalViewWithSending object (likely File's Owner) that specifies loginButton even though it doesn't exist in the class code.
Phillip Mills is correct.
The answer here, for anyone searching, is to edit the storyboard and remove any link to the undefined key specified. You have to edit the storyboard file in an external editor: Right click on the storyboard listing in the hierarchy and then click on "show in finder" or what have you. Open in a text editor, remove said links by searching, save and return to Xcode. No more issue.
This happens when you remove something from the view controller improperly.
Agreeing here - Phillip Mills is correct.
Someone mentioned opening the storyboard up in a text editor - this isn't necessary.
Just click on the storyboard, go to the Connections Inspector. If you've got a problematic outlet, you'll see an exclamation marker next to the outlet. Delete it.
Since this is the first search result for this error, I decided to write an answer for rookies like me.
You are going to get this error if you have Ctrl dragged connections from Buttons which generates code in your ViewController and then you delete just the code without removing the connection.
I got this error because I had accidently added a few buttons as Outlets, when I wanted to add them as Actions. I deleted the code that got generated but the bad connections were still there.
To find and remove them, View->Utilities->Show Connections Inspector
Then click through the different buttons you have and click the x to remove the bad connections.
If you have any localizations, search for bad links in all the storyboards (expand storyboard to find localized storyboards).
This was my situation. I was looking for bad links in one storyboard and they were in the localized one.
You can try the way I used to fix my setValue:forUndefinedKey:]: problem.
I had the same problem:
I had previously made a connection using storyboards from a textfield to a header file. I later decided I wanted to rename my object's connection, unfortunately, I didn't break the connection properly. If this problem was the same as mine, a connection existed to a button previously, and the code in the header was deleted, but the connection was not.
Check the object connections in the view you are segueing to.
You can use associated objects to avoid subclassing. As for the setValue:forKey: and setValue:forUndefinedKey:, take a look in the header file (NSKeyValueCoding.h) for details.
According to your description,
Find newView in your interface builder and right click it,
you will see something like loginButton which is abnormal.
Today I was working and I presented this same error, I used the response of Phillip Mills was correct and I had some labels in one ViewController.xib that were not being assigned links, i have five bad links, once assigned links, everything works perfectly, thanks answer of Phillip Mills.
Go to the connection inspector of your xib of FinalViewWithSending view controller & remove the reference of login button. After that it will run. Try It... :)
Probably, you earlier created the LoginButton, Cmd+Shift+F and find the "LoginButton" and remove the Referencing Outlet and Re-build.
This reply might be late relative to the time the question was asked but I just had this issue and I tried all solution posted here but none worked.
Here is my solution which could help anyone in the future.
Firstly the problem, :I used a custom cell class and made my connections, everything worked fine with my app then, I renamed my custom cell class ran my app but crashed with setValue:forUndefinedKey:] error.
My solution : Clicked .xib file, selected Identity inspector and typed the new named class in Custom Class -> Class tag.
App error resolved.

Xcode 4.2 Template Changes - UIApplication & MainWindow.xib

Background: Up until Xcode 4.2, new projects created using any of the templates would contain a MainWindow.xib and therefore pass nil as the fourth argument of UIApplicationMain(). Starting in Xcode 4.2 all the templates instantiate the application delegate by passing the class string as the fourth argument and do not build the application's window in a xib.
It is trivial to accomplish this setup in 4.2, and of course it works as expected: create xib setting File's Owner to UIApplication and wire up the delegate, specify it in Info.plist, nil fourth argument in main().
Question: Why is Apple encouraging instantiating the application delegate and building the UIWindow in code now instead of the "old way?" What are the benefits?
Considerations: I would expect this new template behavior if you elect to use storyboarding as a way to manage the UI, but if you uncheck "Use Storyboards" I would have expected the old pass-nil-and-use-MainWindow.xib template.
This question was asked in a roundabout way here, but the answers are a little thin on discussion.
You're asking why Apple is doing something? There can be no definitive answer, unless Apple has spoken out explicitly, which they have not done.
Personally I find the new approach considerably more elegant, transparent, and bulletproof. As you rightly say, in the old approach the main nib was loaded automatically by the runtime in response to the Info.plist setting, and everything else that happened was done through the nib, in particular the instantiation of the app delegate and the window and the associated wiring (the app delegate must be made the application delegate, the window must be made the app delegate's window), except that then we come back to the code in the app delegate for final presentation of the interface.
This was hard to understand; it took a great deal of verbiage for me to describe it in my book. It was also easy to break. The nib has to know the name of the app delegate class, so if you didn't like those funny long names that were created by default, you could easily mess everything up when you changed them.
Now, however, the app delegate is simply named App Delegate and is instantiated in code by UIApplicationMain(), as you rightly say; and everything else is also done in code as a direct follow-on: the app delegate is instantiated and didFinishLaunching is called, whereupon we create the window in code, assign it to our property in code, load the nib if there is one in code, set the window's rootViewController in code, and show the interface in code as before.
Thus the bootstrapping is directly exposed to view because it's all code. This makes it easier to understand and to modify without breaking anything. It's almost as if previously the template designer was just showing off as to how much stuff could be made to happen magically and automatically behind the scenes; now everything happens out in the open, explicitly.

Resources