Custom Context Menu

turkgencturkgenc Member ✭✭
edited January 7 in Xamarin.iOS

Can Custom context menu (new Selector("CustomCopy")) call ios method copy: _share: or cut: on control WKWebView.
or How can these events be customized?

Control->(WKWebView)

 [Export("CustomCopy")]
 void CustomCopy(){
    call -> ios copy:
}

another event share
 [Export("CustomShare")]
 void CustomShare(){
    call -> ios  _share:
}

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you want to call the default Copy method in your custom method, you can try:

    [Export("CustomCopy")]
    void CustomCopy()
    {
        //call->ios copy:
        base.Copy(UIMenuController.SharedMenuController);
    }
    

    So do the other events.
    But _share method isn't exposed by the parent control. So we have to place the custom code at another place:

    public override bool CanPerform(Selector action, NSObject withSender)
    {
    
        if (action == new Selector("_share:"))
        {
            // Add your customized code
            return true;
        }
        return base.CanPerform(action, withSender);
    }
    
  • RognikRognik Member ✭✭

    If your problem successfully solved, tell feedback.

  • turkgencturkgenc Member ✭✭

    base.Copy(UIMenuController.SharedMenuController);

    Unhandled Exception:

    Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[my_iOS_Renderers_HybridWebViewRenderer copy:]: unrecognized selector sent to instance 0x7ffe344b1f70
    Native stack trace:
    0 CoreFoundation 0x000000011b58f1bb exceptionPreprocess + 331
    1 libobjc.A.dylib 0x000000011c317735 objc_exception_throw + 48
    2 CoreFoundation 0x000000011b5adf44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3 UIKitCore 0x0000000126186b4a -[UIResponder doesNotRecognizeSelector:] + 287
    4 CoreFoundation 0x000000011b593c34 forwarding + 772
    5 CoreFoundation 0x000000011b595da8 _CF_forwarding_prep_0 + 120
    6 my.iOS 0x000000010dd9c11c xamarin_dyn_objc_msgSendSuper + 220
    7 ??? 0x000000014147d4b7 0x0 + 5390193847
    8 my.iOS 0x000000010db80311 mono_jit_runtime_invoke + 1441
    9 my.iOS 0x000000010dc6f87f mono_runtime_invoke_checked + 127
    10 my.iOS 0x000000010dc731f3 mono_runtime_invoke + 83
    11 my.iOS 0x000000010d9fa18c _ZL31native_to_managed_trampoline_49P11objc_objectP13objc_selectorPP11_MonoMethodP8NSObjectj + 604
    12 my.iOS 0x000000010d9f9f28 -[my_iOS_Renderers_HybridWebViewRenderer Kopyala:] + 56
    13 UIKitCore 0x0000000125d075de -[UICalloutBar buttonPressed:] + 407
    14 UIKitCore 0x0000000125d05633 -[UICalloutBarButton fadeAndSendAction] + 81
    15 Foundation 0x000000011732b431 __NSFireDelayedPerform + 436
    16 CoreFoundation 0x000000011b4f4f34 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION
    + 20
    17 CoreFoundation 0x000000011b4f4b32 __CFRunLoopDoTimer + 1026
    18 CoreFoundation 0x000000011b4f439a __CFRunLoopDoTimers + 266
    19 CoreFoundation 0x000000011b4eea1c __CFRunLoopRun + 2252
    20 CoreFoundation 0x000000011b4ede11 CFRunLoopRunSpecific + 625
    21 GraphicsServices 0x00000001219161dd GSEventRunModal + 62
    22 UIKitCore 0x000000012615881d UIApplicationMain + 140
    23 ??? 0x000000013d13eb2e 0x0 + 5319682862
    24 ??? 0x000000013d13e8c3 0x0 + 5319682243

  • turkgencturkgenc Member ✭✭

    For _share, this code working but only list facebook,twitter, SinaWeibo, TencentWeibo. Can we list all property? not only social media.

    public void ShareLink(string title, string status, string link)
    {
    var actionSheet = new UIActionSheet("Share on");
    foreach (SLServiceKind service in Enum.GetValues(typeof(SLServiceKind)))
    {
    actionSheet.AddButton(service.ToString());
    }
    actionSheet.Clicked += delegate(object a, UIButtonEventArgs b)
    {
    SLServiceKind serviceKind = (SLServiceKind)Enum.Parse(typeof(SLServiceKind), actionSheet.ButtonTitle(b.ButtonIndex));
    ShareOnService(serviceKind, title, status, link);
    };
    actionSheet.ShowInView(UIApplication.SharedApplication.KeyWindow.RootViewController.View);
    }

    private void ShareOnService(SLServiceKind service, string title, string status, string link)
    {
        if (SLComposeViewController.IsAvailable(service))
        {
            var slComposer = SLComposeViewController.FromService(service);
            slComposer.SetInitialText(status);
            slComposer.SetInitialText(title != null ? string.Format("{0} {1}", title, status) : status);
            if (link != null)
            {
                slComposer.AddUrl(new NSUrl(link));
            }
            slComposer.CompletionHandler += (result) =>
            {
                UIApplication.SharedApplication.KeyWindow.RootViewController.InvokeOnMainThread(() =>
                {
                    UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(true, null);
                });
            };
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(slComposer, true, null);
        }
    }
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    @turkgenc I notice you used a HybridWebViewRenderer to consume WKWebView in Xamarin.Forms. Do not place your code in that renderer class, create your own customized WKWebView and place all your logic code there. Then set this custom wkwebview to your native control in the renderer:

    var webView = new MyWKWebView (Frame, config);
    SetNativeControl (webView);
    

    I make a demo depending on the official HybirdWebview sample for you, you could refer to the attachment for more detailed code.

    But for share event, this is controlled by the system. It could list all the third party app which support the type of your sharing things. i.e. if you want to share text, the destination apps need to support public.text type.
    Moreover click More button in the sharing actionsheet to choose the third party apps.

Sign In or Register to comment.