Redirect to specific view - uiviewcontroller

I want to redirect the user of my app to a specific view if he is logged in or not. This is my code:
override func viewDidLoad() {
super.viewDidLoad()
if FIRAuth.auth()?.currentUser != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"LoginViewController") as! LoginViewController
self.present(viewController, animated: true)
}
else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"SWRevealViewController") as! SWRevealViewController
self.present(viewController, animated: true)
}
}
If I open my app I always get a white screen.
Does anyone know my mistake?

It worked. I didn't connected the navigation controller in the storyboard. The code itself works finde.

Related

Share JSON Data in TabBarController to view controllers

I am using a tabbarcontroller to show 3 xib's. I would like to decode JSON data in the UITabBarController subclass, and then share the data with the view controllers (as I understand that is the preferred way to do this). I had already successfully accomplished this individually in each view controller, where the same JSON data was getting decoded separately 3 times, but I am now trying to make the process more efficient by only dealing with JSON once.
I am currently getting the following error
"Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee7ab7d98)".
Below is the code I am currently using. I'm mostly only including the code for the first view controller, but it is the same for the others
Here is one of the view controllers. Any help would be appreciated, thank you!
class FirstCollectionViewController: UIViewController {
var tbvc = CustomTabBar()
var statisticsData = [Model]()
let firstCellIdentifier = "FirstCellIdentifier"
#IBOutlet weak var FirstCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
FirstCollectionView.delegate = self
FirstCollectionView.dataSource = self
FirstCollectionView.register(UINib(nibName: "FirstCollectionViewCell", bundle: nil),forCellWithReuseIdentifier: firstCellIdentifier)
}
}
Here is the subclasses UITabBarController
import UIKit
class CustomTabBar: UITabBarController {
let website = "https:......."
var statisticsData = [Model]()
override func viewDidLoad() {
super.viewDidLoad()
let firstTab = FirstCollectionViewController(nibName: "FirstCollectionViewController", bundle: nil)
let secondTab = SecondCollectionViewController(nibName: "SecondCollectionViewController", bundle: nil)
let thirdTab = ThirdCollectionViewController(nibName: "ThirdCollectionViewController", bundle: nil)
viewControllers = [firstTab, secondTab, thirdTab]
downloadJSON(website: website) {
firstTab.statisticsData = self.statisticsData
secondTab.statisticsData = self.statisticsData
thirdTab.statisticsData = self.statisticsData
firstTab.FirstCollectionView.reloadData()
secondTab.SecondCollectionView.reloadData()
thirdTab.ThirdCollectionView.reloadData()
}
}
func downloadJSON(website:String, completed:#escaping ()->()){
guard let qurl = URL(string: website) else { return }
URLSession.shared.dataTask(with: qurl) { (data, response, error) in
if error == nil {
do{
self.statisticsData = try JSONDecoder().decode([Model].self, from: data!)
DispatchQueue.main.async{
completed()
}
} catch {
print("JSON Error")
}}
}.resume()
}
}
Once the data is loaded, you should assign the data to the viewControllers that are added in the tabBarController's Child list as below,
downloadJSON(website: website) {
firstTab.statisticsData = self.statisticsData
secondTab.statisticsData = self.statisticsData
thirdTab.statisticsData = self.statisticsData
firstTab.FirstCollectionView.reloadData()
secondTab.SecondCollectionView.reloadData()
thirdTab.ThirdCollectionView.reloadData()
}
You can also remove the below lines from viewDidLoad of FirstCollectionViewController, SecondCollectionViewController and ThirdCollectionViewController
tbvc = tabBarController as! CustomTabBar
statisticsData = tbvc.statisticsData

iOS share pdf with webView

I have a problem with webView. I completely load the content of webView:
func openUrl(fileURL: URL, htmlString: String) {
webView.loadHTMLString(htmlString, baseURL: fileURL)
}
but then I want to share content.
When I tried:
let activityController = UIActivityViewController(activityItems: [webView.request?.url], applicationActivities: nil)
let popover = activityController.popoverPresentationController
popover?.sourceView = self.view
popover?.barButtonItem = self.navigationItem.rightBarButtonItem
present(activityController, animated: true, completion: nil)
It didn't work, there wasn't image what i load from function loadHTMLString
How can i share it?
Does anybody know how this problem solve?

How to send data from AppDelegate to my subclass of ViewController in swift 4

