NativeScript / ios-jsc

NativeScript for iOS using JavaScriptCore
http://docs.nativescript.org/runtimes/ios
Apache License 2.0
299 stars 59 forks source link

error when trying to implement Facebook login on ios #150

Closed enchev closed 9 years ago

enchev commented 9 years ago

From @duongnhatduy on May 20, 2015 9:28

I tried to use iOS SDK, but it crash the whole app without recovering:

var myF = function() { }; 
export function m() { 
  var fbLoginMng: FBSDKLoginManager = new FBSDKLoginManager(); 
  console.log(nsArray); 
  try {
    fbLoginMng.logInWithReadPermissionsHandler(["email"], myF); 
    } catch(e){ 
      console.log(e, JSON.stringify(e,null, 2)) 
    } 
} 

log

/app/loginpage/login-page.js:27:16: CONSOLE LOG ( 
    email 
) 
1   0x3cc36d Inspector::JSGlobalObjectInspectorController::reportAPIException(JSC::ExecState*, JSC::JSValue) 
2   0xede33 NativeScript::reportFatalErrorBeforeShutdown(JSC::ExecState*, JSC::JSValue) 
3   0xd1716 -[TNSRuntime initWithApplicationPath:]::$_0::__invoke(NSException*) 
4   0x2ad7b80 __handleUncaughtException 
5   0x2381d3d _objc_terminate() 
6   0x314bcf0 std::__terminate(void (*)()) 
7   0x314b70f __cxxabiv1::exception_cleanup_func(_Unwind_Reason_Code, _Unwind_Exception*) 
8   0x2381bae _objc_exception_destructor(void*) 
9   0x6071b0 +[FBSDKInternalUtility validateURLSchemes] 
10  0x6071d4 +[FBSDKInternalUtility validateURLSchemes] 
11  0x630d55 -[FBSDKLoginManager logInParametersWithPermissions:] 
12  0x6311f5 -[FBSDKLoginManager logInWithBehavior:] 
13  0x6311ae -[FBSDKLoginManager logInWithPermissions:handler:] 
14  0x62ff51 -[FBSDKLoginManager logInWithReadPermissions:handler:] 
15  0x5f0c9d .LCFI1 
16  0x5f127f ffi_call 
17  0xe3eca NativeScript::FFICall::executeFFICall(JSC::ExecState*, void (*)()) 
18  0xceac4 NativeScript::ObjCMethodCall::executeCall(JSC::ExecState*) 
19  0x45fd9f JSC::LLInt::setUpCall(JSC::ExecState*, JSC::Instruction*, JSC::CodeSpecializationKind, JSC::JSValue, JSC::LLIntCallLinkInfo*) 
20  0x45bc46 llint_slow_path_call 
21  0x465876 llint_entry 
22  0x465886 llint_entry 
23  0x465839 llint_entry 
24  0x465886 llint_entry 
25  0x461446 callToJavaScript 
26  0x3303f1 JSC::JITCode::execute(JSC::VM*, JSC::ProtoCallFrame*) 
27  0x3112d5 JSC::Interpreter::executeCall(JSC::ExecState*, JSC::JSObject*, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&) 
28  0x13b67d JSC::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&) 
29  0xcf50f NativeScript::FFICallback<NativeScript::ObjCMethodCallback>::callFunction(JSC::JSValue const&, JSC::ArgList const&, void*) 
30  0xced8a NativeScript::ObjCMethodCallback::ffiClosureCallback(void*, void**, void*) 
31  0xcf5f2 NativeScript::FFICallback<NativeScript::ObjCMethodCallback>::ffiClosureCallback(ffi_cif*, void*, void**, void*) 
Session ended without errors. 

Copied from original issue: NativeScript/NativeScript#192

enchev commented 9 years ago

From @EddyVerbruggen on May 20, 2015 9:35

Did you setup a valid Fb URL scheme in your .plist file? It looks like that's what the stacktrace is referring to.

For instance, the Cordova plugin adds it for you but with {N}, currently, you need to do it yourself.

enchev commented 9 years ago

From @duongnhatduy on May 20, 2015 9:37

Yeah I forgot that, I will try it now.

enchev commented 9 years ago

From @duongnhatduy on May 20, 2015 10:20

yes, it works. Thanks

enchev commented 9 years ago

From @duongnhatduy on May 20, 2015 12:25

Facebook requires to edit App Delegate: https://developers.facebook.com/docs/ios/getting-started#startcoding. We can do it in Cordova, how can we do it in Nativescript? At the moment, I always get Cancelled result when handling login callback from browser.

ivanbuhov commented 9 years ago

