Recent

Author Topic: Weak linking objcclass?  (Read 6827 times)

CCRDude

  • Hero Member
  • *****
  • Posts: 502
Weak linking objcclass?
« on: March 19, 2015, 10:58:27 am »
As soon as I declare iOS SDK classes that do not exist in older iOS versions, my app fails.

Now, from my background with regular external libraries, I would say that I need to dynamically dynamically instead of dynamically statically link UIUserNotificationSettings. But I simply have no idea if that is possible for objcclasses in FPC, and if so, how to do it. Any help and hints would be appreciated :)

The background of this issue: I want to support UILocalNotification, which works fine on iOS 6/7, but needs registration (to allow the OS to ask the user for permission) on iOS 8. The classes needed for that are not available in previous OS versions.

My app crashes in 7.x simulators, XCode tells me:
Quote
dyld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings

My class declaration:
Code: [Select]
type
  UIUserNotificationSettings = objcclass external (NSObject)
  public
    class function settingsForTypes_categories (types: UIUserNotificationType; categories: NSSet): instancetype; message 'settingsForTypes:categories:';
    function types: UIUserNotificationType; message 'types';
    function categories: NSSet; message 'categories';
  end;

I use the code the following way, but it doesn't matter if I use it at all, so I assume smart linking won't remove the above linking.
Code: [Select]
var
   uns: UIUserNotificationSettings;
[...]
  if (UIApplication.instancesRespondToSelector(objcselector('registerUserNotificationSettings:'))) then begin
     uns := UIUserNotificationSettings.settingsForTypes_categories(UIUserNotificationTypeBadge or UIUserNotificationTypeSound or UIUserNotificationTypeAlert, nil);
     UIApplication_UIUserNotificationSettings(UIApplication.sharedApplication).registerUserNotificationSettings(uns);
  end;

I don't want to limit my application to iOS 8 users, which would in an instant rule out most testers I have currently.
« Last Edit: March 20, 2015, 09:40:35 am by CCRDude »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2289
    • havefunsoft.com
Re: Dynamically linking objcclass?
« Reply #1 on: March 19, 2015, 03:35:22 pm »
I wonder if you're looking for weak linking feature. ... hmm... is it on todo list?
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 674
Re: Dynamically linking objcclass?
« Reply #2 on: March 19, 2015, 06:24:42 pm »
It's indeed weak linking of Objective-C classes that's needed, but that's not yet implemented (it's largely unrelated to the linked wiki page).

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2289
    • havefunsoft.com
Re: Dynamically linking objcclass?
« Reply #3 on: March 19, 2015, 06:36:07 pm »
(it's largely unrelated to the linked wiki page).
this is odd. Isn't
Quote
Required for: supporting weakly linking symbols (symbols which may not be available at run time, and which map to NIL in that case)
specifically speaks about it.

Or do you mean the wiki page (todo list) in general
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 674
Re: Dynamically linking objcclass?
« Reply #4 on: March 19, 2015, 06:37:25 pm »
The handling of Objective-C classes is unrelated to the handling of regular symbols.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2289
    • havefunsoft.com
Re: Dynamically linking objcclass?
« Reply #5 on: March 19, 2015, 06:53:32 pm »
The handling of Objective-C classes is unrelated to the handling of regular symbols.
Ah! The compiler's kitchen!

I'll leave these two links (DIY Objective-C weak import, ObjC Classes Weak Import), if anyone is interested in (non-FPC, but ObjC) techy details

hmm, I wonder if the first DIY method works with the current binutils.
« Last Edit: March 19, 2015, 06:58:29 pm by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

CCRDude

  • Hero Member
  • *****
  • Posts: 502
Re: Dynamically linking objcclass?
« Reply #6 on: March 20, 2015, 09:40:22 am »
Many thanks for the qualified feedback on my issue!

Sorry for giving it the wrong name, will remember the term "weak linking" :)

Also thanks for the non-FPC links. While it obviously didn't help to do this asm call just within the main.m file, it leads me into a direction that might do the trick - since it's only a small amount of code here, I could write the code surrounding local notifications in Objective-C, and use that in my FPC code. That way, I can do the weak linking and testing where it's supported.

Update: tested the mentioned method. Wrote an Objective-C class wrapping access to the 8.x+ class, and am calling that class from my Pascal code. This works on both 7.1 and 8.1 now. Mission accomplished!
« Last Edit: March 20, 2015, 10:02:07 am by CCRDude »