WKWebview: Handle the button click event from app - swift2

Can I handle the button click event of WKWebView from my app.
Simply I have opened a facebook login page in my WKWebView. Is it possible to handle the event of that login button from my app?

You can use the navigationDelegate of the WKWebView to listen to that action. The delegate method you should implement is:
Swift
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Swift.Void)
{
let request = navigationAction.request; // Inspect this request to see if it's from Facebook
let policy = WKNavigationActionPolicyAllow or WKNavigationActionPolicyCancel depending if you want the request to continue firing or not
decisionHandler( policy );
}
Objective-C
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSURLRequest *request = navigationAction.request; // Inspect this request to see if it's from Facebook
WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow or WKNavigationActionPolicyCancel depending if you want the request to continue firing or not
decisionHandler( policy );
}

Related

xcode 12 firebase notification when app in background how to do a reload if the app have a opened webview

I implemented firebase notification in Xcode 12 via cocoapod and basic step by step... notification ARE working all nice and good, even when app in background.
this tutorial : https://www.appcoda.com/firebase-push-notifications/
the only thing I need is this condition : when App in background and user hit the notification, it opens the APP, but I want the webview to reload.
i think it would be in this part of the code
// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
but what code and I add to reload the view ?
// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: (messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}

Links in iframe are not triggering decidePolicyForNewWindowAction

I have a iframe of youtube player in my webview macos application, and most of the links (the <a> element) inside the iframe are not triggering the decidePolicyForNewWindowAction delegate.
The only working <a> element is the channel link, others like video title, the youtube icon are all silent, and I can't tell the differences between these <a>s.
So why are some links cannot trigger decidePolicyForNewWindowAction?
Documentation of the delegate: https://developer.apple.com/documentation/webkit/webpolicydelegate/1536381-webview?language=objc
Documentation of the iframe youtube player: https://developers.google.com/youtube/iframe_api_reference
For someone maybe interested:
Turns out it's not a iframe problem, it's because some <a> are not directly changing the url, it calls some javascript to do the job for it. This was inspected from the callstack.
In such case, the first thing is that one more delegate needs to be hooked up, the createWebViewWithRequest. This delegate returns nil by default. So change it like this:
- (WebView*)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
return sender;
}
Not doing much but let the following codes continue.
The second thing is that the javascript url request goes to decidePolicyForNavigationAction instead. So still not firing decidePolicyForNewWindowAction but at least there's a chance to know it.
In my case I add some condition inside decidePolicyForNavigationAction to distinguish my request and iframe's request, like this:
- (void) webView: (WebView*) sender decidePolicyForNavigationAction: (NSDictionary*) actionInformation
request: (NSURLRequest*) request
frame: (WebFrame*) frame
decisionListener: (id <WebPolicyDecisionListener>) listener
{
NSString* mainDocumentURL = [[request mainDocumentURL] absoluteString];
NSString* fromYT = #"https://www.youtube.com";
if ([mainDocumentURL hasPrefix:fromYT]) {
[[NSWorkspace sharedWorkspace] openURL:url]; // fires Safari
[listener ignore];
return;
}
// normal flow here, you could open it inside the webview or something
}

Add buttons to dynamic notification interface

I'm creating a custom dynamic notification to show the user. Once i receive this notification at didReceiveNotification function at NotificationController I set the interface outlets with the right data. My problem is that i did not realize how can i add a custom button above the default dismiss button, since the notification storyboard doesnt allow buttons insertion and Apple Documentation says that
Do not include buttons, switches, or other interactive controls.
But i saw a lot of watch applications that have their custom actions, as Messages and Facebook Messenger. There is any way to add custom actions to the dynamic interface at watchOS?
You simply cannot add buttons to Dynamic notification interfaces. If you try to do this, you will receive the error
Illegal Configuration: Buttons are not supported in Notification interfaces.
However, you can add system buttons to your notifications other than the Dismiss button. When setting up the categories for the notification center, you can specify custom UNNotificationActions to be added to your notification category.
var categories = Set<UNNotificationCategory>()
let myCategory = UNNotificationCategory(identifier: "MyCategory", actions: [/*your custom actions go here*/], intentIdentifiers: [], options: []) //set up the actions here
categories.insert(myCategory)
center.setNotificationCategories(categories)
Then you can handle the user interactions with these actions (which as displayed as normal buttons on your dynamic notification interface) in your the UNUserNotificationCenterDelegate method, userNotificationCenter(_:didReceive:withCompletionHandler:) like this:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
switch response.actionIdentifier {
case "Ok":
print("Ok action tapped")
case "Dismiss":
print("Dismiss action tapped")
default:
break
}
completionHandler()
}

How to share a link on Facebook Messenger from other apps?

From Facebook Developer Documentation, I see that I can share image, animated image, even audio clips from other app to Facebook Messenger. But I can see no way to share a link. Neither does when I try to look what kind of share does the FBSDKMessengerShare offer.
How can I share a link using Facebook Messenger?
Seems that it can't be done with FBSDKMessengerShare, but with FBSDKShareKit it's possible. (I'm using Xcode 8.3, Swift 3.1, Cocoapods with FacebookShare 0.2.0)
More info available at
https://developers.facebook.com/docs/sharing/ios
There are two methods:
Using FBSDKMessageDialog:
"The Message Dialog switches to the native Messenger for iOS app, then returns control to your app after a post is published."
#IBAction func messengerButtonAction(_ sender: UIButton) {
let linkContent = FBSDKShareLinkContent()
linkContent.contentURL = URL(string: "https://itunes.apple.com/in/app/someValidAppURL...")
let dialog = FBSDKMessageDialog()
dialog.shareContent = linkContent
dialog.shouldFailOnDataError = true
if dialog.canShow() {
dialog.show()
}
}
Using FBSDKSendButton: "The Send button lets people privately send photos, videos and links to their friends and contacts using the Facebook Messenger. The Send button will call a Message dialog."
This creates a share button that is displayed in a specified view. The button will be automatically disabled if the Messenger app is not installed on the device.
override func viewDidLoad() {
let linkContent = FBSDKShareLinkContent()
linkContent.contentURL = URL(string: "https://itunes.apple.com/in/app/someValidAppURL...")
let button = FBSDKSendButton()
button.shareContent = linkContent
if button.isEnabled {
self.view.addSubview(button)
}
}

Swift 2 Is it possible to add a web link to an annotation callout?

I'm able to create pin's with their popup's as well as the info-light button to the right. And when tapped, the info button pops up an alert with some text and an "ok" button at the bottom. Is it now possible to add a link at the bottom of the text that will direct the user to a URL, saw wikkipedia for example in safari?
I've used the following called function to succesfully do so with a regular button in the mapview, but I can't figure out how to implement it the way I've stated above
#IBAction func webLink(sender: AnyObject) {
if let url = NSURL(string: "http://www.wikipedia.com") {
UIApplication.sharedApplication().openURL(url)
}
}

Resources