- Home /
Cannot receive URL Scheme with App not running
Hi.I made a iOS app to receive URL Scheme from Browser and show URL. But sometimes not works.(Launch App is always no problem, but cannot receive URL) Can you help me?
Suspend or Background -> fine
Not running -> bad
UrlReceiver.cs (Attach with Empty Object)
public class UrlReceiver : MonoBehaviour {
public Text text;
void OnOpenUrl(string url){
text.text = url;
}
}
#import "UnityAppController.h"
void UnitySendMessage(const char* objName, const char* methodName, const char* param);
char* MakeStringUrl(const char* string) {
if (string == NULL) return NULL;
char* res = (char*)malloc(strlen(string) + 1);
strcpy(res, string);
return res;
}
@interface UnitySchemeController : UnityAppController{
}
@end
@implementation UnitySchemeController
-(BOOL) application:(nonnull UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options {
NSString* message = [url absoluteString];
UnitySendMessage("UrlReceiver", "OnOpenUrl", MakeStringUrl([message UTF8String]));
return YES;
}
@end
IMPL_APP_CONTROLLER_SUBCLASS(UnitySchemeController)
Is your GameObject name same as "UrlReceiver" on which the script is attached?
Answer by Imankit · Sep 04, 2017 at 07:19 AM
The best way to do it is to save the urlscheme data in playerprefs and access that in unity.
For example do something like this-
-(BOOL) application:(nonnull UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options {
if([url.absoluteString rangeOfString:@"mygame://text="].location != NSNotFound){
[[NSUserDefaults standardUserDefaults] setObject:url.absoluteString forKey:@"myurl"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
This will store your urlscheme data to myurl key in playerprefs. Also you need to check some predefined text in urlscheme before saving so that every url scheme will not get saved in myurl key.
Hey Imankit...Could explain in detail....could help answering this questionQuestion
Your answer
Follow this Question
Related Questions
Returning a byte array from ObjC to C# script on ios 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to pass unicode text from C# to Objective-C 1 Answer
iOS Restoring Purchases through Playfab 0 Answers