JS views on iOS

183 views
Skip to first unread message

Matija Alanović

unread,
May 19, 2014, 10:09:55 AM5/19/14
to mobile-c...@googlegroups.com
Hi!

How would I go about supporting Javascript views in an iOS application? Following the instructions on https://github.com/couchbase/couchbase-lite-ios/wiki/JavaScript-Views just leaves me with a bunch of linker errors.

Thanks in advance,
Matija Alanovic

J. Chris Anderson

unread,
May 19, 2014, 1:20:14 PM5/19/14
to mobile-c...@googlegroups.com


On Monday, May 19, 2014 7:09:55 AM UTC-7, Matija Alanović wrote:
Hi!

How would I go about supporting Javascript views in an iOS application? Following the instructions on https://github.com/couchbase/couchbase-lite-ios/wiki/JavaScript-Views just leaves me with a bunch of linker errors.

Here is an example of how we do it within the PhoneGap plugin -- this should be similar enough to the code you want that it helps to copy from.


Are you targetting iOS 6 (and shipping your own JS) or just using the built-in JavaScriptCore from iOS 7?

Chris

Matija Alanović

unread,
May 19, 2014, 4:11:40 PM5/19/14
to mobile-c...@googlegroups.com
Thanks Chris!

I'll check it out tomorrow.
I'm only targeting iOS 7 so I'll be using the built-in JavaScriptCore.

Matija Alanović

unread,
May 20, 2014, 8:20:02 AM5/20/14
to mobile-c...@googlegroups.com
Hi!

I looked at the code you provided. It's essentially the same thing I'm using and it doesn't work for me. I get a lot of linker errors like this:

Undefined symbols for architecture i386:
  "_JSContextGetGlobalObject", referenced from:
      -[CBLJSViewCompiler init] in CBLJSViewCompiler.o

It does that for every call to JavaScriptCore API. I don't really get why - shouldn't the old C JavaScriptCore API still be available on iOS 7, although the Objective-C wrapper functions can also be used?

Matija Alanović

unread,
May 20, 2014, 11:35:51 AM5/20/14
to mobile-c...@googlegroups.com
I managed to get this working on a clean project, so it was a project-related problem. Maybe because I was using Cocoapods?

However, I'm now having problems accessing the views. What's the view naming scheme I should use?

This code doesn't return a view:
CBLView *myView = [self.database existingViewNamed:@"designdocumentname/viewname"];

Matija Alanović

unread,
May 21, 2014, 3:52:23 AM5/21/14
to mobile-c...@googlegroups.com
Let me describe what I achieved and what doesn't work in a bit more detail.

I've got a working Javascript view on Cloudant. It's called "messages" and it's in a design document with an ID "_design/desdoc". I'd like to use it on iOS too. Here's what I do in the iOS application:
1. Set the view compiler to Javascript view compiler: CBLRegisterJSViewCompiler();
2. Get the view by its name: CBLView *myView = [self.database existingViewNamed:@"desdoc/messages"];
3. Try logging the view to see if the previos call returned anything: NSLog(@"view: %@", myView);

Unfortunately, only "view: (null)" is logged.

Is there anything I'm missing? Am I referencing the view by the correct name?

I can print the view's map function using this code:
NSLog(@"map function: %@", [self.database existingDocumentWithID:@"_design/desdoc"][@"views"][@"messages"][@"map"]);
Is there perhaps a way to feed this string to the compiler manually? I see there is a method named compileMapFunction in CBLJSViewCompiler, but it seems to be private.

Jens Alfke

unread,
May 21, 2014, 10:23:07 AM5/21/14
to mobile-c...@googlegroups.com

On May 21, 2014, at 12:52 AM, Matija Alanović <mat...@infinum.hr> wrote:

I've got a working Javascript view on Cloudant. It's called "messages" and it's in a design document with an ID "_design/desdoc". I'd like to use it on iOS too.

Only the REST API looks up and compiles view source code from design documents. If you’re using the native API you’ll have to do it yourself. Once you have the JS source code, call
CBLMapBlock mapBlock = [[CBLView compiler] compileMapFunction: mapSource language: @"javascript”];
Then you can use that block to register the view’s map function.

I didn’t expect people to use JS views in native apps, to be honest; this feature was implemented for use with PhoneGap. The performance is going to be a lot worse than a native function, so IMHO it’s worth the effort to rewrite your map function in Objective-C.

—Jens

Matija Alanović

unread,
May 21, 2014, 11:03:22 AM5/21/14
to mobile-c...@googlegroups.com
Thanks Jens, that was the line I was looking for!
I can't abandon JS views so this is my only option.

Thank you once again for your help.
This feature could be better documented though.


--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/nKJeJJ0ecOA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/3197CCAE-F011-4BC9-B9B5-B99BEFCF6788%40couchbase.com.
For more options, visit https://groups.google.com/d/optout.

Julián Astrada

unread,
Feb 3, 2015, 9:36:23 AM2/3/15
to mobile-c...@googlegroups.com
Hi Matija, can you please tell me what framework are you using? I tried compiling a JavaScriptCore framework from Ejecta project or using a compiled one but I keep getting this same errors:

Undefined symbols for architecture i386:

  "_OBJC_CLASS_$_JSContext", referenced from:

objc-class-ref in AppDelegate.o

Thats when I call JSContext to start using a JavaScript.

Jens Alfke

unread,
Feb 3, 2015, 11:57:11 AM2/3/15
to mobile-c...@googlegroups.com

On Feb 3, 2015, at 6:36 AM, Julián Astrada <astrad...@gmail.com> wrote:

Hi Matija, can you please tell me what framework are you using? I tried compiling a JavaScriptCore framework from Ejecta project or using a compiled one but I keep getting this same errors:

FYI: There's no need to compile JavaScriptCore unless your app needs to run on iOS 6.
JavaScriptCore is available as a public system framework on iOS 7+ and Mac OS, so you can just link against it.

—Jens

Julián Astrada

unread,
Feb 3, 2015, 12:52:06 PM2/3/15
to mobile-c...@googlegroups.com
How do you link against it? What you mean? It is not available in the "Linked frameworks and libraries" list. And just adding the #import <JavaScriptCore/JavaScriptCore.h> doesn't fix it.

--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/nKJeJJ0ecOA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.

Jens Alfke

unread,
Feb 3, 2015, 5:25:21 PM2/3/15
to mobile-c...@googlegroups.com

On Feb 3, 2015, at 9:52 AM, Julián Astrada <astrad...@gmail.com> wrote:

How do you link against it? What you mean? It is not available in the "Linked frameworks and libraries" list. And just adding the #import <JavaScriptCore/JavaScriptCore.h> doesn't fix it.

You add it under Build Phases:

If you don't see it there, make sure your target is configured to use a current SDK (iOS 7+).

—Jens

Julián Astrada

unread,
Feb 3, 2015, 11:31:43 PM2/3/15
to mobile-c...@googlegroups.com
Jens

First of all, thanks for your time. I was using iOS 8.2 and it wasn't there :( I just installed Xcode 6.2 beta 5 and it's there and works perfectly fine in the test application I did. I don't know if I messed up my previous xcode or beta 4 didn't include it.... It suspect it was me. 

Once again, thank you very much Jens!!

--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/nKJeJJ0ecOA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages