Swift 3: Ambiguous reference to member 'subscript' while using UberRides SDK - ios

I'm currently developing an iOS application using Swift 3 and the UberRides SDK. According to their docs, the AppDelegate class needs to be modified to contain the following:
UberRides SDK AppDelegate information
I did so, and here's the code I have:
import UIKit
import UberRides
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Handle incoming SSO Requests
RidesAppDelegate.sharedInstance.application(application, didFinishLaunchingWithOptions: launchOptions)
// Other logic
return true
}
#available(iOS 9, *)
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
let handledURL = RidesAppDelegate.sharedInstance.application(app, openURL: url as URL, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
if (!handledURL) {
// Other URL parsing logic
}
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
let handledURL = RidesAppDelegate.sharedInstance.application(application, openURL: url as URL, sourceApplication: sourceApplication, annotation: annotation)
if (!handledURL) {
// Other URL parsing logic
}
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:.
}
}
However, I am getting the error: "Ambiguous reference to member 'subscript'" in this function, specifically at "options[UIApplicationOpenURLOptionsSourceApplicationKey]":
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
let handledURL = RidesAppDelegate.sharedInstance.application(app, openURL: url as URL, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
if (!handledURL) {
// Other URL parsing logic
}
return true
}
I am very new to Swift, and am not really sure as to how to go about solving this issue! Any help would be appreciated. Thanks!

Related

Facebook Login Button Termination in xcode 9

I am using Xcode 9 and i set the compiler to Swift 3.2
I installed Facebook Login with Cocoapods and everything compiles without any error but whenever i run it in the simulator i click on the login button it crashes with only this printed in the console:
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
and this is my ViewController.swift:
import UIKit
import FBSDKLoginKit
import FacebookLogin
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
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.
}
}
And this is my AppDelegate.swift
import UIKit
import FBSDKCoreKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options [UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
return handled
}
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:.
}
Simulator
4
In your AppDelegate, didFinishLaunchingWithOptions, you have to return the boolean returned from the sharedIstance.application(....):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let bool = FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
FBSDKAppEvents.activateApp()
return bool
}
After check your Info.plistyou should have something like this. FacebookAppID and FacebookDisplayName are required:

Facebook login page is empty and will not load

I implemented all of the macbook login steps from their website. And when I click my unbutton to present the Facebook login page, the Facebook safari login page will not load, or even attempt to load and is essentially blank. Here is my code. Can someone provide some edit or advice on what I should do to solve this problem. Here is my appdelagate. In my info.plist I've included the FacebookAppID, lsApplicationQuiersSchemes, Facebook display name. Ive also put my bundle identifier in on face books website. Is there anything I'm missing. When I press my Facebook button it presents a blank safari page.
import FBSDKCoreKit
import FBSDKLoginKit
import ParseFacebookUtilsV4
import CoreData
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerTransitioningDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions)
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
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 throttle down OpenGL ES frame rates. 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 inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
FBSDKAppEvents.activateApp()
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
private func setupGlidingCollection() {
var config = GlidingConfig.shared
config.buttonsFont = UIFont.boldSystemFont(ofSize: 22)
config.inactiveButtonsColor = config.activeButtonColor
GlidingConfig.shared = config
}
}
Check your console, it should give an error.

facebookSDK, Swift3 and AppDelegate to handle logins

Following the docs here
I've implemented the login button fine, the app passes off to the fblogin completes fine, however after i log in i get passed back to the app and the console always reads
"User cancelled Login"
Some searching around the web shows that i need to add something to AppDelegate.Swift however the docs only have code for Objective-C projects.
there is this guy's guide however i think it's written for swift 1.x
altering it slightly catered for the dropping of NSURL i'm using:
import UIKit
import FBSDKCoreKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(application: UIApplication,
open url: URL,
sourceApplication: String?,
annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
application,
openURL: url,
sourceApplication: sourceApplication,
annotation: annotation)
}
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.
FBSDKAppEvents.activateApp()
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
But i am getting the error 'application(:openURL:sourceApplication:annotation:)' has been renamed to 'application(:open:sourceApplication:annotation:)'
Anyone have any ideas how to get this delegation to work and getting the right stuff back from a Facebook login?
You need to make changes in appdelegate as below :
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
with FacebookCore.ApplicationDelegate (that I hope it will be named FBApplicationDelegate) as it follow
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
return FacebookCore.ApplicationDelegate.shared.application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey] ?? [])
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FacebookCore.ApplicationDelegate.shared.application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
refer link for more : https://github.com/facebook/facebook-sdk-swift/issues/35

FB Login using Swift 3 not returning any values and not get back the user to the App after successful login

I'm using iOS 10 Swift 3 to integrate FB Login. I have followed all steps from Facebook documentation. Now the issue is, after successful login it doesn't returning any values and not get back the user to the app.
Note: The same works perfectly in Swift 2.
Hi I raised this issue to facebook developer support and the issue is that I have not implemented the correct delegate methods for iOS10. and you can find the correct delegate implementation here
import UIKit
import CoreData
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation]
)
}
public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
application,
open: url as URL!,
sourceApplication: sourceApplication,
annotation: annotation)
}
}
Don't forget to set Keychain Sharing
project targets -> Capabilities -> Keychain Sharing -> Toggle Switch ON
you can follow this
http://ashishkakkad.com/2015/05/facebook-login-swift-language-ios/
for me the "FBSDKApplicationDelegate" doesn't work for the latest Facebook Swift SDK version v0.2.0
You can find my implementation for v0.2.2 here: https://gist.github.com/mbecker/bdbd28cd11394085c01cf442aaa42c72
//
// AppDelegate.swift
//
// Created by Mats Becker on 9/25/16.
// Copyright © 2016 safari.digital. All rights reserved.
//
import UIKit
import FacebookCore
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}
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.
// Call the 'activate' method to log an app event for use
// in analytics and advertising reporting.
AppEventsLogger.activate(application)
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return SDKApplicationDelegate.shared.application(
app,
open: url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation]
)
}
public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return SDKApplicationDelegate.shared.application(
application,
open: url as URL!,
sourceApplication: sourceApplication,
annotation: annotation
)
}
}
Evn: iOS 10, XCode 8.3.2, Swift 3, facebook-sdk-swift-0.2.0
Tested and worked.
Since above answers are indicated as deprecated code, I have changed the syntax.
https://gist.github.com/jgchoi/097b59d85d6b378a81925be93a62fef9
import UIKit
import FacebookCore
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return SDKApplicationDelegate.shared.application(application, open: url)
}
}
}
I have experienced the same problem with Facebook and Twitter integration.I have solved it as follows.
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let appname:String = options.first?.value as! String
print(appname)
if appname == "com.facebook.Facebook" {
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation]
)
}
return Twitter.sharedInstance().application(app, open: url, options: options)
}
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
Add this in addition to what you have in AppDelegate
Somehow the below code didn't work for me
public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return SDKApplicationDelegate.shared.application(application, open: url)
}
So had to change it to
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let options = [UIApplication.OpenURLOptionsKey.sourceApplication: sourceApplication as Any,
UIApplication.OpenURLOptionsKey.annotation: annotation]
return SDKApplicationDelegate.shared.application(application, open: url, options: options)
}

Swift project crashing with Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x0)

It looks like a bad memory access, like trying to access an object that does not exist. I tried using NSZombie to see if something came up, as far as I could tell nothing did. It is crashing at the declaration for the app delegate.
AppDelegate.swift
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]) -> Bool
{
// Override point for customization after app launches
Parse.setApplicationId("removed on purpose", clientKey: "removed on purpose")
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
PFFacebookUtils.initializeFacebook()
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool
{
return FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication, withSession: PFFacebookUtils.session())
}
func applicationDidBecomeActive(application: UIApplication)
{
FBAppCall.handleDidBecomeActiveWithSession(PFFacebookUtils.session())
}
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 throttle down OpenGL ES frame rates. 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 applicationWillTerminate(application: UIApplication)
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
DashboardViewController.swift
import UIKit
class DashboardViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view
}
}
Using breakpoints I have determined that it is not even getting past the class declaration for the app delegate. I tried checking all of the classes in my Main.storyboard file as well to make sure everything was linked properly, again as far as I can tell it is.
I ran into the same issue today. As of Xcode 6 beta 6 the auto complete suggests:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]) -> Bool {}
This crashes at startup with an EXC_BAD_ACCESS and a blank screen.
As soon as an ! is added to the last argument, everything works fine:
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]!) -> Bool {}
In current documentation the ! is missing as well:
optional func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]) -> Bool
Works with Xcode 6.1:
Try
PFAnalytics.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
instead of
PFAnalytics.trackAppOpenedWithLaunchOptions()
Solution from OP.
Problem solved by fixing the code as below.
In all method signatures, replace:
application: UIApplication
with:
application: UIApplication!
And in application:didFinishLaunchingWithOptions:, replace:
launchOptions: [NSObject : AnyObject]
with:
launchOptions: NSDictionary!

Resources