Change AppDelegate.mm to AppDelegate.swift on React Native 0.71 - ios

I encountered the error "A moduleName is required to create an RCTROotView", and then the app was stuck on the startup page. Who can help me? thank you.
Step
run 'npx react-native init RN0710RC3 --version 0.71.0-rc.3' to create a RN project
create 'AppDelegate.swift' 'Bridging-Header.h' and delete 'AppDelegate.mm' 'AppDelegate.h' 'main.m'
AppDelegate.mm
#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.moduleName = #"RN0710RC3";
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:#"index"];
#else
return [[NSBundle mainBundle] URLForResource:#"main" withExtension:#"jsbundle"];
#endif
}
- (BOOL)concurrentRootEnabled
{
return true;
}
#end
AppDelegate.swift
import Foundation
import UIKit
#UIApplicationMain
class AppDelegate: RCTAppDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
CommonTheme.currentTheme.primaryColor = .red;
self.moduleName = "RN0710RC3";
return self.application(application, didFinishLaunchingWithOptions: launchOptions);
}
override func sourceURL(for bridge: RCTBridge!) -> URL! {
#if DEBUG
return RCTBundleURLProvider.sharedSettings()?.jsBundleURL(forBundleRoot: "index", fallbackResource: nil)
#else
return Bundle.main.url(forResource: "main", withExtension: "jsBundle")
#endif
}
func concurrentRootEnabled() -> Bool {
return true;
}
}
Bridging-Header.h
#import <RCTAppDelegate.h>
#import <React/RCTBundleURLProvider.h>

The function concurrentRootEnabled is required by the Objective-C interface RCTAppDelegate, so you should put #objc to it.
#objc func concurrentRootEnabled() -> Bool {
return true;
}

return [super application:application didFinishLaunchingWithOptions:launchOptions];
should convert into
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
and then the issue should be solved.

Related

Flutter AppDelegate.m file under the Runner folder doesn't exist for ios config

I would like to integrate
simple_auth_flutter: ^2.0.11
into my flutter project. This should run via keycloak ...
In the installation it says that I have to modify the AppDelegate.m for ios like this:
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import <Flutter/Flutter.h>
#import <simple_auth_flutter/SimpleAuthFlutterPlugin.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];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
return [SimpleAuthFlutterPlugin checkUrl:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [SimpleAuthFlutterPlugin checkUrl:url];
}
#end
but my problem is, there doesn't exist this file in runner folder.
There is only the AppDelegate.swift
it look like this
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
I guess I'll have to work with that. How do I do that properly?
The AppDelegate.m is used by iOS Projects that primarily use the Objective-C language. iOS Project using the newer Swift language have a AppDelegate.swift file instead. They are identical in functionality.
You might be able to use the following code to complete the integration.
import UIKit
import Flutter
// Don't forget to add this!
import simple_auth_flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// Add this
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return SimpleAuthFlutterPlugin.check(url)
}
// Then, add this
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return SimpleAuthFlutterPlugin.check(url)
}
}
Notice that the function names are identical between the Objective-C and Swift versions. Since your iOS project is already using .swift files, it is already setup to use something called a Bridging Header.
This allows code written in Objective-C (in this case, iOS code in the simple_auth_flutter package) to be used from Swift.
Because of this, you can import the required "simple_auth_flutter" module and start using its Objective-C methods from Swift without additional setup.
References
Simple Auth Code for iOS
Related Stack Overflow Question

Google Maps for Flutter iOS Swift setup

The instructions for setting up the official Google Maps for Flutter plugin include adding the Google API key to the AppDelegate.m file:
Specify your API key in the application delegate ios/Runner/AppDelegate.m:
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:#"YOUR KEY HERE"];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
#end
My flutter project has an AppDelegate.swift file instead of an AppDelegate.m file and I'm not sure how to add the required key, as the syntax is different:
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Can anyone help me out?
You can add your API key as follows:
AppDelegate.swift:
import UIKit
import Flutter
import GoogleMaps // Add this line!
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
GMSServices.provideAPIKey("YOUR_API_KEY") // Add this line!
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
One more thing, Dont forget to add below line on ios/Runner/Info.plist
<key>io.flutter.embedded_views_preview</key>
<true/>

Implementing Facebook Login IOS