I am using Swift 4.1 and I am wondering how to pass the url data that is given here:
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
}
into my subclass of UIViewController I called HotSpotRISViewController. Here is what I attempted:
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let vc = HotSpotRISViewController()
vc.setURL(theURL: url)
let navigationController = UINavigationController(rootViewController: vc)
self.window!.rootViewController = navigationController
}
My HotSpotRISViewController contains this function:
func setURL(theURL: URL) {
self.url = theURL
print(self.url ?? "nil")
}
Inside my HotSpotRISViewController I have a property of type URL ready to be set. The above code correctly passes the info, because my print statement prints out the URL, but I get a black screen instead of my app. What am I missing? And then on the other hand, the following makes the app start up correctly but I have no idea how to pass the url data using this method:
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = mainStoryboard.instantiateInitialViewController()
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
I'm wondering how I can get both my app started up correctly without a black screen and pass the url data. Thank you.
You can add a global variable in your appDelegate file and fetch it with your setURL function :
In your AppDelegate.swift
var myurl: URL!
....
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
self.myurl = url
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = mainStoryboard.instantiateInitialViewController()
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
...
In your HotSpotRISViewController.swift
func setURL(theURL: URL) {
DispatchQueue.main.async {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
self.url = appDelegate.myurl
}
}

swift: how to pass JSON to secondViewController

I want to pass the JSON data to MainMenuPageViewController.
MainMenuPageViewController UILabel(UsernameLabel)
Thank you
Picture1:
let jsonUserId: String = json["return"] as! String
if (jsonUserId != "0") {
print("username and password correct")
dispatch_async(dispatch_get_main_queue(), {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenuPageViewController")
self.showViewController(vc, sender: self)
//pass the jsonUserId to MainMenuPageViewController
//MainMenuPageViewController has UILabel(UserIdLabel)
})
}
If I input wrong username and password, system will run to line 57 but the alert will error, when I input long String(a,A,#,etc.). However, if I input shot String(shing, herry,123,etc.) app can display the alert message.
Also, if input the space bar and (!##$%^&*()_+). it will error.
Can you help me to fix this error? Thank you.
Just declare variable in MainMenuPageViewController like
class MainMenuPageViewController: UIViewController {
var UserIdLabel = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
and for pass data
let jsonUserId: String = json["return"] as! String
if (jsonUserId != "0") {
print("username and password correct")
dispatch_async(dispatch_get_main_queue(), {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenuPageViewController") as? MainMenuPageViewController {
vc.UserIdLabel = jsonUserId
self.showViewController(vc, sender: self)
}
})
}
You should create a property in the MainMenuPageViewController to store the user id and then in viewDidLoad set the UsernameLabel text to the stored property.
var userID:String! //define this in the MainMenuPageViewController
Then after you create the vc set the userID property to the jsonUserID
if let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenuPageViewController") as? MainMenuPageViewController {
vc.userID = jsonUserId
self.showViewController(vc, sender: self)
} else {
printf("The VC is not of the right type")
}
override func viewDidLoad() {
super.viewDidLoad()
UsernameLabel.text = userID
}

Custom URL scheme for Pinterest on iOS Safari to open board URL?

I can't for the life of me find out how to simply open a Pinterest board (by launching the APP if it's installed) through an anchor within HTML.
I've taken a look at: https://developers.pinterest.com, but still can't find what i'm looking for. It seems most of the documentation out there is geared more towards the action of pinning an item, rather than viewing.
All I want to do is open a users profile. For example, i've dug up the following which works great for alternative social media links:
Facebook
Twitter
Instagram
While I'm able to launch the Pinterest app, I'm unsure what the parameters would be passed to open a specific profile / board on Pinterest:
Pinterest
Thanks to: http://handleopenurl.com/ for help with the above, but it seems Pinterest is absent. Any clues?
Looks like Pinterest has published an iOS SDK, and defined some URLS:
Pin
pinterest://pin/285063851385287883/
Board
pinterest://board/meaghanror/cats-cats-cats/
User
pinterest://user/carlrice/
See more at https://developers.pinterest.com/ios/
Working Code for Swift 4.x
func openSocialMedia(appURI : String, webURI : String){
let appURL = NSURL(string: appURI)!
let webURL = NSURL(string: webURI)!
if UIApplication.shared.canOpenURL(appURL as URL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(appURL as URL)
}
} else {
//redirect to safari because the user doesn't have Instagram
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(webURL as URL)
}
}
}
How to call
#IBAction func didPressInstagram(_ sender: Any) {
let appHandle = "instagram://user?username=erashukr"
let webHandle = "https://instagram.com/erashukr"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
#IBAction func didPressGplus(_ sender: UIButton) {
let appHandle = "gplus://plus.google.com/u/0/+Ashutoshkumarrr"
let webHandle = "https://plus.google.com/u/0/+Ashutoshkumarrr"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
#IBAction func didPressFacebook(_ sender: Any) {
let appHandle = "fb://profile?id=erashukr"
let webHandle = "https://facebook.com/erashukr"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
#IBAction func didPressTwitter(_ sender: UIButton) {
let appHandle = "twitter://user?screen_name=ace_ashu"
let webHandle = "https://twitter.com/ace_ashu"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
#IBAction func didPressPinterest(_ sender: UIButton) {
let appHandle = "pinterest://user/apple"
let webHandle = "https://www.pinterest.com/apple/"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
100% Working