Define global variable with Alamofire + SwiftyJSON - swift3

I'm using Alamofire for requests and i'm using swiftyjson for json parsing.
I need define global variables for other view controllers.
I have this code:
import UIKit
import Alamofire
import SwiftyJSON
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
struct Settings {
static var registration_url = String();
static var login_url = String();
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Alamofire.request("http://localhost/settings.php").responseJSON { response in
if response.result.isSuccess {
let json = JSON(data: response.data!);
Settings.registration_url = json["registration_url"].stringValue;
Settings.login_url = json["login_url"].stringValue;
} else {
Settings.registration_url = "http://localhost/register.php";
Settings.login_url = "http://localhost/login.php";
}
}
print(Settings.registration_url);
print(Settings.login_url)
return true
}
}
I'm checking debug window, and print(Settings.registration_url); is looks blank
Why?
Thanks. Sorry for my poor english.

Your Settings structure is not global. You need to define it outside the class for it to be global:
struct Settings {
static var registration_url = String();
static var login_url = String();
}
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Alamofire.request("http://localhost/settings.php").responseJSON { response in
if response.result.isSuccess {
let json = JSON(data: response.data!);
Settings.registration_url = json["registration_url"].stringValue;
Settings.login_url = json["login_url"].stringValue;
} else {
Settings.registration_url = "http://localhost/register.php";
Settings.login_url = "http://localhost/login.php";
}
}
print(Settings.registration_url);
print(Settings.login_url)
return true
}
}

Related

How Can I Solve 'Notifications not accepted. You can turn them on later under your iOS settings' Issue?

I'm using onesignal for push notifications and it was working well until the ios 14 upgrade. When I build the same app without any changes on Xcode 12, I got this warning in the console.
Notifications not accepted. You can turn them on later under your iOS
settings
There was no problem on iOS 13, it happened when I update to iOS 14.
AppDelegate.swift
import UIKit
import CoreData
import Firebase
import GoogleMobileAds
import OneSignal
import UserNotifications
import SDWebImageWebPCoder
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, OSPermissionObserver, OSSubscriptionObserver {
var window: UIWindow?
var shortcutItemToProcess: UIApplicationShortcutItem?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
GADMobileAds.sharedInstance().start(completionHandler: nil)
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false, kOSSettingsKeyInAppLaunchURL: false]
OneSignal.initWithLaunchOptions(launchOptions,
appId: "my key is here",
handleNotificationAction: nil,
settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
})
// Add your AppDelegate as an obsserver
OneSignal.add(self as OSPermissionObserver)
OneSignal.add(self as OSSubscriptionObserver)
registerForPushNotifications()
let WebPCoder = SDImageWebPCoder.shared
SDImageCodersManager.shared.addCoder(WebPCoder)
return true
}
func registerForPushNotifications() {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
let readAction = UNNotificationAction(identifier: "oku", title: "Haberi Oku", options: [.foreground])
let closeAction = UNNotificationAction(identifier: "kapat", title: "Kapat", options: [])
let category = UNNotificationCategory(identifier: "etkilesim", actions: [readAction, closeAction], intentIdentifiers: [], options: [])
notificationCenter.setNotificationCategories([category])
}
func onOSPermissionChanged(_ stateChanges: OSPermissionStateChanges!) {
if stateChanges.from.status == OSNotificationPermission.notDetermined {
if stateChanges.to.status == OSNotificationPermission.authorized {
print("Thanks for accepting notifications!")
} else if stateChanges.to.status == OSNotificationPermission.denied {
print("Notifications not accepted. You can turn them on later under your iOS settings.")
}
}
}
func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
if !stateChanges.from.subscribed && stateChanges.to.subscribed {
print("Subscribed for OneSignal push notifications!")
}
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
// Grab a reference to the shortcutItem to use in the scene
if let shortcutItem = options.shortcutItem {
shortcutItemToProcess = shortcutItem
}
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
var postId:String = ""
var postType:String = ""
if let custom = response.notification.request.content.userInfo["custom"] as? NSDictionary{
if let a = custom["a"] as? NSDictionary{
if let id = a["id"] as? String{
postId = id
}
if let type = a["rights"] as? String{
postType = type
}
}
}
if response.actionIdentifier == "oku" {
if postId != ""{
DispatchQueue.main.async(execute: {
NotificationCenter.default.post(name: NSNotification.Name("Detail"), object: nil, userInfo: ["id": postId, "type": postType])
})
completionHandler()
}
}else if response.actionIdentifier == "kapat" {
print("KAPAT")
completionHandler()
} else {
if postId != ""{
DispatchQueue.main.async(execute: {
NotificationCenter.default.post(name: NSNotification.Name("Detail"), object: nil, userInfo: ["id": postId, "type": postType])
})
completionHandler()
}
}
}
}
I solved it! :) If your app name contains non-English characters, change your Product Name under Build Settings and build it again, that's it :)
Settings
Then you can change 'Bundle display name' in info.plist.

