Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

objective c - How to access iOS private APIs in Swift?

How can I call non-public iOS functions and acces non public properies from Swift? Specifically, I would like to use one non-public class in QuartzCore framework.

One solution that came to my mind is to create "bridging" Objective-C project that would wrap this non-public APIs into public ones and than call this Objective-C functions from Swift. However, my solution is pure Swift now and I would prefer to keep it that way. Is there any more staitforward way? (for example adding something to Objective-C bridging header file)

Note: I know what you are thinking, private APIs are private because they should not be used. I know the risks, I am aware of all the donwsides, app store restrictions etc. After all that carefully considered and lot of research, it unfortunatelly still seems the best way to go in this particular case.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do the very same trick that you would do if your project was in Objective-C.

Create a header file and put the declaration of a category on the class, along with the declaration of the private methods you wish to expose. Then, tell the Swift compiler to import this bridging header.

For demonstration purposes, I'm going to poke around in the internals of NSBigMutableString, a private subclass of NSMutableString, and I will call its _isMarkedAsImmutable method.

Note that here, the entire class itself is private, hence I have to declare it as a subclass of its real superclass first. Because of this, I could have as well declared the method right in the declaration of the class itself; that, however, wouldn't have demonstrated the usage of the (Private) trick-category.

If your class is public, then obviously you will only need the category, and you won't need to (re-)declare the class itself.

Bridge.h:

#import <Foundation/Foundation.h>

@interface NSMutableString (Private)
- (BOOL)_isMarkedAsImmutable; // this is a lie
@end

@interface NSBigMutableString : NSMutableString
@end

s.swift:

let s = NSBigMutableString()
println(s._isMarkedAsImmutable())

Compile and run:

$ xcrun -sdk macosx swiftc -o foo -import-objc-header Bridge.h s.swift
$ ./foo
false
$

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...