#[repr(C)]pub struct WKUserContentController { /* private fields */ }
WKUserContentController
only.Expand description
A WKUserContentController object provides a way for JavaScript to post messages to a web view. The user content controller associated with a web view is specified by its web view configuration.
See also Apple’s documentation
Implementations§
Source§impl WKUserContentController
impl WKUserContentController
Sourcepub unsafe fn userScripts(&self) -> Retained<NSArray<WKUserScript>>
Available on crate feature WKUserScript
only.
pub unsafe fn userScripts(&self) -> Retained<NSArray<WKUserScript>>
WKUserScript
only.The user scripts associated with this user content controller.
Sourcepub unsafe fn addUserScript(&self, user_script: &WKUserScript)
Available on crate feature WKUserScript
only.
pub unsafe fn addUserScript(&self, user_script: &WKUserScript)
WKUserScript
only.Adds a user script.
Parameter userScript
: The user script to add.
Sourcepub unsafe fn removeAllUserScripts(&self)
pub unsafe fn removeAllUserScripts(&self)
Removes all associated user scripts.
Sourcepub unsafe fn addScriptMessageHandler_contentWorld_name(
&self,
script_message_handler: &ProtocolObject<dyn WKScriptMessageHandler>,
world: &WKContentWorld,
name: &NSString,
)
Available on crate features WKContentWorld
and WKScriptMessageHandler
only.
pub unsafe fn addScriptMessageHandler_contentWorld_name( &self, script_message_handler: &ProtocolObject<dyn WKScriptMessageHandler>, world: &WKContentWorld, name: &NSString, )
WKContentWorld
and WKScriptMessageHandler
only.Adds a script message handler.
Parameter scriptMessageHandler
: The script message handler to add.
Parameter contentWorld
: The WKContentWorld in which to add the script message handler.
Parameter name
: The name of the message handler.
Adding a script message handler adds a function window.webkit.messageHandlers. <name
.postMessage( <messageBody ) to all frames, available in the given WKContentWorld.
The name argument must be a non-empty string.
Each WKContentWorld can have any number of script message handlers, but only one per unique name.
Once any script message handler has been added to a WKContentWorld for a given name, it is an error to add another script message handler to that WKContentWorld for that same name without first removing the previous script message handler.
The above restriction applies to any type of script message handler - WKScriptMessageHandler and WKScriptMessageHandlerWithReply objects will conflict with each other if you try to add them to the same WKContentWorld with the same name.
Sourcepub unsafe fn addScriptMessageHandlerWithReply_contentWorld_name(
&self,
script_message_handler_with_reply: &ProtocolObject<dyn WKScriptMessageHandlerWithReply>,
content_world: &WKContentWorld,
name: &NSString,
)
Available on crate features WKContentWorld
and WKScriptMessageHandlerWithReply
only.
pub unsafe fn addScriptMessageHandlerWithReply_contentWorld_name( &self, script_message_handler_with_reply: &ProtocolObject<dyn WKScriptMessageHandlerWithReply>, content_world: &WKContentWorld, name: &NSString, )
WKContentWorld
and WKScriptMessageHandlerWithReply
only.Adds a script message handler.
Parameter scriptMessageHandlerWithReply
: The script message handler to add.
Parameter contentWorld
: The WKContentWorld in which to add the script message handler.
Parameter name
: The name of the message handler.
Adding a script message handler adds a function window.webkit.messageHandlers. <name
.postMessage( <messageBody ) to all frames, available in the given WKContentWorld.
The name argument must be a non-empty string.
Each WKContentWorld can have any number of script message handlers, but only one per unique name.
Once any script message handler has been added to a WKContentWorld for a given name, it is an error to add another script message handler to that WKContentWorld for that same name without first removing the previous script message handler.
The above restriction applies to any type of script message handler - WKScriptMessageHandlerWithReply and WKScriptMessageHandler objects will conflict with each other if you try to add them to the same WKContentWorld with the same name.
Refer to the WKScriptMessageHandlerWithReply documentation for examples of how it is more flexible than WKScriptMessageHandler.
Sourcepub unsafe fn addScriptMessageHandler_name(
&self,
script_message_handler: &ProtocolObject<dyn WKScriptMessageHandler>,
name: &NSString,
)
Available on crate feature WKScriptMessageHandler
only.
pub unsafe fn addScriptMessageHandler_name( &self, script_message_handler: &ProtocolObject<dyn WKScriptMessageHandler>, name: &NSString, )
WKScriptMessageHandler
only.Adds a script message handler to the main world used by page content itself.
Parameter scriptMessageHandler
: The script message handler to add.
Parameter name
: The name of the message handler.
Calling this method is equivalent to calling addScriptMessageHandler:contentWorld:name: with [WKContentWorld pageWorld] as the contentWorld argument.
Sourcepub unsafe fn removeScriptMessageHandlerForName_contentWorld(
&self,
name: &NSString,
content_world: &WKContentWorld,
)
Available on crate feature WKContentWorld
only.
pub unsafe fn removeScriptMessageHandlerForName_contentWorld( &self, name: &NSString, content_world: &WKContentWorld, )
WKContentWorld
only.Removes a script message handler.
Parameter name
: The name of the message handler to remove.
Parameter contentWorld
: The WKContentWorld from which to remove the script message handler.
Sourcepub unsafe fn removeScriptMessageHandlerForName(&self, name: &NSString)
pub unsafe fn removeScriptMessageHandlerForName(&self, name: &NSString)
Removes a script message handler.
Parameter name
: The name of the message handler to remove.
Calling this method is equivalent to calling removeScriptMessageHandlerForName:contentWorld: with [WKContentWorld pageWorld] as the contentWorld argument.
Sourcepub unsafe fn removeAllScriptMessageHandlersFromContentWorld(
&self,
content_world: &WKContentWorld,
)
Available on crate feature WKContentWorld
only.
pub unsafe fn removeAllScriptMessageHandlersFromContentWorld( &self, content_world: &WKContentWorld, )
WKContentWorld
only.Removes all script message handlers from a given WKContentWorld.
Parameter contentWorld
: The WKContentWorld from which to remove all script message handlers.
Sourcepub unsafe fn removeAllScriptMessageHandlers(&self)
pub unsafe fn removeAllScriptMessageHandlers(&self)
Removes all associated script message handlers.
Sourcepub unsafe fn addContentRuleList(&self, content_rule_list: &WKContentRuleList)
Available on crate feature WKContentRuleList
only.
pub unsafe fn addContentRuleList(&self, content_rule_list: &WKContentRuleList)
WKContentRuleList
only.Adds a content rule list.
Parameter contentRuleList
: The content rule list to add.
Sourcepub unsafe fn removeContentRuleList(
&self,
content_rule_list: &WKContentRuleList,
)
Available on crate feature WKContentRuleList
only.
pub unsafe fn removeContentRuleList( &self, content_rule_list: &WKContentRuleList, )
WKContentRuleList
only.Removes a content rule list.
Parameter contentRuleList
: The content rule list to remove.
Sourcepub unsafe fn removeAllContentRuleLists(&self)
pub unsafe fn removeAllContentRuleLists(&self)
Removes all associated content rule lists.
Methods from Deref<Target = NSObject>§
Sourcepub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
Handle messages the object doesn’t recognize.
See Apple’s documentation for details.
Methods from Deref<Target = AnyObject>§
Sourcepub fn class(&self) -> &'static AnyClass
pub fn class(&self) -> &'static AnyClass
Dynamically find the class of this object.
§Example
Check that an instance of NSObject
has the precise class NSObject
.
use objc2::ClassType;
use objc2::runtime::NSObject;
let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());
Sourcepub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
👎Deprecated: this is difficult to use correctly, use Ivar::load
instead.
pub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
Ivar::load
instead.Use Ivar::load
instead.
§Safety
The object must have an instance variable with the given name, and it
must be of type T
.
See Ivar::load_ptr
for details surrounding this.
Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
Attempt to downcast the object to a class of type T
.
This is the reference-variant. Use Retained::downcast
if you want
to convert a retained object to another type.
§Mutable classes
Some classes have immutable and mutable variants, such as NSString
and NSMutableString
.
When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.
So using this method to convert a NSString
to a NSMutableString
,
while not unsound, is generally frowned upon unless you created the
string yourself, or the API explicitly documents the string to be
mutable.
See Apple’s documentation on mutability and on
isKindOfClass:
for more details.
§Generic classes
Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.
You can, however, safely downcast to generic collections where all the
type-parameters are AnyObject
.
§Panics
This works internally by calling isKindOfClass:
. That means that the
object must have the instance method of that name, and an exception
will be thrown (if CoreFoundation is linked) or the process will abort
if that is not the case. In the vast majority of cases, you don’t need
to worry about this, since both root objects NSObject
and
NSProxy
implement this method.
§Examples
Cast an NSString
back and forth from NSObject
.
use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};
let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();
Try (and fail) to cast an NSObject
to an NSString
.
use objc2_foundation::{NSObject, NSString};
let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());
Try to cast to an array of strings.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();
This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.
Downcast when processing each element instead.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
for elem in arr {
if let Some(data) = elem.downcast_ref::<NSString>() {
// handle `data`
}
}
Trait Implementations§
Source§impl AsRef<AnyObject> for WKUserContentController
impl AsRef<AnyObject> for WKUserContentController
Source§impl AsRef<NSObject> for WKUserContentController
impl AsRef<NSObject> for WKUserContentController
Source§impl Borrow<AnyObject> for WKUserContentController
impl Borrow<AnyObject> for WKUserContentController
Source§impl Borrow<NSObject> for WKUserContentController
impl Borrow<NSObject> for WKUserContentController
Source§impl ClassType for WKUserContentController
impl ClassType for WKUserContentController
Source§const NAME: &'static str = "WKUserContentController"
const NAME: &'static str = "WKUserContentController"
Source§type ThreadKind = dyn MainThreadOnly
type ThreadKind = dyn MainThreadOnly
Source§impl Debug for WKUserContentController
impl Debug for WKUserContentController
Source§impl Deref for WKUserContentController
impl Deref for WKUserContentController
Source§impl Hash for WKUserContentController
impl Hash for WKUserContentController
Source§impl Message for WKUserContentController
impl Message for WKUserContentController
Source§impl NSCoding for WKUserContentController
impl NSCoding for WKUserContentController
Source§impl NSObjectProtocol for WKUserContentController
impl NSObjectProtocol for WKUserContentController
Source§fn isEqual(&self, other: Option<&AnyObject>) -> bool
fn isEqual(&self, other: Option<&AnyObject>) -> bool
Source§fn hash(&self) -> usize
fn hash(&self) -> usize
Source§fn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
Source§fn is_kind_of<T>(&self) -> bool
fn is_kind_of<T>(&self) -> bool
isKindOfClass
directly, or cast your objects with AnyObject::downcast_ref
Source§fn isMemberOfClass(&self, cls: &AnyClass) -> bool
fn isMemberOfClass(&self, cls: &AnyClass) -> bool
Source§fn respondsToSelector(&self, aSelector: Sel) -> bool
fn respondsToSelector(&self, aSelector: Sel) -> bool
Source§fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
Source§fn debugDescription(&self) -> Retained<NSObject>
fn debugDescription(&self) -> Retained<NSObject>
Source§impl PartialEq for WKUserContentController
impl PartialEq for WKUserContentController
Source§impl RefEncode for WKUserContentController
impl RefEncode for WKUserContentController
Source§const ENCODING_REF: Encoding = <NSObject as ::objc2::RefEncode>::ENCODING_REF
const ENCODING_REF: Encoding = <NSObject as ::objc2::RefEncode>::ENCODING_REF
impl DowncastTarget for WKUserContentController
impl Eq for WKUserContentController
Auto Trait Implementations§
impl !Freeze for WKUserContentController
impl !RefUnwindSafe for WKUserContentController
impl !Send for WKUserContentController
impl !Sync for WKUserContentController
impl !Unpin for WKUserContentController
impl !UnwindSafe for WKUserContentController
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<'a, T> MainThreadOnly for T
impl<'a, T> MainThreadOnly for T
Source§fn mtm(&self) -> MainThreadMarker
fn mtm(&self) -> MainThreadMarker
MainThreadMarker
from the main-thread-only object. Read more