Noodlecode parody of spaghetti code

Tag: objective c

Creating [object] in a different context than the calling function.

Creating [object] in a different context than the calling function.

This warning occured when a JS callback function is passed to Objective-C as KrollCallback and then executed on non-JS thread.

KrollCallback *callback = [args objectForKey@"callback"];

[OneSignal initWithLaunchOptions:[TiApp app].launchOptions appId:appId handleNotificationAction:^(OSNotificationResult *result) {
    [callback call:@[] thisObject:nil];
}];

Problem with this warning is, if the JS callback create Ti.Network.HTTPClient instance, then that instance is null.

function callback() {
    var http = Ti.Network.createHTTPClient();
    http.open('GET', getUrl()); // JS error! http is null
    http.send();
}

To fix, get krollContext instance of current module/proxy and execute callback inside invokeBlockOnThread

// For TiModule / TiProxy descendants
KrollContext *context = [self.executionContext krollContext];

// For TiUIView descendants
KrollContext *context = [self.proxy.executionContext krollContext];

// invoke
[context invokeBlockOnThread:^{
    [callback call:@[] thisObject:nil];
}];

Load nib file in Titanium module iOS

When creating Titanium module for iOS platform, sometimes we need to include xib (NeXT Interface Builder) file. But if we include the file just like that, then we will face this error

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/username/Library/Application Support/iPhone Simulator/6.1/Applications/<app id>/myiosmodule.app> (loaded)' with name 'MyView''

To fix, we need to include a compiled xib file known as nib file. To compile it, you need to include it in a native iOS project & build it.

Update: Easier way to compile xib to nib file is using ibtool

$ ibtool MPAdBrowserController.xib --compile MPAdBrowserController.nib

Compile xib to nib file

For example here, I’m create mopub module for iOS

  1. Open Xcode, create new project, and choose ‘Single View Application’
    new-project-option
  2. Add xib file to the project
    add-file
    select-xib-file
  3. Make sure it is included in the ‘Copy Bundle Resource’ list, then click ‘Run’ button to launch the sample app in simulator
    copy-bundle-run
  4. After it runs on simulator, using Finder go to /Users/username/Library/Application Support/iPhone Simulator/6.1/Applications/<app id>/test.app, then see package content
    open-simulator-folder-2
    show-package-content
  5. You’ll find the nib file there. Put this nib file into /assets/ folder of TItanium module. Then you need to edit the path to load nib file in initWithNibName method.
    edit-code-init

In case you can’t edit the source Objective-C file, you have to put the nib file into /Resources/iphone/ folder of the app project

Copyright © 2017 Noodlecode