@duongnhatduy Our application module defines custom AppDelegate called TNSAppDelegate. When the system calls TNSAppDelegate methods (e.g. application:didFinishLaunchingWithOptions:) it calls our custom event handler (e.g. onResume, onLaunch). You can read more about these events here: https://docs.nativescript.org/application-management#use-application-callbacks. You can execute custom logic by setting custom function on these events:

var application = require("application");
application.mainModule = "main-page";
application.cssFile = "./app.css";

application.onResume = function() {
    FBSDKAppEvents.activateApp();
}

application.start();

For iOS, onLaunch function is called from applicationDidFinishLaunchingWithOptions method of our TNSAppDelegate. As you can see in source code, onLaunch is called without arguments, so from there you have no access to launchOptions parameter. A temporary solution is to edit the application module source code and pass all the needed arguments to the respective event handlers. You can keep an eye on this issue https://github.com/NativeScript/NativeScript/issues/233 and its progress. Feel free to contribute to our repo if come up with an implemented solution.

MiroValchev commented 9 years ago

Anyone willing to share working FB login with NS or perhaps create a module for it?

benkingcode commented 9 years ago

+1, need to get Facebook Login working for my app but no idea how to do it!

duongnhatduy commented 9 years ago
  1. Open platforms/ios with XCode, import FBSDKCoreKit and LoginKit, edit tns-Info.plist like instructed by Facebook: https://developers.facebook.com/docs/ios/getting-started
  2. Facebook requires editing AppDelegate. There is this workaround without having to modify tns source code. this is what my app.ts looks like:
/// <reference path="./tns_modules/application/application.d.ts"/>
import {mainModule, cssFile, start, onLaunch, onResume, onOpenURL} from "application";

mainModule = "launchpage/launch-page";
cssFile = "./app.css";
// Init Facebook SDK login
onLaunch = function(application, launchOptions) {
    FBSDKApplicationDelegate.sharedInstance().applicationDidFinishLaunchingWithOptions(null, null);
};
onResume = function() {
    FBSDKAppEvents.activateApp();
};
NSNotificationCenter.defaultCenter().addObserverForNameObjectQueueUsingBlock(
    "com.telerik.TLKApplicationOpenURL",
    null,
    null,
    function(note: NSNotification, a2) {
        var TLKApplicationOpenURL = note.userInfo;
        var url = TLKApplicationOpenURL.objectForKey("TLKApplicationOpenURL");
        var application = TLKApplicationOpenURL.objectForKey("TLKApplication");
        console.log("noti", TLKApplicationOpenURL, url, application);
        FBSDKApplicationDelegate.sharedInstance().applicationOpenURLSourceApplicationAnnotation(
            application,
            url,
            "com.apple.mobilesafari",
            null
            );
    }
    );

start();

Here is my login handler

export function facebookLoginHandler() {
  var fbLoginMng: FBSDKLoginManager = new FBSDKLoginManager();
  fbLoginMng.logInWithReadPermissionsHandler([], function(result: FBSDKLoginManagerLoginResult, error: NSError) {
    console.log("ERRROR", error);
    console.log("RESULT Token", result.token);
    console.log("GRented", result.grantedPermissions);
    console.log("Deny", result.declinedPermissions);
    console.log("Cancel", result.isCancelled);
  });
}

I managed to get it working. However, I always get response isCancelled as true.

benkingcode commented 9 years ago

Would it be possible to share a sample app? My NS build is just failing with ld: framework not found FBSDKCoreKit

duongnhatduy commented 9 years ago

Did you drag FBSDKLoginKit.framework and FBSDKCoreKit.framework to your ios project? snip20150617_4

benkingcode commented 9 years ago

I got it working in the end, but manually dragging the frameworks into the project didn't work, had to run tns library add ..., and then also had to add CoreGraphics in the Xcode project.

Unfortunately it now only runs on the emulator, not the device. the tns deploy step fails at VerifyingApplication, and Xcode says "The application does not have a valid signature".

duongnhatduy commented 9 years ago

yea, now I remember I did run tns library add... as well

Fatme commented 9 years ago

Hi @dbbk,

From what I've seen when this happens, it is usually a code-signing error. Can you try to deploy to device using Xcode? Also, you may have to allow your device to be used for development in Xcode Organizer: Run DevToolsSecurity -enable

benkingcode commented 9 years ago

I've had no problems with deploying to device until adding the Facebook SDK. I even recreated the project from scratch and could deploy the Hello World sample fine, but it stopped working once the SDK library was added.

duongnhatduy commented 9 years ago

I just updated nativescript cli from 1.0 to 1.1 and encountered same problem. I had to add CoreGraphics and deploying to device fails at VerifyingApplication

duongnhatduy commented 9 years ago

has anyone managed to solve the problem?

jasssonpet commented 9 years ago

Moved to https://github.com/NativeScript/ios-runtime/issues/220