MessageKit set up MessageLabelDelegate for didSelectURL - ios

I'm trying to open url from messages embedded in an NSAttributedString. I'm using the latest version of MessageKit. I'm using the function didSelectURL, but it doesn't seem to reach that part of the code. I came across this solution, but I wanted to follow up with regards to how to set up MessageLabelDelegate. Just like in the linked solution, I have implemented the delegate methods detectorAttributes and enabledDetectors.
I currently have MessageLabelDelegate set up as an extension to my ViewController, but it never seems to reach those methods.
extension ChatViewController:MessageLabelDelegate {
func didSelectURL(_url: URL) {
// .. open URL
}
}
It seems to be getting overwritten by the method didTapMessage in the MessageCellDelegate extension. I tried going through the example code in the MessageKit repo, but still unclear as to how it's meant to be set up or what I'm doing wrong.
UPDATE: The problem was the way I was declaring the NSAttributedString. I was setting its value as a string instead of a URL.

The MessageCellDelegate implements the the MessageLabelDelegate so you need to make sure that the messagesCollectionView.messageCellDelegate is set to the ChatViewController and that the ChatViewController has an extension for MessageCellDelegate where you can put your didSelectURL
public protocol MessageCellDelegate: MessageLabelDelegate {

Related

library mention swift when typing # like twitter

I have 'facebook like' project to learn ios mobile development, the feature i want to create right now is mention another user in comment UITextView.
i already tried library SZMentionswift, but i found it very difficult to implement because the example is very confusing to me.
is there any library or example to make listener when user typing '#' then show the list of user like twitter?
thanks
link to SZMentionsswift https://github.com/szweier/SZMentionsSwift
I can help you integrating the library, ref: Example code kindly follow the steps
Conform to UITextViewDelegate
private var myInputAccessoryView: UIView?
init() {
super.init(nibName: nil, bundle: nil)
myInputAccessoryView = SZExampleAccessoryView(delegate: self)
}
add following code and override inputAccessoryView property of UITextField
override var inputAccessoryView: UIView {
return myInputAccessoryView!
}
Copy SZExampleAccessoryView.swift and SZExampleMentionsTableViewDataManager file to your code
Pass your array to be searched while looking for names to be mentioned in #mention to names in SZExampleMentionsTableViewDataManager.swift.
this will let you run # mention same as it is done in example.

Swift Protocol in Framework

so i have this weird problem that's been bugging me for the last few hours.
I have a framework in which I created a protocol named ChatDelegate (code bellow)
public protocol ChatDelegate: class {
func chat(_ chatCollectionView: UICollectionView, didSelect message: Message)
}
and a ViewController (not in the framework), which conforms to the ChatDelegate, like so
extension ChatContainerViewController: ChatDelegate {
func chat(_ chatCollectionView: UICollectionView, didSelect message: Message) {
print("did select")
}
}
but the compiler still complains that the ChatContainerViewController does not conform to the protocol and I don't understand why?? The function has the exact same header (I also tried putting public in front ...didn't help).
Any help would be much appreciated.
UPDATE
I figured it out. The problem was that I had Message class in my project and in the framework and the compiler didn't know which one to choose. Adding ModuleName in front (ModuleName.Message) fixed it. :D
I had the same issue. The file that defined the protocol had a target membership in both the framework and the application targets. I solved the issue by making the file that defined the protocol only have a target membership in the framework and then adding an import <FrameworkName> to the code in the application target that needed to use the protocol.

Implementing Methods from Objective C Library with Swift

