Flutter-writing-ios-specific-code
提供:Dev Guides
Flutter-IOS固有のコードの記述
iOS固有のコードへのアクセスは、iOS固有の言語(Objective-CまたはSwiftとiOS SDK)を使用することを除いて、Androidプラットフォームのコードに似ています。 それ以外の場合、概念はAndroidプラットフォームの概念と同じです。
iOSプラットフォームについても、前の章と同じアプリケーションを作成してみましょう。
- Android Studio(macOS)で新しいアプリケーション_flutter_browser_ios_app_を作成しましょう
- 前の章のように、手順2〜6に従います。
- XCodeを起動し、[ファイル]→[開く]をクリックします*
- flutterプロジェクトのiosディレクトリの下にあるxcodeプロジェクトを選択します。
- *ランナー→ランナーパス*でAppDelegate.mを開きます。 次のコードが含まれています-
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//[GeneratedPluginRegistrant registerWithRegistry:self];
//Override point for customization after application launch.
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
*指定されたURLでブラウザーを開くメソッドopenBrowserを追加しました。 単一の引数urlを受け入れます。
- (void)openBrowser:(NSString* )urlString {
NSURL *url = [NSURL URLWithString:urlString];
UIApplication *application = [UIApplication sharedApplication];
[application openURL:url];
}
- didFinishLaunchingWithOptionsメソッドで、コントローラーを見つけてコントローラー変数に設定します。
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
- didFinishLaunchingWithOptionsメソッドで、ブラウザチャネルをflutterapp.finddevguides.com/browseに設定します-
FlutterMethodChannel* browserChannel = [
FlutterMethodChannel methodChannelWithName:
@"flutterapp.finddevguides.com/browser" binaryMessenger:controller];
- 変数、weakSelfを作成し、現在のクラスを設定します-
__weak typeof(self) weakSelf = self;
- 次に、setMethodCallHandlerを実装します。 call.methodに一致させてopenBrowserを呼び出します。 call.argumentsを呼び出してURLを取得し、openBrowserの呼び出し中にそれを渡します。
[browserChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
if ([@"openBrowser" isEqualToString:call.method]) {
NSString* url = call.arguments[@"url"];
[weakSelf openBrowser:url];
} else { result(FlutterMethodNotImplemented); }
}];
*完全なコードは次のとおりです-
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication* )application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//custom code starts
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
FlutterMethodChannel* browserChannel = [
FlutterMethodChannel methodChannelWithName:
@"flutterapp.finddevguides.com/browser" binaryMessenger:controller];
__weak typeof(self) weakSelf = self;
[browserChannel setMethodCallHandler:^(
FlutterMethodCall *call, FlutterResult result) {
if ([@"openBrowser" isEqualToString:call.method]) {
NSString* url = call.arguments[@"url"];
[weakSelf openBrowser:url];
} else { result(FlutterMethodNotImplemented); }
}];
//custom code ends
[GeneratedPluginRegistrant registerWithRegistry:self];
//Override point for customization after application launch.
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)openBrowser:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
UIApplication *application = [UIApplication sharedApplication];
[application openURL:url];
}
@end
- プロジェクト設定を開きます。
- *機能*に移動し、*バックグラウンドモード*を有効にします。
- バックグラウンドフェッチ*および*リモート通知*を追加します。
- 次に、アプリケーションを実行します。 Androidバージョンと同様に機能しますが、クロムではなくSafariブラウザーが開きます。