Hi Folks,
I'm back with the same solution for iOS - I have a working Kivy webview on iOS now. Yay.
First: There is a caveat, for which I could use help from the kivy authors: I cannot seem to run the webview simultaneously with SDL. The only way to get the webview started from python was to initiate a function call from pyobjus into ObjectiveC to create a new window that contains the webview. But without disabling SDL, the window simply did not appear. Of course I don't *want* to disable it, but it works until someone with more expertise can figure out how to run them both at the same time.
Anyway, here's the steps:
1. First, open the xcode project and remove all the "*SDL*.a" libraries from the xcode project in the "Build Phases" section under "Link with Libraries" towards the bottom.
2. Find the main.m and main.h files and remove them. Because we have temporarily removed SDL, we no longer have a UIApplicationMain that was provided by Kivy-ios, so we have to make one from scratch and *then* call python main.py after the UI framework has started up. (Hopefully, someone else can recommend a better way)
3. Make a new main.h that looks like this:
https://github.com/hinesmr/mica-ios/blob/master/main.h
4. Make a new main.m that looks like this:
https://github.com/hinesmr/mica-ios/blob/master/main.m
You'll notice that this looks much like the original kivy-ios template, except that we have brought back the UIApplicationMain main routine startup.
6. Next open the kivy-generated file "bridge.h" and add this to the class:
@property (strong, nonatomic) UIWindow *window;
5. Next, open the kivy-generated file "bridge.m" in your xcode project and add the below new functions (we will call these from python using pyobjus).
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@implementation mica {
}
- (id) init {
if(self = [super init]) {
self.window = nil; // add this line to init function
}
return self;
}
- (void) dealloc {
[self.window release]; // add this line to dealloc function
[super dealloc];
}
/* Add these two new functions */
- (void) webviewstatic :(NSString*)html {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (self.window != nil) {
[self.window release];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSLog(@"Creating static webview splash.");
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIWebView * webView = [[UIWebView alloc] initWithFrame: self.window.bounds];
[webView loadHTMLString:html baseURL:nil];
UIViewController *vc = [[UIViewController alloc] init];
self.window.rootViewController = vc;
[vc.view addSubview:webView];
}];
}
- (void) webview :(NSString*)fullURL {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (self.window != nil) {
[self.window release];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSLog(@"Creating webview. with url %@", fullURL);
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
NSURL * url = [NSURL URLWithString:fullURL];
NSURLRequest * requestObj = [NSURLRequest requestWithURL:url];
UIWebView * webView = [[UIWebView alloc] initWithFrame: self.window.bounds];
[webView loadRequest:requestObj];
UIViewController *vc = [[UIViewController alloc] init];
self.window.rootViewController = vc;
[vc.view addSubview:webView];
}];
}
6. Finally, create a main.py file, but don't include anything from kivy (because we deleted SDL).
We just want to make sure we can successfully invoke the webview from python.
7. In your main.py file, you should be able to do something like this:
from pyobjus import autoclass, objc_f, objc_str as String
|
bridge= autoclass('bridge').alloc().init()
|
bridge.webview_(String("http://www.google.com"))
|
bridge.webviewstatic_(String("hello world webpage"))
|
That's it!
- Michael