I am trying to implement the following method in swift:
From the class FLIROneSDKImageReceiverDelegate, which is subclassed inside my ViewController class as so:
class ViewController: UIViewController, FLIROneSDKImageReceiverDelegate,
FLIROneSDKStreamManagerDelegate,
FLIROneSDKImageEditorDelegate{
Note that I have already created a bridging header etc.
In the FLIROneSDKImageReceiverDelegate header file:
- (void) FLIROneSDKDelegateManager:(FLIROneSDKDelegateManager *)delegateManager didReceiveBlendedMSXRGBA8888Image:(NSData *)msxImage imageSize:(CGSize)size;
Am I wrong in thinking that this is the correct way to implement this function?
func FLIROneSDKDelegateManagerdidReceiveBlendedMSXRGBA8888ImageimageSize(delegateManager: FLIROneSDKDelegateManager!, msxImage: NSData, size: CGSize){
Note that FLIROneSDKDelegateManager is a class.
Off the top of my head, but try this:
func FLIROneSDKDelegateManager(delegateManager: FLIROneSDKDelegateManager!, didReceiveBlendedMSXRGBA8888Image msxImage: NSData!, imageSize size: CGSize) {
// method imp
}
#Laxsnor's solution in the comments on the answer by #aaron-wojnowski helped me too, thanks both.
To consolidate:
The problem is a conflict created by the name FLIROneSDKDelegateManager being used as a both a class name and a function name - which seems to be OK in Objective-C but not in Swift.
Replacing the class FLIROneSDKDelegateManager with NSObject in the function parameter seems to solve the problem without side-effects. This has to be done in both the Objective-C protocol header file and the Swift delegate class source file.
NOTE I also found this same solution applied more broadly to Swift-ify the entire FLIROneSDK at https://github.com/jruhym/flirmebaby.
Happy developing for FLIROne on Swift. (I'm new to FLIROne and relatively new to Swift so apologies if my language isn't quite precise enough.)

NEWBIE can I use my own SFSafariViewController and preload a url. Then use it on storyboard

I am trying to designate on a storyboard a UIViewController as my own custom class SimpleSafariViewController. The compiler won't let me override init() method to preload http://news.google.com
Compile error is "Override of init method is unavailable"
If the compiler allowed me, I would have the following code
SimpleSafariViewController
convenience init() {
super.init(URL: URL("http://news.google.com"), entersReaderIfAvailable: true)
}
If so, then is there another way of solving this.
I have posted the simplest project on github
https://github.com/joshuacalloway/SimpleSafari
swift 2.1, xcode 7.2
You are not using SFSafariViewController like suggested to do so. It is not meant to sit as a ViewController on your StoryBoard. The implementation in SuggestedSafariViewController is the correct way to do it. (Agree with #matt)

I want to call a class function written in Swift from the AppDelegate (which is in Objective C). What am I doing wrong?

I have a swift file "SomeController.swift" it is like this:
import Foundation
func performSomeStuff() {
println("Performing stuff")
}
Now in the app delegate, I am trying to do this: (note that the swift bridging header is imported)
[SomeController performSomeStuff]
But its not working.
I have also tried this:
import Foundation
class SomeController:NSObject {
class func performSomeStuff() {
println("Performing stuff")
}
}
But it still fails.
What is the correct way?
Add:
#objc
before the class keyword in your swift code so it will be:
#objc class SomeStuff: NSObject {
}
Also add #obj in front of any function that you want to call.
Then in your app delegate make sure to use #import "projectName-Swift.h"
Are you able to access "SomeController" class in objective-c, if not then you firstly need to add "${ProjectName}-Swift.h file and add Swift compilation support in Build settings as:
And for accessing methods from Swift to Objective-C, add
import Foundation
class SomeStuff:NSObject {
#objc class func performSomeStuff() {
println("Performing stuff")
}
}
before functions name.
In some cases, you need finer grained control over how your Swift API is exposed to Objective-C. You can use the #objc attribute if your Swift class doesn’t inherit from an Objective-C class, or if you want to change the name of a symbol in your interface as it’s exposed to Objective-C code.
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html
Ok, So I figured it out. First, here's the correct way. Its like some of you said:
In the swift file, I have it like this:
class MyController:NSObject{
class func performTask {
// Here my task is running.
}
}
Then in the app delegate, I just import the swift header file. And do this:
[MyController performTask];
Now for the part I had wrong. It was an error on my part, but maybe it'll be useful to someone else out there.
When I first created the first swift file, I had placed it inside a folder within the my Source folder. But when I started having multiple swift files, I moved the bridging header outside that folder and into the main Source folder (just for organising).
The problem was, it did not give me a direct error to tell me what was the problem. I had to check the issue navigator to identify the problem.
Hope this helps someone out there.

Resources