Here, i would like to addObserver to check if i got internet connection,
i used reachability-swift 5.0 from https://github.com/ashleymills/Reachability.swift and i use this code
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let reachability = try! Reachability()
NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability?.startNotifier()
print("start notifierrrr")
}catch{
print("could not start reachability notifier")
}
...
}
When i turn on/off my wi-fi, the function didn't called at all
#objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.currentReachabilityStatus {
case .reachableViaWiFi:
print("Reachable via WiFi bbb")
case .reachableViaWWAN:
print("Reachable via WWAN bbb")
case .notReachable:
print("Network not reachable bbb")
}
}
nb: i'm using real device iphone 7 and using ReachabilitySwift (5.0.0)
public convenience init(hostname: String,
queueQoS: DispatchQoS = .default,
targetQueue: DispatchQueue? = nil,
notificationQueue: DispatchQueue? = .main) throws {
guard let ref = SCNetworkReachabilityCreateWithName(nil, hostname) else {
throw ReachabilityError.failedToCreateWithHostname(hostname, SCError())
}
Intialise the reachability with a host name(www.google.com) before start notifier()
Related
I m building a swift application. I m trying to integrate Firebase Notification message. So I write all code as documentation, but if I try to start application after some seconds, I will have this strange error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FBLPromise HTTPBody]: unrecognized selector sent to instance 0x281b9cdb0'
This is my CocoaPods file:
target 'appUser' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'TPKeyboardAvoidingSwift'
pod 'ActiveLabel'
pod 'HSAttachmentPicker'
pod 'IQKeyboardManagerSwift'
pod 'Alamofire', '~> 4.8.0'
pod 'SwiftyJSON'
pod 'DropDown'
pod 'SDWebImage/WebP'
pod 'SVProgressHUD'
pod 'Cosmos'
pod 'Gallery'
pod 'Charts'
pod 'Firebase/Analytics'
pod 'Firebase/Messaging'
# Pods for appUser
#target 'appUserTests' do
# inherit! :search_paths
# Pods for testing
#end
#target 'appUserUITests' do
# Pods for testing
#end
end
This is the code into AppDelegate.swift file
//
import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,MessagingDelegate {
var window: UIWindow?
let notificationCenter = UNUserNotificationCenter.current()
/*func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}*/
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
USER_DEFAULT.set("mykey", forKey: IOS_TOKEN)
notificationCenter.delegate = self
FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().isAutoInitEnabled = true
self.configureNotification()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func configureNotification() {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
}
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")
USER_DEFAULT.set(token, forKey: IOS_TOKEN)
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
// k.iosRegisterId = deviceTokenString
Messaging.messaging().apnsToken = deviceToken
print("APNs device token: \(deviceTokenString)")
// USER_DEFAULT.set(deviceTokenString, forKey: IOS_TOKEN)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("APNs registration failed: \(error)")
}
// MARK:- Received Remote Notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
print("main method - \(userInfo)")
if let info = userInfo as? Dictionary<String, AnyObject> {
//let alert1 = info["aps"]!["alert"] as! Dictionary<String, AnyObject>
let title = userInfo["gcm.notification.status"] as! String
hanleNotification(info: info, strStatus: title, strFrom: "Front")
}
completionHandler(UIBackgroundFetchResult.newData)
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
print("willPresent \(userInfo)")
Messaging.messaging().appDidReceiveMessage(userInfo)
/*if let info = userInfo as? Dictionary<String, AnyObject> {
let title = userInfo["google.c.a.c_l"] as! String
hanleNotification(info: info, strStatus: title, strFrom: "Front")
}*/
completionHandler([[.alert, .sound]])
}
//metodo chiamata quando l'utente clicca sulla notifica
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("Did Recceive \(userInfo)")
if let info = userInfo as? Dictionary<String, AnyObject> {
//let alert1 = info["aps"]!["alert"] as! Dictionary<String, AnyObject>
//let title = userInfo["gcm.notification.data"]
hanleNotification(info: info, strStatus: "titolo", strFrom: "Back")
}
//Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler()
}
/*func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.noData)
}*/
//MARK:GoViewCotroller
func hanleNotification(info:Dictionary<String,AnyObject>,strStatus:String,strFrom:String) {
let visibleVC = UIApplication.topViewController()
//recupero i dati del messaggio
var datiNascosti = info["gcm.notification.data"]
//let swiftyJsonVar = JSON(datiNascosti)
datiNascosti = datiNascosti?.replacingOccurrences(of: "\"", with: "") as AnyObject
datiNascosti = datiNascosti?.replacingOccurrences(of: "{", with: "") as AnyObject
datiNascosti = datiNascosti?.replacingOccurrences(of: "}", with: "") as AnyObject
let array:[String]? = datiNascosti?.components(separatedBy: [","])
var contentOfPushMessage = [String: String]()
for riga in array! {
//ho ogni riga adesso effettuo lo split
let rigaSlittata:[String]? = riga.components(separatedBy: [":"])
if(rigaSlittata!.count > 1){
contentOfPushMessage[rigaSlittata![0]] = rigaSlittata![1]
}
}
print("ho creato il mio dizionario con i dati ottenuti")
//se nel dizionario esiste la chiave status ed il valore = chat
//devo aprire direttamente la chat con l'utente
if contentOfPushMessage.keys.contains("status") {
// contains key
let status = contentOfPushMessage["status"]
if(status == "chat"){
//devo aprire la chat. Mi occorre anche l'id del utente
let user_id = contentOfPushMessage["user_id"]
let username = contentOfPushMessage["username"]
//apro il mio ViewController
guard let window = UIApplication.shared.keyWindow else { return }
//let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
let objVC = kStoryboardMain.instantiateViewController(withIdentifier: "ChatVC") as! ChatVC
objVC.receiverId = user_id!
objVC.userName = username!
let navController = UINavigationController(rootViewController: objVC)
navController.modalPresentationStyle = .fullScreen
// you can assign your vc directly or push it in navigation stack as follows:
window.rootViewController = navController
window.makeKeyAndVisible()
}
} else {
// does not contain key
}
if strStatus == "join_live_session" || strStatus == "exist_session" || strStatus == "join_request" {
// if visibleVC is liveSessionVC {
// NotificationCenter.default.post(name: NSNotification.Name(NotificationCenterStruct.JOIN_SESSION), object: "", userInfo: nil)
// } else {
// //visibleVC?.tabBarController?.selectedIndex = 2
// }
}
}
}
I attach also an image to display Xcode when app still crashed
Hi developers I have this issue with Adobe Audience Manager POD
in my VC I have this
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
ACPAudience.signal(withData: ["ViewedScreen": "ButtonClicked"]) { (response, error) in
if let error = error {
print(error)
} else {
print(response as Any)
}
}
}
every time I run it it crash with this:
dynamic_cast error 2: One or more of the following type_info's has hidden visibility or is defined in more than one translation unit. They should all have public visibility. N20AdobeMarketingMobile6ModuleE, N20AdobeMarketingMobile13ConfigurationE, N20AdobeMarketingMobile22ModuleDetailsInterfaceE.
implementation is correct as far I know
the solution is creating a NSNotification in the app delegate works no crashes or anything weird
code in app delegate is pretty much the same
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
NotificationCenter.default.addObserver(self, selector: #selector(getProfileACP2), name: NSNotification.Name(rawValue: "getProfileACP"), object: nil)
}
#objc private func getProfileACP2(){
ACPAudience.signal(withData: ["ViewedScreen": "ButtonClicked"]) { (response, error) in
if let error = error {
print(error)
} else {
print(response as Any)
}
}
}
then on the VC works like this
override func viewDidLoad() {
super.viewDidLoad()
print("getProfileACP")
print("----------------------------------------------")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "getProfileACP"), object: nil)
print("----------------------------------------------")
print("getProfileACP")
}
after calling it seems like it takes some time after giving the response is not immediately
I can't seem to resolve this issue. I went in and deleted all parse and fb SDKs and then downloaded the newest parse and fb SDKs and put into project.
Still getting this error in logs. Don't even get to fb screen. Below is my code. I have researched all instances of this issue and yet no resolve. I am missing something.
AppDelegate.swift:
import UIKit
import Parse
import ParseFacebookUtilsV4
import Bolts
// If you want to use any of the UI components, uncomment this line
// import ParseUI
// If you want to use Crash Reporting - uncomment this line
// import ParseCrashReporting
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//--------------------------------------
// MARK: - UIApplicationDelegate
//--------------------------------------
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Enable storing and querying data from Local Datastore.
// Remove this line if you don't want to use Local Datastore features or want to use cachePolicy.
Parse.enableLocalDatastore()
let parseConfiguration = ParseClientConfiguration(block: { (ParseMutableClientConfiguration) -> Void in
ParseMutableClientConfiguration.applicationId = "xxx"
ParseMutableClientConfiguration.clientKey = "xxx"
ParseMutableClientConfiguration.server = "https://yourapp.herokuapp.com/parse"
})
Parse.initializeWithConfiguration(parseConfiguration)
PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions)
PFUser.enableAutomaticUser()
let defaultACL = PFACL();
// If you would like all objects to be private by default, remove this line.
defaultACL.publicReadAccess = true
PFACL.setDefaultACL(defaultACL, withAccessForCurrentUser:true)
if application.applicationState != UIApplicationState.Background {
// Track an app open here if we launch with a push, unless
// "content_available" was used to trigger a background push (introduced in iOS 7).
// In that case, we skip tracking here to avoid double counting the app-open.
let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
var noPushPayload = false;
if let options = launchOptions {
noPushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil;
}
if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
}
}
if application.respondsToSelector("registerUserNotificationSettings:") {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
let types: UIRemoteNotificationType = [UIRemoteNotificationType.Badge, UIRemoteNotificationType.Alert, UIRemoteNotificationType.Sound]
application.registerForRemoteNotificationTypes(types)
}
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
//--------------------------------------
// MARK: Push Notifications
//--------------------------------------
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
PFPush.subscribeToChannelInBackground("") { (succeeded, error) in
if succeeded {
print("ParseStarterProject successfully subscribed to push notifications on the broadcast channel.");
} else {
print("ParseStarterProject failed to subscribe to push notifications on the broadcast channel with error = %#.", error)
}
}
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
if error.code == 3010 {
print("Push notifications are not supported in the iOS Simulator.")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %#", error)
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
}
func application(application: UIApplication,
openURL url: NSURL,
sourceApplication: String?,
annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
func applicationDidBecomeActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
}
}
ViewController.swift:
import UIKit
import Parse
import FBSDKCoreKit
import FBSDKLoginKit
import ParseFacebookUtilsV4
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// array of what we want to get from the fb user account
let permissions = ["public_profile"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
(user: PFUser?, error: NSError?) -> Void in
if let error = error { // if error exists
print(error)
} else {
if let user = user { // if user exists
print(user)
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
How to check push notification before showing?
I want check data and if incorrect data I don't want show notification
my code:
I just use simple Parse.com example. This is my AppDelegate class
let defaults = NSUserDefaults.standardUserDefaults()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("//",
clientKey: "//")
if application.applicationState != UIApplicationState.Background {
let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
var pushPayload = false
if let options = launchOptions {
pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
}
if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
}
}
var notificationActionAccept :UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionAccept.identifier = "FIRST_ACTION"
notificationActionAccept.title = "Įvertinti"
var notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "CallNotificationCategory"
notificationCategory .setActions([notificationActionAccept], forContext: UIUserNotificationActionContext.Default)
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: NSSet(array:[notificationCategory]) as! Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
if let launchOptions = launchOptions as? [String : AnyObject] {
if let notificationDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
self.application(application, didReceiveRemoteNotification: notificationDictionary)
}
}
return
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
return
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
if error.code == 3010 {
print("Push notifications are not supported in the iOS Simulator.")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %#", error)
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
}
}
I Realy need help:/
No, unfortunately you can not stop notification from showing when your app is in background.
application:didReceiveRemoteNotification: is just a delegate method, which is invoked upon notification showing, it can not control if notification should be shown or not.
I have imported Reachability.h .m files like written here into my Swift project, but observer / event handler will not get called after start, why?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)
let hostReachability = Reachability(hostName: "www.apple.com");
hostReachability.startNotifier();
return true
}
func reachabilityChanged(note: NSNotification) { // <- DOES NOT GET CALLED
let reachability: Reachability = note.object as Reachability;
if(reachability.currentReachabilityStatus() != .NotReachable) {
}
}
The Reachability object returned is an autoreleased object , so it has deallocated hence notification is not triggering . You can fix by keeping a strong reference to it.
create a property to hold Reachability object
var reachability:Reachability?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name:kReachabilityChangedNotification, object: nil)
reachability = Reachability(hostName: "www.apple.com");
reachability?.startNotifier();
return true
}
This will fix your problem.
The observer's name is changed from ReachabilityChangedNotification to kReachabilityChangedNotification. Notice the "k"
As mentioned in other answers, you need to maintain a strong reference to the Reachability object. If you aren't checking a particular host, then call reachabilityForInternetConnection:
let internetReachability = Reachability.reachabilityForInternetConnection()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: internetReachability)
internetReachability.startNotifier()
let hasInternet = internetReachability.currentReachabilityStatus().value == 0 ? false : true
// Save to user defaults, log to console, etc.
return true
}
func reachabilityChanged(note: NSNotification) {
var hasInternet = Bool()
if let reachability = note.object as? Reachability {
hasInternet = internetReachability.currentReachabilityStatus().value == 0 ? false : true
}
// Update user defaults, log to console, show error message, etc.
}
For iOS 3+, use below code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
NotificationCenter.default.addObserver(self, selector: #selector(checkForReachability(notification:)), name: NSNotification.Name.reachabilityChanged, object: nil)
reachability = Reachability.forInternetConnection()
reachability.startNotifier()
return true
}
// MARK:
// MARK: Check Internet Status
func checkForReachability(notification:NSNotification)
{
let networkReachability = notification.object as! Reachability;
let remoteHostStatus = networkReachability.currentReachabilityStatus()
if (remoteHostStatus == NotReachable)
{
print("Not Reachable")
}
else
{
print("Reachable")
}
}