I'm trying to follow the facebook developers website's instructions for implementing their login. Step 5 is telling me to add some code to my AppDelegate class, specifically AppDelegate.m (which I don't have, I only have AppDelegate.swift).
This is the code I'm supposed to add:
// AppDelegate.m
#import <FBSDKCoreKit/FBSDKCoreKit.h>
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
// Add any custom logic here.
return YES;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
];
// Add any custom logic here.
return handled;
}
If I try putting it into the AppDelegate.swift file I get all sorts of errors, and I'm not finding any good documentation out there to do this properly. How do I solve this?
That code in objective-c , you should use swift
import FBSDKCoreKit
import FBSDKLoginKit
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ app: UIApplication,open url: URL,options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
{
if #available(iOS 9.0, *) {
let sourceApplication: String? = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url,sourceApplication: sourceApplication, annotation: nil)
}
return true
}
public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)
}

Facebook SDK Setup

I'm trying to add the Facebook SDK to my app, I've followed every step.
the only problem I have is that the the the code that I need to add to the AppDelegate is in Objective C and I'm using Swift. I don't know how to convert the Objective C code to swift, Can you help me?
This is the code:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
Thanks!
try facebook login code :
Appdelegate.swift
import UIKit
import FBSDKCoreKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func applicationWillResignActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
}
ViewController.swift
import UIKit
import FBSDKLoginKit
class ViewController: UIViewController {
var dict : NSDictionary!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func btnFBLoginPressed(sender: AnyObject) {
var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager .logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
if (error == nil){
var fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.containsObject("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
})
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
self.dict = result as NSDictionary
println(result)
println(self.dict)
NSLog(self.dict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as String)
}
})
}
}
}

Ignoring the dynamic type in iOS: Accessibility

Is there a way to completely ignore dynamic type/font size settings in iOS apps?
I mean is there a way like a plist entry so that I can disable it completely. I understand there is a notification we can observe and re-configure the font whenever there is change in the settings. I am looking for a simpler solution. I am using iOS8.
Thanks.
There's no need to swizzle UIApplication. Just subclass UIApplication and provide your own implementation of preferredContentSizeCategory:
Swift:
class MyApplication: UIApplication {
override var preferredContentSizeCategory: UIContentSizeCategory {
get { return UIContentSizeCategory.large }
}
}
UIApplicationMain(
CommandLine.argc,
CommandLine.unsafeArgv,
NSStringFromClass(MyApplication.self),
NSStringFromClass(AppDelegate.self)
)
Objective-C:
#interface MyApplication : UIApplication
#end
#implementation MyApplication
- (UIContentSizeCategory) preferredContentSizeCategory
{
return UIContentSizeCategoryLarge;
}
#end
int main(int argc, char * argv[]) {
#autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([AppDelegate class]));
}
}
In your AppDelegate add:
#import <objc/runtime.h>
#implementation AppDelegate
NSString* swizzled_preferredContentSizeCategory(id self, SEL _cmd)
{
return UIContentSizeCategoryLarge; // Set category you prefer, Large being iOS' default.
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
Method method = class_getInstanceMethod([UIApplication class], #selector(preferredContentSizeCategory));
method_setImplementation(method, (IMP)swizzled_preferredContentSizeCategory);
...
}
Just provide Swift 4 fix to #gebirgsbärbel answer.
"preferredContentSizeCategory" in Objective-C is a method, but in Swift it is a read-only variable. So in your AppDelegate is like this:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// MARK: - UIApplicationDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UIApplication.classInit
self.window = UIWindow(frame: UIScreen.main.bounds)
...
self.window?.makeKeyAndVisible()
return true
}
}
// MARK: - Fix Dynamic Type
extension UIApplication {
static let classInit: Void = {
method_exchangeImplementations(
class_getInstanceMethod(UIApplication.self, #selector(getter: fixedPreferredContentSizeCategory))!,
class_getInstanceMethod(UIApplication.self, #selector(getter: preferredContentSizeCategory))!
)
}()
#objc
var fixedPreferredContentSizeCategory: UIContentSizeCategory {
return .large
}
}
The swift equivalent to the answer of #meaning-matters looks as follows:
In your AppDelegate:
#objc func swizzled_preferredContentSizeCategory() -> UIContentSizeCategory {
return UIContentSizeCategory.small
}
open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let originalMethod = class_getInstanceMethod(UIApplication.self, #selector(preferredContentSizeCategory))
let swizzledMethod = class_getInstanceMethod(UIApplication.self, #selector(swizzled_preferredContentSizeCategory))
method_exchangeImplementations(originalMethod, swizzled_preferredContentSizeCategory)
}

Resources