facebook login with swift 3

please help me with Facebook login
I've started with putting into pod
pod 'FacebookCore'
pod 'FacebookLogin'
pod 'FacebookShare'
and then run pod install
after this i've imported this into my viewController
import UIKit
import FacebookCore
import FacebookLogin
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let loginButton = LoginButton(readPermissions: [ .publicProfile ])
loginButton.center = view.center
view.addSubview(loginButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I updated info.plist with Facebook data
how Do i get user basic profile data once user has authorized app?
What goes in AppDelegate file?
thanks
Here is the code for login the Facebook by a UIButton click.
Step 1: Copy this code into your class file:
import UIKit
import FBSDKShareKit
import FBSDKLoginKit
import FBSDKCoreKit
class Facebook_loginPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
}
#IBAction func login(_ sender: Any) {
self.facebooklogin()
}
func facebooklogin() {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email"], handler: { (result, error) -> Void in
print("\n\n result: \(result)")
print("\n\n Error: \(error)")
if (error == nil) {
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if(fbloginresult.isCancelled) {
//Show Cancel alert
} else if(fbloginresult.grantedPermissions.contains("email")) {
self.returnUserData()
//fbLoginManager.logOut()
}
}
})
}
func returnUserData() {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil) {
// Process error
print("\n\n Error: \(error)")
} else {
let resultDic = result as! NSDictionary
print("\n\n fetched user: \(result)")
if (resultDic.value(forKey:"name") != nil) {
let userName = resultDic.value(forKey:"name")! as! String as NSString?
print("\n User Name is: \(userName)")
}
if (resultDic.value(forKey:"email") != nil) {
let userEmail = resultDic.value(forKey:"email")! as! String as NSString?
print("\n User Email is: \(userEmail)")
}
}
})
}
}
Step 2: In AppDelegate add the id of Facebook:
import UIKit
import FBSDKShareKit
import FBSDKLoginKit
import FBSDKCoreKit
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let str_URL = url.absoluteString as NSString
if str_URL.contains("fb1111111111111111")// put you id here {
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
}
Updated Code :App Delegate
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let str_URL = url.absoluteString as NSString
if str_URL.contains("fb111111111111111"){
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url as URL?,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation])
}
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url as URL?,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation])
}
}
Step 3: put this code into `info.plist'
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb1111111111111111</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>2</string>
<key>FacebookAppID</key>
<string>1111111111111111</string>
<key>FacebookDisplayName</key>
<string>Together Deal</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
Here is the code of
1. ViewController.swift
import UIKit
import FacebookLogin
import FBSDKLoginKit
class ViewController: UIViewController {
var dict : [String : AnyObject]!
override func viewDidLoad() {
super.viewDidLoad()
//creating button
let loginButton = LoginButton(readPermissions: [ .publicProfile ])
loginButton.center = view.center
//adding it to view
view.addSubview(loginButton)
//if the user is already logged in
if let accessToken = FBSDKAccessToken.current(){
getFBUserData()
}
}
//when login button clicked
#objc func loginButtonClicked() {
let loginManager = LoginManager()
loginManager.logIn([ .publicProfile ], viewController: self) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
self.getFBUserData()
}
}
}
//function is fetching the user data
func getFBUserData(){
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
self.dict = result as! [String : AnyObject]
print(result!)
print(self.dict)
}
})
}
}
}
2. AppDelegate.swift
import UIKit
import FBSDKLoginKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//added these 3 methods
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func applicationWillResignActive(_ application: UIApplication) {
FBSDKAppEvents.activateApp()
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
}
Source: Facebook Login Swift 3 Tutorial
Hope this video be helpfull for you
https://youtu.be/bOS4jqRc8Io
Contents in this Video:
Facebook SDK integration tutorial with Swift lastest 2018
Xcode new project setup
Swift iOS app login with Facebook
Tutorial Facebook login feature in iOS app
Add Facebook to Swift iOS app
Facebook login in iOS app
Steps:
Create new simple Swift Xcode project
Login to facebook develop page to create the same name APP
Add login with Facebook button, login and logout methods
Modify the configure plist, create bridging header for C, configure AppDelegate,
Run the application and test with simulator
The following are the steps to follow for facebook login in ios app.
1. Create or open an application from Xcode.
2. Create an application from your facebook account and do required configuration by going through the app settings.
3. Install required pods in your project.
4. Do required configuration in your Appdelegate
5. We need to modify plist with your facebook app id that you can get from your app by logging in facebook
6. Design UI as per your requirement.
I have created an article about Facebook login implementation in iOS app using Swift 5 in detail.
You can find clear explanation here Facebook login using Swift5

How to create singleton for the WebSocket (Starscream)?

I am trying to create a singleton with WebSocket functionality and I just don't know why it's not working
This is my singleton extension:
import Starscream
extension WebSocketManager: WebSocketDelegate {
func websocketDidConnect(socket: WebSocket) {
print("websocket is connected")
}
func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
if let e = error {
print("websocket is disconnected: \(e.localizedDescription)")
} else {
print("websocket disconnected")
}
}
func websocketDidReceiveMessage(socket: WebSocket, text: String) {
print("Received text: \(text)")
}
func websocketDidReceiveData(socket: WebSocket, data: Data) {
print("Received data: \(data.count)")
}}
My singleton:
final class WebSocketManager: NSObject {
static let sharedInstance = WebSocketManager()
public var socket = WebSocket(url: URL(string: "ws://t-w-a.herokuapp.com/new")!)
private override init() {
super.init()
// testing connection of the socket
}
func establishConnection() {
socket.connect()
}
func closeConnection() {
socket.disconnect()
}}
When I am trying to connect the server just nothing happens.
Here is my controller:
class ChatViewController: UIViewController {
#IBOutlet weak var typeText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
WebSocketManager.sharedInstance.establishConnection()
}}
I've found problem, I just forgot to assign socket variable with the delegate

requirejs + django_select2

I'm building a wagtail / django app using requirejs as js assets combiner, for the site front end.
I'm using it because I've ever been in a kind of JS dependencies hell, where nothing works because of multiple versions of same libs loaded, from different django apps... (I don't even know if it is a good solution)
I've to tell that I'm not a JS expert, and I've none arround me :(
I'm using the good old templates to render the pages, not using angular, react, riot nor vue : I'm a pretty old school dev :)
I've already adapted some scripts to use require, but I'm stuck for now...
I've installed the django_select2 application, and I'm trying to adapt the django_select2.js asset.
I've loaded select2 through bower, and I've updaetd my config.js:
"shim": {
select2: {
deps: ["jquery"],
exports: "$.fn.select2"
}
},
paths: {
...
select2: "select2/dist/js/select2"
}
Then I'm trying to adapt the django_select2.js:
require(['jquery', 'select2'], function ($, select2) {
return (function ($) {
var init = function ($element, options) {
$element.select2(options);
};
var initHeavy = function ($element, options) {
var settings = $.extend({
ajax: {
data: function (params) {
var result = {
term: params.term,
page: params.page,
field_id: $element.data('field_id')
}
var dependentFields = $element.data('select2-dependent-fields')
if (dependentFields) {
dependentFields = dependentFields.trim().split(/\s+/)
$.each(dependentFields, function (i, dependentField) {
result[dependentField] = $('[name=' + dependentField + ']', $element.closest('form')).val()
})
}
return result
},
processResults: function (data, page) {
return {
results: data.results,
pagination: {
more: data.more
}
}
}
}
}, options);
$element.select2(settings);
};
$.fn.djangoSelect2 = function (options) {
var settings = $.extend({}, options);
$.each(this, function (i, element) {
var $element = $(element);
if ($element.hasClass('django-select2-heavy')) {
initHeavy($element, settings);
} else {
init($element, settings);
}
});
return this;
};
$(function () {
$('.django-select2').djangoSelect2();
});
}($));
});
I'm having a Mismatched anonymous define() when running my page in the browser...
I'me realy not a JS expert, I'm coding by trial and error... Could anyone help me with this ?
Thanks !
OK, I have an auto-response...
I've inherited the mixin:
class _Select2Mixin(Select2Mixin):
def _get_media(self):
"""
Construct Media as a dynamic property.
.. Note:: For more information visit
https://docs.djangoproject.com/en/1.8/topics/forms/media/#media-as-a-dynamic-property
"""
return forms.Media(js=('django_select2/django_select2.js', ),
css={'screen': (settings.SELECT2_CSS,)})
media = property(_get_media)
class _Select2MultipleWidget(_Select2Mixin, forms.SelectMultiple):
pass
Then I can use the widget:
class DocumentationSearchForm(forms.Form):
...
document_domains = forms.ModelMultipleChoiceField(required=False,
label=_('Document domains'),
queryset=NotImplemented,
widget=_Select2MultipleWidget)
I've set my config.js file for path:
requirejs.config({
paths: {
jquery: 'jquery/dist/jquery',
select2: "select2/dist/js"
},
shim: {
"select2/select2": {
deps: ["jquery"],
exports: "$.fn.select2"
}
}
});
Then I've overridden the django_select2.js file, to wrap the content in a require satement:
require(['jquery', 'select2/select2'], function ($) {
(function ($) {
var init = function ($element, options) {
$element.select2(options);
};
var initHeavy = function ($element, options) {
var settings = $.extend({
ajax: {
data: function (params) {
var result = {
term: params.term,
page: params.page,
field_id: $element.data('field_id')
}
var dependentFields = $element.data('select2-dependent-fields')
if (dependentFields) {
dependentFields = dependentFields.trim().split(/\s+/)
$.each(dependentFields, function (i, dependentField) {
result[dependentField] = $('[name=' + dependentField + ']', $element.closest('form')).val()
})
}
return result
},
processResults: function (data, page) {
return {
results: data.results,
pagination: {
more: data.more
}
}
}
}
}, options);
$element.select2(settings);
};
$.fn.djangoSelect2 = function (options) {
var settings = $.extend({}, options);
$.each(this, function (i, element) {
var $element = $(element);
if ($element.hasClass('django-select2-heavy')) {
initHeavy($element, settings);
} else {
init($element, settings);
}
});
return this;
};
$(function () {
$('.django-select2').djangoSelect2();
});
}($));
});
That's all, folks !

ionic2 ModalController can not show in websocket

import { Component } from '#angular/core';
import { NavController, Events, ModalController } from 'ionic-angular';
import * as SockJS from 'sockjs-client';
import { SomePage } from './somepage';
export class DashboardPage {
sockjs: any;
constructor(public navCtrl: NavController,
public events: Events,
private modalCtrl: ModalController) {
this.initWebsocket();
}
initWebsocket() {
this.sockjs = new SockJS('http://192.168.0.141:8080/ws-driver');
this.sockjs.onopen = function () {
console.log('open');
};
this.sockjs.onmessage = function (e) {
console.log('message', e.data);
alert('ok');
let model = this.modalCtrl.create(SomePage);
model.present();
};
this.sockjs.onclose = function () {
console.log('close');
};
}
}
alert('ok') had been process.
thanks
Use arrow functions instead of function keyword as this within the function refers to function object.
Try:
this.sockjs.onmessage = (e)=> {
console.log('message', e.data);
alert('ok');
let model = this.modalCtrl.create(SomePage);
model.present();
};
Or set self=this;
let self = this;
this.sockjs.onmessage = function (e) {
console.log('message', e.data);
alert('ok');
let model = self.modalCtrl.create(SomePage);
model.present();
};