I am new to Swift and iOS development. I am trying to wrap a web app where the orientation is dependent on the URL. I have the code working with Stack Overflow as an example where "https://stackoverflow.com" displays in portrait and all other pages change to landscape after being loaded. I have a URL observer that triggers when the URL changes and calls requestGeometryUpdate. I'm running into the following problem:
When changing the orientation with requestGeometryUpdate, the orientation changes, but if the device is physically rotated after the change, the orientation changes again. I would like to make the orientation change locked and permanent until a new page is loaded.
Any help would be much appreciated. My code is below:
import SwiftUI
import WebKit
struct TestView: View {
private let urlString: String = "https://stackoverflow.com/"
var body: some View {
TestWebView(url: URL(string: urlString)!)
.background(Color.black)
.scrollIndicators(.hidden)
.ignoresSafeArea([.all])//stretchs webview over notch on iphone
.defersSystemGestures(on:.bottom)//deprioritizes multitasking indicator
.statusBar(hidden: true)//hides time and battery
}
}
class TestController: UIViewController {
var webview: WKWebView!
var webViewURLObserver: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
let winScene = UIApplication.shared.connectedScenes.first
let windowScene = winScene as! UIWindowScene
webview = WKWebView(frame: self.view.frame)
webview.autoresizingMask = [.flexibleWidth,.flexibleHeight]//makes webview fit screen in portrait and landscape
self.view.addSubview(self.webview)
webViewURLObserver = self.webview.observe(\.url, options: .new) { webview, change in
let url=change.newValue!!;//! converts from optional to string
print(url)
let arr = url.absoluteString.split(separator: "stackoverflow.com").map(String.init)
var portrait=false
if(arr.count>1){
let path = arr[1];
if path=="/"{
portrait=true
}
}
if portrait==true {
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait)) { error in print(error)}
}
else{
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .landscape)) { error in print(error)}
}
self.setNeedsUpdateOfSupportedInterfaceOrientations()
}
}
}
// WebView Struct
struct TestWebView: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: Context) -> TestController {
let webviewController = TestController()
return webviewController
}
func updateUIViewController(_ webviewController: TestController, context: Context) {
let request = URLRequest(url: url)
webviewController.webview.scrollView.contentInsetAdjustmentBehavior = .never
webviewController.webview.load(request)
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
Post
Replies
Boosts
Views
Activity
We are developing a kids application that has a small number of videos to be played in the app. At this point, we are hosting the videos on youtube. Right now if the device has strict parental controls on, then before the video is played there is a prompt that says "Allow content from youtube.com" and the parent has to allow all content from youtube. I was wondering if it possible to add a small number of URLs to a whitelist in xcode, so that the users can watch this small number of videos while using the native iOS app without having to allow access to all of youtube.
We are making a design app where users can create designs, and then use those designs to print a T-shirt with that design. We are thinking about subscription model, where a user pays $49/year to access that design tool. With the yearly subscription, they will receive $10 credit toward making a T-shirt. Our terms would like to make the $10 credit contingent on keeping the yearly subscription.
Consider this scenario: a user subscribes to app through the app store, and immediately uses the $10 credit for the physical goods, and then requests a refund for the app through reportaproblem.apple.com. In this case:
are we able to charge them $10, or
will they receive a full refund and we will have to pay the $10 cost ourselves?
I am beta testing an app that has never been submitted to the App Store, and I am trying to test In-App Subscriptions on my app. I am looking for some documentation to get this to work. So far, I have done the following:
In App Store Connect under this app in the Subscriptions section, I have have created a Subscription Group with a single subscription.
The status of the subscription is "Ready to Submit" and at the top of the page for that subscription group I see the message "Your first subscription must be submitted with a new app version. Create your subscription, then select it from the app’s In-App Purchases and Subscriptions section on the version page before submitting the version to App Review."
I see where to add the subscription to the app under the App Store section of App Store Connect, but I am not sure how to add this to TestFlight. When I run the app and try to make the purchase, I get the "This item cannot be found" error:
error 12:15:56.241763-0700 storekitd AMSPurchaseTask: [E4F222FA_SK1] Purchase completed with error: Error Domain=AMSErrorDomain Code=305 "Server Error" UserInfo={AMSURL=https://sandbox.itunes.apple.com/WebObjects/MZBuy.woa/wa/inAppBuy?guid=00008027-000104342E62402E, AMSServerErrorCode=3504, NSLocalizedFailureReason=This item cannot be found., AMSServerAllowed=false, NSLocalizedDescription=Server Error, AMSStatusCode=200, AMSServerPayload={
"cancel-purchase-batch" = 1;
customerMessage = "This item cannot be found.";
failureType = 3504;
"m-allowed" = 0;
pings = (
);
}}